Kubernetes Day 4: Services Explained — Discovery, Load Balancing & Networking
Kubernetes Day 4: Everything About Services
Service Discovery, Load Balancing & Networking — the three problems every Kubernetes cluster must solve
📋 What you will learn today
1. The 3 Real Problems Kubernetes Services Solve
Before we learn what a Service is, let us understand why it exists. On Day 3, we learned that Deployments keep your Pods running via auto healing. But auto healing creates a new problem — and that problem splits into three real-world scenarios every team faces.
Deployments implement auto healing using ReplicaSets. When a Pod goes down and a new one comes up, it gets a brand new IP address. The old IP is gone forever. So how does a customer, another service, or even your frontend know the new IP? They cannot hardcode it — it changes every single time.
If your application needs to handle many concurrent users, you run multiple replicas of a Pod. Each replica gets its own unique IP. But your users need one single address — just like everyone in the world accesses Google via google.com or 8.8.8.8, not by calling individual Google servers directly. Who manages the one-to-many routing?
Your application runs inside Pods, inside a cluster. By default, that is a private network. People from other teams in your organisation — or customers on the internet — do not have access to the cluster's internal network. How do you safely expose your application to the outside world?
A Kubernetes Service is a stable network abstraction that sits in front of your Pods and solves all three problems at once — dynamic IP changes, load balancing across replicas, and controlled external exposure. It is one of the most important objects you will work with every day.
2. What is a Kubernetes Service?
A Kubernetes Service is a stable, long-lived network endpoint that provides a consistent way to access a group of Pods. While Pods are ephemeral (they come and go with changing IPs), a Service has a fixed IP address and DNS name that never changes — regardless of how many times the Pods behind it are replaced.
Think of it this way:
A Service does three things simultaneously:
- Service Discovery — gives Pods a stable DNS name and IP so other services can always find them, even after restarts
- Load Balancing — distributes incoming traffic evenly across all healthy Pods that match its selector
- Exposure control — determines who can access the Pods: only within the cluster, within your organisation's network, or the entire internet
3. How Services Work — Labels, Selectors & kube-proxy
Understanding how a Service actually connects to Pods is important — especially for debugging. There are three moving parts.
Part 1 — Label selectors (how a Service finds its Pods)
A Service does not know about specific Pod names or IPs. Instead, it uses a label selector — it asks: "Give me all Pods in this cluster that have the label app: nginx." Whatever Pods match that selector at any given moment are the targets. When an old Pod dies and a new one comes up (with the same labels), the Service automatically starts routing to the new Pod. This is how the dynamic IP problem is solved — the Service tracks labels, not IPs.
ClusterIP: 10.96.0.1
IP: 10.1.0.4
IP: 10.1.0.7
IP: 10.1.0.9
Part 2 — Endpoints (the live list of Pod IPs)
When you create a Service, Kubernetes automatically creates an Endpoints object with the same name. This object holds the live list of IP:port pairs for all Pods currently matching the selector. As Pods come and go, the Endpoints object is updated automatically by the Endpoints Controller.
Part 3 — kube-proxy (the routing engine)
kube-proxy runs on every node in the cluster. It watches the API Server for Service and Endpoints changes and maintains iptables rules on each node. These rules intercept traffic destined for a Service's ClusterIP and redirect it to one of the actual Pod IPs — performing the load balancing at the network level. No traffic ever actually goes to the ClusterIP itself; it is a virtual IP that only exists in routing rules.
10.96.0.1:80 (Service) arrives at 10.1.0.7:80 (a real Pod).4. ClusterIP — Solving Problem 1 & 2 (Internal Discovery + Load Balancing)
ClusterIP is the default Service type. It exposes the Service on a virtual IP that is reachable only from inside the cluster. Every other Pod in the cluster can reach your app via this stable IP or its DNS name — regardless of how many times the Pods behind it restart and change IPs.
This directly solves Problem 1 (dynamic IPs) and Problem 2 (one entry point for multiple replicas).
How DNS works with ClusterIP
Kubernetes runs a DNS server (CoreDNS) inside the cluster. Every Service automatically gets a DNS name in the format:
ClusterIP Service YAML
5. NodePort — Solving Problem 3 (Access from Inside Your Organisation)
NodePort extends ClusterIP by also opening a specific port on every node in the cluster. Traffic sent to any node's IP on that port gets forwarded to the Service and then to a Pod. This makes your app accessible from outside the cluster — as long as you can reach any node's IP address.
This solves Problem 3 for internal teams — a developer in another team who can reach the cluster's node IPs (but does not have kubectl access) can now access your application.
How NodePort works
NodePort Service YAML
minikube service nginx-nodeport --url to get the exact URL to open in your browser. Minikube's node IP is not a regular IP — this command gives you the correct access URL.6. LoadBalancer — Solving Problem 3 for Production (Internet Access)
LoadBalancer is the standard way to expose an application to the internet in production. When you create a Service of type LoadBalancer on a cloud-managed Kubernetes cluster (EKS, GKE, AKS), Kubernetes automatically talks to the cloud provider's API and provisions a real cloud load balancer — an AWS ALB, a GCP Load Balancer, or an Azure Load Balancer — with a public IP address.
External users access your application through that public IP or domain, and the cloud load balancer forwards traffic into your cluster, to the Service, and then to your Pods.
How LoadBalancer works
Public IP assigned
LoadBalancer Service YAML
<pending> state because there is no cloud provider to assign an IP. Run minikube tunnel in a separate terminal — this simulates a cloud load balancer and assigns a local IP.7. All 4 Service Types — When to Use Each
Internal only
Exposes the Service on a virtual IP reachable only from within the cluster. Default type — used for all inter-service communication. Your frontend Pod calls your backend Pod using its ClusterIP DNS name.
Organisation access
Opens a port (30000–32767) on every node. Anyone who can reach a node's IP can access your app. Automatically creates a ClusterIP too. Best for dev/test environments and internal org access.
Internet production access
Provisions a real cloud load balancer with a public IP. The production standard for customer-facing services. Creates a ClusterIP and NodePort automatically. Costs money — the cloud provider charges for the LB.
DNS alias
Maps a Kubernetes Service to an external DNS name (like an RDS database or a third-party API). When a Pod queries this Service, it gets the external DNS name back. No proxying — pure DNS aliasing.
Comparison table
| Feature | ClusterIP | NodePort | LoadBalancer | ExternalName |
|---|---|---|---|---|
| Accessible from inside cluster | ✓ Yes | ✓ Yes | ✓ Yes | ✓ Yes |
| Accessible from org network | ✗ No | ✓ Yes | ✓ Yes | ✗ No |
| Accessible from internet | ✗ No | ⚠️ Risky | ✓ Yes | ✗ No |
| Load balancing | ✓ Yes | ✓ Yes | ✓ Yes | ✗ No |
| Stable DNS name | ✓ Yes | ✓ Yes | ✓ Yes | ✓ Yes |
| Cloud LB provisioned | ✗ No | ✗ No | ✓ Yes | ✗ No |
| Extra cost | Free | Free | Cloud LB cost | Free |
| Best for | Internal services | Dev / testing | Production | External DNS mapping |
8. Full Hands-on Demo
Let us deploy an Nginx application, create all three main Service types, and see exactly how each one works. Make sure minikube start is running before you begin.
Step 1 — Create the Deployment
Step 2 — Create a ClusterIP Service and test service discovery
Step 3 — Create a NodePort Service and access from outside
Step 4 — Prove that Service Discovery works across Pod restarts
Step 5 — Clean up
🎯 Putting It All Together — 3 Problems, 1 Solution
| Problem | Root cause | Solved by | How |
|---|---|---|---|
| Problem 1 Dynamic Pod IPs |
Auto healing creates Pods with new IPs every time | Service (any type) | Service tracks Pods by labels, not IPs. New Pod = same label = automatically included in routing |
| Problem 2 Multiple replicas, one address |
3 Pods = 3 IPs, users need one entry point | Service (ClusterIP) | Single stable ClusterIP + DNS name. kube-proxy load balances across all matching Pods |
| Problem 3 External access |
Cluster network is private by default | NodePort (org) or LoadBalancer (internet) | NodePort opens a port on every node. LoadBalancer provisions a public cloud LB with a real IP |
📌 Day 4 Recap — What You Learned
- The 3 problems: Dynamic Pod IPs after restarts, multiple replicas needing one address, and external access to a private cluster — all three are solved by Kubernetes Services.
- What a Service is: A stable, long-lived network endpoint with a fixed IP and DNS name that sits in front of your Pods and handles discovery, load balancing, and access control.
- How it works: Label selectors identify Pods dynamically. The Endpoints object holds live Pod IPs. kube-proxy maintains routing rules on every node to redirect traffic.
- ClusterIP: Internal only. Default type. Use for all service-to-service communication inside the cluster. Gets a stable DNS name like
my-service.default.svc.cluster.local. - NodePort: Opens a port on every node. Good for internal org access, dev/test, and Minikube demos. Not recommended for direct internet exposure.
- LoadBalancer: Provisions a real cloud load balancer with a public IP. The production standard for internet-facing applications on cloud clusters.
- Demo: You created a Deployment, exposed it via ClusterIP and NodePort, proved service discovery works across Pod restarts, and verified the Endpoints object updates automatically.
Comments
Post a Comment