Day 34 - Working with Services in Kubernetes

Day 34 - Working with Services in Kubernetes

What are Services in K8s

In Kubernetes, Services are objects that provide stable network identities to Pods and abstract away the details of Pod IP addresses. Services allow Pods to receive traffic from other Pods, Services, and external clients.

In K8s why do we need services?

In k8s, when a pod is get created an IP address is allocated to that pod and the Kube proxy component from the worker node is responsible for that. So if we want to communicate any content inside the cluster we have to communicate it through a pod's Ip and in a Node there can be many pods. But what will happen, is if a pod gets deleted due to any reason like accidentally or gets crashed, then with the help of the Replicaset controller again a pod will get created within a sec and also its IP address will get changed.

If anyone is accessing the container from the outside of the world, for them it's not possible to know about the changed Ip address and no one will hit by giving a Pod's IP address, they will always hit the DNS [like www. google .com]. So, a pod's Ip can keep changing.

To solve this issue We create a service object on top of a deployment object.

Whenever we create any service file, an IP address is created along with it and if any pod gets failed due to any reason and a new pod gets created there will be no issue because a pod's IP will get mapped to the service's IP. So if anyone will try to communicate from the outer world by default the request will be redirected to the service's IP.

Types of Services :

There are mainly three types of Services used widely.

  1. ClusterIP: Whenever you will create a service yaml file a default Ip will get allocated to it, this is known as ClusterIp.With the help of cluster ip, we can communicate with our application Internally within the cluster only.

  2. NodePort: A NodePort service exposes the service on the IP of each node at a static port. A ClusterIP service is created automatically to route the traffic to the NordPort service. Users can communicate with the service from the outside.

  3. LoadBalancer: This is the preferred solution to expose the cluster to the wider internet. The LoadBalancer type of service will create a load balancer (load balancer type depends on the cloud provider) and expose the service externally.

    It will also automatically create ClusterIP and NodePort services and route traffic accordingly.

Service Type as Nodeport

vi deployment.yaml

Deployment code https://github.com/rjthapaa/react_django_demo_app/blob/main/deployment.yaml

Change the Service type as Nodeport

$ kubectl apply -f deployment.yaml

Service Type as ClusterIP

vi deployment.yaml

Change the Service type as ClusterIP

$ kubectl apply -f deployment.yaml

Service Type as Loadbalancer

vi deployment.yaml

Change the Service type as ClusterIP

$ kubectl apply -f deployment.yaml

You can access the application in a web browser using Nodeport and LoadBalancer service only. ClusterIP can be only accessible inside the cluster only.

For Nodeport and LoadBalancer use K8sMasterorslavepublicIP:Portnumber(Nodeport)

For accessing ClusterIP service inside cluster use POD IP:port (Application port number)