Working with Services in Kubernetes [Day-33 Task]

Working with Services in Kubernetes [Day-33 Task]

Navigating Kubernetes Services: Building Efficient Networking Solutions

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.

Before going straight away to tasks, let us recall the types of services in a tabular form so it becomes easy for memorization.

FeatureClusterIPNodePortLoadBalancer
AccessOnly accessible within the cluster.Accessible from within the cluster and from outside the cluster via a specific port on each node.Accessible from within the cluster and from outside the cluster via a single IP address and port.
External AccessNot accessible from outside.Accessible using the node's IP.Accessible using public IP.
How does it work?Assign a virtual IP inside the cluster to the service.Exposes the service on a static port on each worker node's IP.Provisioning of an external load balancer.
ConfigurationNot required.'nodePort' should be specified.'externalIPs' and 'ports' should be specified.
When to use it?Services not requiring accessibility outside the cluster.Services that need accessibility outside the cluster without scaling.Services that need accessibility outside the cluster with scaling.
ExampleInternal services, backend APIs.Exposing apps to external users.Production environments requiring external access.
Exposed PortsA specific port within the cluster.A static port on each worker node.The port is provided by an external LB.
AvailabilityService discovery and load balancing within the cluster.Distributes traffic across worker nodes.Utilizes external load balancer.
Infrastructure DependencyInternal cluster networking.Worker nodes' network configuration.Cloud provider with LB support.

I am performing today's activities on the same Django todo app that I used in the Django deployment project.

Let's start with the tasks.

Task-1: Create a Service for your todo-app Deployment

  • Create a service.yml file into the previously created namespace[django-ns]

  • Then create the service file using the vim editor and mention its type to ClusterIP[ still if you don't mention its type by default it will create a ClusterIp ] and mention the selector as pod's selector to identify the pod for which the service is getting created. Through this service's ip, you can communicate with your application internally from another pod.

  • Then apply the service.yml file like the below command.

  • Check the service status, you will notice that a ClusterIP is created.

    If you want to communicate to your application internally from another pod you can do it by using the curl command.

[Here kind will be the Service type, In the spec section type will be clusterIP and the selector will be the same as the pod's selector. In the part of the ports, the port is the host port and targetPort is the 8000 container port ]

Task-2: Create a ClusterIP Service for accessing the todo-app from within the cluster

Now instead of creating a ClusterIP, create a service type NodePort. When you will create a Nodeport type, along with it a ClusterIp will be created.

Just change its type to NodePort and save the file.

Then apply the command and if you check the service's status now its type is Nodeport. A node port range(30000-32767) will also be created. A node port range remains between 30000-32767 or you can also specify a node-port in your service file any range between this.

To get the nodeport url to execute the below command and you will get a URL. If you curl to this URL the application will get redirected to it and it is running internally.

The application is accessible and if everything is perfect you can access the application from outside by giving your public IP:30775 [nodeport range], allowing the port into the inbound rule.

Task-3: Create a LoadBalancer Service for accessing the todo-app from outside the cluster

Loadbalancer service type generally use,if the load will increase in your application from outside the world while accessing it.

A loadbalancer will be created if you are using any cloud platform.

This time type will be LoadBalancer.

In the service's status, an external Ip will get initialized when the loadbalancer will get created and you will able to access the application from outside.

Happy Learning :)