Sub topic 11
kube-proxy: routing traffic to Service backends
This chapter assumes you know what a Pod and a Service are, and that the API server stores cluster objects. It covers how a Service's virtual IP ends up reaching a real Pod.
The problem kube-proxy solves
A Service gives a set of Pods one stable virtual IP, called the ClusterIP. No network interface owns that address. Packets sent to it must be rewritten to the IP of a real Pod. kube-proxy is the node component that sets up this rewriting.
What kube-proxy does
kube-proxy runs on each node, usually as a DaemonSet in kube-system. It does three things:
- It watches the API server for Service and EndpointSlice objects.
- It translates them into packet-forwarding rules on its own node.
- It updates those rules when Pods come and go.
In the common iptables and IPVS modes, kube-proxy only programs the kernel. Traffic is rewritten by the kernel and does not pass through the kube-proxy process. Newer clusters can also use an nftables mode.
Following one Service
Take this Service from the earlier chapters:
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80The control plane keeps an EndpointSlice listing the IPs of Ready Pods whose labels match app: web. kube-proxy reads that list. It then installs rules on every node that say: traffic to the ClusterIP on port 80 goes to one of those Pod IPs on port targetPort. The backend is chosen per connection. In iptables mode the choice is effectively random.
Inspecting it
kubectl get service web
kubectl get endpointslices -l kubernetes.io/service-name=web
kubectl get pods -n kube-system -o wideThe first command shows the ClusterIP. The second shows the Pod IPs that kube-proxy will forward to. The third shows the kube-proxy Pods, normally one per node.
Common errors
- Empty endpoints. If the selector matches no Ready Pods, the EndpointSlice is empty. The Service has an IP but nothing answers. Compare the Service
selectorwith the Pod labels. - Wrong
targetPort. Rules forward to the port you declare. If the container listens elsewhere, connections fail. - Pinging the ClusterIP. The address is only a forwarding rule for the declared ports, so
pingusually fails even when the Service works. - Assuming kube-proxy is always present. Some network plugins, such as Cilium, can replace it. Then you will find no kube-proxy Pods.
Exercise
A Service named api has a ClusterIP, but connections to it time out. kubectl get endpointslices shows no endpoints for it. What should you check first, and why is kube-proxy probably not the cause?
Show answer
Check that the Service selector matches the labels on running, Ready Pods. kube-proxy only turns EndpointSlice contents into forwarding rules. With no endpoints there is nothing to forward to, so the fault lies in the labels or Pod readiness.