Kubernetes Services, an easy explanation!

H Dev
2 min readAug 9, 2023

If you are anything like me, it’ll take you a while to grasp networking concepts. But here I am to make that task a little simpler.

Photo by Luca Bravo on Unsplash
  1. Service Creation: When you create a service in Kubernetes, you define its type (ClusterIP, NodePort, LoadBalancer, or Headless). You also specify the selector, which is a label that identifies the pods that this service should route traffic to.
  2. Service Virtual IP (ClusterIP): For a ClusterIP service, Kubernetes assigns a virtual IP address (e.g., 10.0.0.1) to the service. This IP is internal to the cluster and acts as the main entrance for communication with the pods associated with the service.
  3. Pods Selection: The service uses the selector you provided to discover pods that match the labels. These are the pods that the service will route traffic to. For instance, if you have a label “app=web” on your pods and your service selector is “app=web,” the service will route traffic to those pods.
  4. Internal Load Balancing (ClusterIP): When a pod wants to communicate with a service, it sends a request to the ClusterIP’s virtual IP. The Kubernetes service component then uses its internal routing table to determine which pods are associated with the service. It balances the load by forwarding the request to one of these pods. This provides internal load balancing among the pods.
  5. NodePort Allocation (NodePort): In a NodePort service, Kubernetes reserves a specific port (e.g., 30080) on each node. This port serves as the entry point for external traffic. When a request comes in to a node’s IP on the NodePort, the node forwards the request to one of the pods associated with the service based on the selector.
  6. Load Balancer Setup (LoadBalancer): For a LoadBalancer service, Kubernetes interacts with the cloud provider’s API to provision an external load balancer. This external IP address is what users will access. Incoming traffic hits the load balancer, and it distributes the requests among the pods linked to the service, ensuring even load distribution.
  7. Individual Pod IPs (Headless Service): With a Headless service, Kubernetes doesn’t allocate a virtual IP. Instead, when you query the Headless service, it directly returns the individual IPs of the pods associated with the service. Applications can use these individual IPs to establish direct connections to specific pods, often beneficial for stateful applications like databases.

By understanding how each type of Kubernetes service functions, you can strategically choose the appropriate type based on your application’s needs. Services are an essential mechanism for enabling communication within the cluster and making your applications accessible from the external world.

Happy learning! Hope you got a clear understanding…

Follow and shower some claps as a token of encouragement !

--

--

H Dev

just another X-shaped personality, love to learn and tinker with new tech.