Services deep-drive in Kubernetes:

Services deep-drive in Kubernetes:
  1. What if there is no concept of Services in K8's?

Suppose a POD is running on a IP address of 172.16.2.3 but since Pods are ephemeral in nature they might be created and destroyed.

Once they are again running surely it will have a new IP address associated with it. This leads to a problem, if some user tries to reach that set of Pod inside your cluster, it will not be reachable or cant connect.

So now Comes SERVICE [svc]:

On top of Deployment, create a service and this service acts as a Load Balancer. Instead of accessing IP address access the service.

Service Discovery:

If service is keeping track of a deployment and it keeps 3 pods and if any one IP address changest then there is a problem.

So Services came up with Labels & Selectors

For every Pod that is being created. DevOps engineers will apply a Label, label will be common for all pods. so if the Label is same despite change in IP addresss, the probelm is solved.

Expose to external world:

Kubernetes Services Explained

    • Service (ClusterIP): This type of service exposes pods within the Kubernetes cluster. It provides a stable IP address and DNS name that other applications within the cluster can use to access the pods. Traffic is load-balanced across all pods that match the service's selector.
  • Service (NodePort): This type of service exposes pods on a specific port of each node in the cluster. It allows external access to the pods from outside the cluster by forwarding traffic to the appropriate nodes and then to the pods.
  • External Load Balancer (LoadBalancer): In a cloud environment, an external load balancer can be used to distribute incoming traffic from the internet to the nodes in the Kubernetes cluster. It typically forwards traffic to nodes based on the NodePort service.
  1. Kubernetes Cluster-IP practical:

  • First clone this repo or you can take any github repo for code:

    https://github.com/iam-veeramalla/Docker-Zero-to-Hero.git

  • cd /Docker-zero-to-hero/examples/python-web-app and you will find the Dockerfile, just build the image:

    docker build . -t sample-python-app:latest

    push to image to dockerhub and make it public

    docker tag sample-python-app:latest subho4563/sample-python-app:latest

    docker push subho4563/sample-python-app:latest

  • Now create a deployment.yml file ----> go to k8s docs and take a sample deployment yaml file and make the necessary changes.

    such as --> how many replicas you want, giving proper lables and selectors, and image image & port no.

  • Now lets create the k8s cluster with the deployment.yml file

    kubectl apply -f deployment.yml

  • Now to check the pods are running and to see the ip address of the running pods run commands:

    kubectl get pods

    kubectl get pods -o wide

  • Now to run the application within the cluster just - minikube ssh

    So you can see you are able to run the application inside your K8s Cluster because by default you get a CLUSTER IP service.

  • Now if you try to run the app outside the K8s cluster then you will see no traffice is hitting and you are not getting any response.

    1. Kubernetes NodePort practical:

  • Now create a service.yml file ----> go to k8s docs and take a sample service (NodePort) yaml file and make the necessary changes.

    such as --> selectors name & port no.

    • Now create the service:

    kubectl apply -f service.yml

    • Now to see NodePort type is allocated along with port no:

    kubectl get svc

    By default it will have a Cluster IP as well, you can use the 30007 default port to run the app outside the cluster but within your organisation.

    You can see the app is executing, and even you can check on your own laptop browser, the app will be running

    minikube ip

    curl -L 192.168.49.2:30007/demo

  1. Kubernetes LoadBalancer hands-on:

  • Simply edit the svc yaml file and in place of nodeport just insert LoadBalancer

  • Now if you do kubectl get svc, you can see the Type- LoadBalancer with pending IP.

    Because since this is a minikube cluster you wont get any public ip address, if you use AWS, AZURE or GCP then you will get a public ip by which your app can be used by external and it happens with the help of Cloud Controller Manager.

    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxThankyou for checking out my blog, feel free to provide your outputs.