Sub topic 5
ClusterIP: internal virtual IPs
This chapter assumes you can apply YAML with kubectl, know what Pods, Deployments and labels are, and understand IP addresses, ports and DNS. A ClusterIP Service gives a changing set of Pods one stable address that works only inside the cluster.
What a ClusterIP is
ClusterIP is the default Service type. When you create one, the control plane allocates a virtual IP from the cluster's Service CIDR. No network interface holds that address and no process listens on it. Each node's kube-proxy (or an equivalent data plane) installs rules that rewrite traffic sent to the virtual IP so it goes to one of the Pods behind the Service.
Pod IPs change whenever a Pod is replaced. The ClusterIP stays the same for the life of the Service, so clients can depend on it.
Creating one
The Service below selects the Pods from the web Deployment (label app: web). It listens on port 80 and forwards to the container port named http.
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: ClusterIP
selector:
app: web
ports:
- name: http
port: 80
targetPort: httpApply it and inspect it:
kubectl apply -f service.yaml
kubectl get service webThe CLUSTER-IP column shows the allocated address. Any Pod in the cluster can now reach the backends at http://<cluster-ip>:80, or at http://web through DNS (covered in a later chapter).
How a request flows
The client Pod sends a packet to the virtual IP. The rules on the client's own node pick a ready backend Pod and rewrite the destination address to that Pod's IP and the targetPort. The reply is translated back, so the client sees the answer coming from the Service address.
Things to know
- The IP is virtual. Pinging it usually fails because the rules match only the Service's TCP, UDP or SCTP ports, so a failed
pingdoes not mean the Service is broken. - Backends are chosen per connection, not per request. A long-lived connection stays on one Pod.
- You can request a fixed address with
spec.clusterIP. It must be free and inside the Service CIDR. - The address is reachable only from inside the cluster network. For outside access you need NodePort, LoadBalancer or Ingress, which later chapters cover.
Common errors
| Symptom | Likely cause |
|---|---|
| Connection refused or times out | The selector matches no ready Pods, so the Service has no endpoints |
| Works on one port, fails on another | `targetPort` does not match a port the container listens on |
| Reachable from a Pod, not from your laptop | ClusterIP is internal by design |
| `ping` gets no reply | Expected, since the IP is virtual |
Exercise
A Service has port: 80 and targetPort: http. A client Pod connects to the ClusterIP on port 80. Which address and port does the traffic reach, and why does ping to the ClusterIP not work?
Show answer
Traffic reaches a ready backend Pod's IP on the container port named http, because the node rules rewrite the destination. Ping fails because the ClusterIP is virtual and only the Service's configured protocol and ports are translated, so ICMP gets no reply.