3/860 min
Primitives
Services β Stable Networking
Give your Pods a stable address and load balance traffic between them.
beginner60 minRead β Lab β Quiz β Practice
π§ Brain Warm-Up
π§ Brain Warm-Up: Pods are constantly dying and getting recreated with new IP addresses. If you have a backend database Pod and a frontend client Pod, how can the frontend reliably connect to the database without updating its connection config on every pod crash? Think about this networking challenge.
The Problem with Pod IPs
Pod IPs are ephemeral. Every time a Pod restarts or is rescheduled, it gets a new IP. If you hardcode a Pod IP, your app breaks after the first restart.
A Service provides:
- A stable Virtual IP (ClusterIP) that never changes for the life of the Service.
- A predictable DNS name:
..svc.cluster.local. - Load balancing across all matching Pods (via label selectors).
Service Types
| Type | Reachable from | Use case |
|---|---|---|
| ClusterIP | Inside cluster only | Microservice-to-microservice communication |
| NodePort | Outside via NodeIP:Port | Dev/testing port exposure |
| LoadBalancer | Internet via cloud LB | Production web apps |
How Services Find Pods
Services use label selectors to dynamically discover their target Pods. The control plane monitors pods and updates the Service's Endpoints or EndpointSlices list.
Service selector: { app: web }
β matches
Pod labels: { app: web, version: v2 } β (Selected)
Pod labels: { app: api } β (Not selected)Kubernetes Service Traffic Routing
Client Request (curl http://web-service:80)
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββ
β SERVICE (web-service) β
β ClusterIP: 10.96.45.100 Port: 80 β
ββββββββββββββββββββββββ¬ββββββββββββββββββββββββ
β (Load Balances traffic via Endpoint list)
βΌ
ββββββββββββββββββ΄βββββββββββββββββ
β (kube-proxy / CNI Routing) β
βΌ βΌ
βββββββββββββ βββββββββββββ
β Pod (web) β β Pod (web) β
β IP: 10.244.1.3 β IP: 10.244.2.4
β Port: 80 β β Port: 80 β
βββββββββββββ βββββββββββββ