Internal Service Discovery (ClusterIP & NodePort)
In Kubernetes, Pods are born, they do their jobs, and sometimes they crash and are replaced. Because Pods are ephemeral (temporary), their IP addresses change constantly. If your frontend application relies on a hardcoded IP to talk to your backend database, your application will break the second that database Pod restarts.
This is where Kubernetes Services come in. Services provide a stable, permanent IP address and a friendly DNS name that seamlessly balances traffic across your ever-changing Pods.
- Why we cannot rely on Pod IP addresses in a microservices architecture.
- How to use ClusterIP to create a stable, internal-only network.
- How to use NodePort to open a specific port across all your cluster's machines.
- How Kubernetes uses labels to automatically discover and route traffic to the correct Pods.
1. Kubernetes Services: The Network Anchor
Think of a Kubernetes Service like a restaurant manager. You (the client) don't need to know the names of all the chefs in the kitchen (the Pods) or which one is currently on a break. You just talk to the manager (the Service), and the manager routes your order to whoever is ready to cook.
There are two primary ways to expose these services internally and to your local network: ClusterIP and NodePort.
| Service Type | Visibility | Best Use Case |
|---|---|---|
| ClusterIP (Default) | Internal to the cluster only. | Connecting internal microservices (e.g., Frontend to Backend, Backend to Database). |
| NodePort | Internal + External (via Node IP). | Testing locally, or exposing an app to external traffic without a Cloud Load Balancer. |
2. Implementing ClusterIP and NodePort
To connect our services, we first need some Pods to connect to. We do this by defining a Deployment with specific Labels. The Service will use a Selector to look for these exact labels.
Step 1: Create the Backend Deployment
Example 1: backend-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-api
spec:
replicas: 3
selector:
matchLabels:
app: backend # <--- The Service will look for this label
template:
metadata:
labels:
app: backend # <--- Applying the label to the Pods
spec:
containers:
- name: api
image: my-backend-api:1.0
ports:
- containerPort: 8080
Step 2: Create the ClusterIP Service
Now we create a ClusterIP service. It will group our 3 backend pods under one stable IP and DNS name.
Example 2: clusterip-service.yaml
apiVersion: v1
kind: Service
metadata:
name: backend-service # <--- This becomes the internal DNS name!
spec:
type: ClusterIP # <--- Internal only (this is the default)
selector:
app: backend # <--- Finds all Pods with this label
ports:
- port: 80 # <--- The port the Service listens on
targetPort: 8080 # <--- The port the Pod is listening on
Expected output (after running kubectl apply)
service/backend-service created $ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) backend-service ClusterIP 10.96.120.45 <none> 80/TCP
http://backend-service. Kubernetes handles the DNS resolution and load balances the request across your 3 backend Pods automatically.Step 3: Creating a NodePort Service
If you need to access a service from your web browser outside the cluster (like a frontend UI), you can use a NodePort. It opens a specific port (between 30000-32767) on every worker node in your cluster.
Example 3: nodeport-service.yaml
apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
type: NodePort
selector:
app: frontend
ports:
- port: 80
targetPort: 80
nodePort: 30050 # <--- Accessible externally via http://NodeIP:30050
3. Architecture & Traffic Workflow Visualized
Let's look at how traffic flows when a Frontend Pod wants to talk to the Backend, utilizing a ClusterIP service.
Kubernetes Service Routing (ClusterIP)
The Frontend Pod uses the internal DNS name to hit the ClusterIP service. The service acts as a load balancer, routing the traffic to healthy Backend Pods based on matching labels.
4. Step-by-Step Execution Breakdown
When you create a Service, a lot happens under the hood in the Kubernetes control plane to ensure traffic routes correctly.
backend-service) to that new IP address.selector (e.g., app: backend) and adds their real, ephemeral IPs to an Endpoints list.kube-proxy daemon running on every node updates the local network rules (iptables or IPVS). When traffic hits the virtual ClusterIP, these network rules transparently intercept and redirect it to one of the real Pod IPs in the Endpoints list.selector labels in the Service definition, the Service will successfully create, but its Endpoints list will be empty. Traffic will hit the Service and simply drop. Always ensure your Service selectors perfectly match your Pod labels!5. Hands-On Practice
Ready to test your knowledge? Try these exercises in a local Minikube or kind cluster.
Deploy an NGINX pod using kubectl create deployment nginx --image=nginx. Now, expose it internally on port 80.
Hint: Use the command kubectl expose deployment nginx --port=80. This automatically creates a ClusterIP service!
Create a NodePort service via a YAML file that maps port 80 to targetPort 80 for the NGINX deployment.
Expected result: You should be able to run kubectl get svc, see the allocated 30000+ port, and access NGINX from your browser using your Node's IP address.
Create a temporary busybox Pod to test the internal DNS resolution of your NGINX ClusterIP service.
Hint: Run kubectl run -i --tty --rm debug --image=busybox -- sh and type nslookup nginx inside the shell.
6. Interview Questions
Understanding Service Discovery is absolutely critical for cloud engineering and DevOps interviews.
Answer: Pods are inherently ephemeral in Kubernetes. They can be killed, scaled down, or rescheduled to different nodes at any time. When a Pod restarts, it is assigned a completely new IP address. If microservices relied on hardcoded Pod IPs, the network would constantly break. Services solve this by providing a stable, virtual IP and DNS name that abstracts away the underlying dynamic Pod IPs.
Answer: A ClusterIP service is strictly internal; it provides a virtual IP accessible only from within the Kubernetes cluster, making it ideal for backend service-to-service communication. A NodePort service builds on top of ClusterIP by additionally opening a specific port (between 30000-32767) on the external IP of every worker node in the cluster, allowing traffic from the outside world to reach the service without a dedicated Load Balancer.
Answer: Services use a Label Selector mechanism. In the Service definition, you define a selector (like app: backend). The Kubernetes control plane continuously watches for Pods that possess these exact labels. When matching Pods are found, their actual IP addresses are dynamically added to an Endpoints object associated with the Service, enabling real-time load balancing.
Conclusion
Mastering Kubernetes Services is the moment you transition from just running containers to actually orchestrating a robust, self-healing architecture. By leveraging ClusterIP for secure, internal networking and NodePort for simple external access, you ensure that your applications remain stable and highly available, regardless of how chaotic the underlying infrastructure becomes. Labels and selectors are the glue holding this dynamic system together—keep them organized, and your microservices will scale beautifully.
Official Documentation
Upgradix by Shrash Studio – Learn Technology. Build Your Future.