Skip to content
LF Cert Trainer

Lesson 14 · Section 2: Core Concepts

Lesson 14: Kube Proxy: the component that makes Service IPs work

What kube-proxy does on every node, how it turns Services and EndpointSlices into kernel rules, the proxy modes, and how to inspect and fix it under exam pressure. · 20 min read · 4 min video

1/11Kube Proxy
0:00 / 4:11

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

Why kube-proxy exists

A Service gives a group of Pods one stable address, the ClusterIP. But that address is virtual. No network interface on any machine has it, no process listens on it, and no router outside the cluster knows it exists. If you send a packet to 10.96.0.20 on a plain Linux machine, it goes nowhere.

Something has to make that address behave like a real one, on every node, and keep up as Pods come and go. That something is kube-proxy. It runs on each node, watches the cluster's Services and their backends, and programs the node's kernel so that a connection to a Service IP is redirected to one of the Pods behind it.

What it is

One kube-proxy per node, all watching the same API servercontrolplanenode01node02watchwatchwatchwriteswriteswritesAPI serverServices, sliceskube-proxyPod, host networkkube-proxyPod, host networkkube-proxyPod, host networkKernel rulessame on every nodeKernel rulessame on every nodeKernel rulessame on every node
One kube-proxy per node, all watching the same API server. A DaemonSet puts one kube-proxy Pod on every node. Each one watches Services and EndpointSlices and writes the same rules into its own node's kernel.

Parts:

  • API server (Services, slices)
  • kube-proxy (Pod, host network), in controlplane
  • kube-proxy (Pod, host network), in node01
  • kube-proxy (Pod, host network), in node02
  • Kernel rules (same on every node), in controlplane
  • Kernel rules (same on every node), in node01
  • Kernel rules (same on every node), in node02

Connections:

  • API server to kube-proxy: watch
  • API server to kube-proxy: watch
  • API server to kube-proxy: watch
  • kube-proxy to Kernel rules: writes
  • kube-proxy to Kernel rules: writes
  • kube-proxy to Kernel rules: writes

kube-proxy is one of the three components that run on every node, next to the kubelet and the container runtime. On a cluster built with kubeadm it looks like this:

  • A DaemonSet named kube-proxy in the kube-system namespace, so there is exactly one Pod per node. Its tolerations let it run on control plane nodes as well: those nodes also need to reach Services.
  • A host network Pod. The Pod uses hostNetwork: true and runs privileged, because its job is to change the node's own network rules. That is why a kube-proxy Pod's IP is the node's IP.
  • A ConfigMap named kube-proxy, also in kube-system. Its key config.conf holds a KubeProxyConfiguration (mode, Pod CIDR, ports), and kubeconfig.conf says how to reach the API server.
  • A ServiceAccount named kube-proxy, bound to the system:node-proxier ClusterRole, which lets it watch Services, EndpointSlices and Nodes. It only reads from the API server; it never writes Service data.

It is just as useful to know what kube-proxy is not, because the name misleads:

JobWho does it
Give a Service its ClusterIPThe API server, from the service CIDR
List the ready Pods behind a ServiceThe EndpointSlice controller, in the controller manager
Resolve the name web.shop.svc to the ClusterIPCoreDNS
Carry a packet from one Pod to another, across nodesThe CNI network plugin
Enforce NetworkPoliciesThe CNI network plugin
Redirect a connection for a Service IP or node port to a Podkube-proxy's rules, in the kernel
kubectl proxy (a local tunnel to the API server)kubectl. It has nothing to do with kube-proxy

How it works

kube-proxy is a small control loop:

  1. A Service or Pod changes
  2. The EndpointSlice is updated
  3. kube-proxy's watch receives it
  4. kube-proxy rewrites the node's rules
  5. New connections use the new rules
From a change in the cluster to a rule in the kernel

Every kube-proxy sees the same Services and EndpointSlices, so every node ends up with the same rules. That matters because the redirect happens on the node where the connection starts: the client's node rewrites the packet before it leaves, and from then on it is plain Pod-to-Pod traffic that the network plugin delivers.

What the kernel does with one new connection (iptables mode)Kernel of the client's nodepacket1 in 31 in 31 in 3Client Podto 10.96.0.20:80KUBE-SERVICESmatch IP and portKUBE-SVC-…pick one at randomKUBE-SEP-…DNAT 10.244.1.7KUBE-SEP-…DNAT 10.244.1.9KUBE-SEP-…DNAT 10.244.2.4web Pod10.244.1.7:8080web Pod10.244.1.9:8080web Pod10.244.2.4:8080
What the kernel does with one new connection (iptables mode). The packet to the Service IP walks three chains on the client's own node. The last one rewrites the destination to a real Pod address; kube-proxy itself never touches the packet.

Parts:

  • Client Pod (to 10.96.0.20:80)
  • KUBE-SERVICES (match IP and port), in Kernel of the client's node
  • KUBE-SVC-… (pick one at random), in Kernel of the client's node
  • KUBE-SEP-… (DNAT 10.244.1.7), in Kernel of the client's node
  • KUBE-SEP-… (DNAT 10.244.1.9), in Kernel of the client's node
  • KUBE-SEP-… (DNAT 10.244.2.4), in Kernel of the client's node
  • web Pod (10.244.1.7:8080)
  • web Pod (10.244.1.9:8080)
  • web Pod (10.244.2.4:8080)

Connections:

  • Client Pod to KUBE-SERVICES: packet
  • KUBE-SERVICES to KUBE-SVC-…
  • KUBE-SVC-… to KUBE-SEP-…: 1 in 3
  • KUBE-SVC-… to KUBE-SEP-…: 1 in 3
  • KUBE-SVC-… to KUBE-SEP-…: 1 in 3
  • KUBE-SEP-… to web Pod
  • KUBE-SEP-… to web Pod
  • KUBE-SEP-… to web Pod

In the default iptables mode, a new connection to 10.96.0.20:80 is handled by three kinds of chains in the kernel's NAT table:

  1. KUBE-SERVICES has one rule per Service port. The rule that matches the destination IP, protocol and port jumps to that Service's own chain.
  2. KUBE-SVC-<hash> has one rule per ready backend and picks one at random.
  3. KUBE-SEP-<hash> (a Service endpoint) does the work: a DNAT rule that rewrites the destination to the Pod's address, such as 10.244.1.9:8080.

The kernel's connection tracking remembers the choice, so the rest of the connection goes to the same Pod and the replies are translated back. The client believes it is talking to 10.96.0.20 the whole time.

Under the hood · How three rules give each Pod a one in three chance

The rules in a KUBE-SVC chain are tried in order, and each has a probability:

-A KUBE-SVC-OI3E… -m statistic --mode random --probability 0.33333 -j KUBE-SEP-AAAA…
-A KUBE-SVC-OI3E… -m statistic --mode random --probability 0.50000 -j KUBE-SEP-BBBB…
-A KUBE-SVC-OI3E…                                                  -j KUBE-SEP-CCCC…

The first rule takes a third of the connections. The second takes half of what is left, which is another third. The last rule takes everything that remains. With more Pods the chain grows, and so does the work the kernel does for each new connection, which is one of the reasons the nftables mode exists.

Under the hood · What happens when kube-proxy stops
  • Nothing, at first. The rules live in the kernel, not in the process. Existing Services keep answering, and you can restart or upgrade kube-proxy without dropping traffic.
  • Then the rules go stale. A new Service gets no rules on that node, so connections to it hang. A Pod that was replaced is still in the old rules, so some connections go to an address where nobody answers.
  • Only that node is affected. Each node has its own kube-proxy and its own rules. "The Service works from Pods on node01 but not from Pods on node02" points straight at node02's kube-proxy.
Under the hood · A Service with no endpoints is refused, not ignored

When a Service has a ClusterIP but no ready endpoints, kube-proxy installs a rule that rejects connections to it. The client gets connection refused at once. When the rules for a Service are missing completely (kube-proxy is not running on that node, or has not synced), the packet has nowhere to go and the client usually hangs until it times out. The difference is a useful first clue: refused points at the Service's endpoints, a hang points at kube-proxy or the network.

Traffic from outside: node ports

A node port answers on every nodenode01 (no web Pod)node02:30080DNAT + SNATwritesOutside clientto node01:30080Kernel rulesKUBE-NODEPORTSweb Pod10.244.2.4:8080kube-proxywrote the ruleskube-proxysame rules here
A node port answers on every node. node01 runs no web Pod, but its rules still accept the connection and forward it to a Pod on node02. On the way it replaces the source address with its own, so the reply comes back the same way.

Parts:

  • Outside client (to node01:30080)
  • Kernel rules (KUBE-NODEPORTS), in node01 (no web Pod)
  • web Pod (10.244.2.4:8080), in node02
  • kube-proxy (wrote the rules), in node01 (no web Pod)
  • kube-proxy (same rules here), in node02

Connections:

  • Outside client to Kernel rules: :30080
  • Kernel rules to web Pod: DNAT + SNAT
  • kube-proxy to Kernel rules: writes

kube-proxy also implements NodePort (and so the node side of LoadBalancer). For a Service with node port 30080, every node gets a rule that sends connections to its own port 30080 into the same KUBE-SVC chain. Three consequences follow:

  • Every node answers, whether or not it runs a backend Pod, because every node has the rule.
  • Nothing listens on the port. It is a kernel rule, not a socket, so ss -tlnp on the node does not show 30080.
  • The source address is replaced. When the node forwards to a Pod on another node it also rewrites the source to its own address (SNAT), so the reply returns through it. The Pod therefore sees the node's IP, not the client's. externalTrafficPolicy: Local on the Service tells kube-proxy to use only Pods on the receiving node: the client IP is kept, and nodes without a local Pod stop answering.

Proxy modes

How the rules are written into the kernel is the proxy mode: the mode field of the configuration, or the --proxy-mode flag.

ModeHow it worksStatus (Kubernetes v1.37)
iptablesiptables chains, random backendThe default on Linux when mode is empty
nftablesnftables rules in a table named kube-proxy, random backend. Faster updates on large clusters; needs kernel 5.13 or laterStable. Announced as the future default
ipvsThe kernel's IPVS load balancer, with schedulers such as round robinDeprecated since v1.35; do not use it for new clusters
kernelspaceWindows nodes onlyThe only mode on Windows

Using it

Example · Check kube-proxy on a kubeadm cluster

Work from the outside in: the DaemonSet, then the Pod on the node you care about, then its logs.

kubectl -n kube-system get ds kube-proxy                       # DESIRED = READY = number of nodes
kubectl -n kube-system get pods -l k8s-app=kube-proxy -o wide  # which Pod is on which node
kubectl -n kube-system logs kube-proxy-9d7mz                   # the Pod on the node with the problem
kubectl -n kube-system describe pod kube-proxy-9d7mz           # events, if it does not start

A healthy log names the mode early on, with a line such as "Using iptables Proxier", and has no repeating errors about reaching the API server.

Example · Find out which mode a cluster uses

Three ways, from the least to the most typing:

# 1. The configuration (an empty mode means the default, iptables)
kubectl -n kube-system get configmap kube-proxy -o yaml | grep -n "mode:"

# 2. The logs
kubectl -n kube-system logs ds/kube-proxy | grep -i proxier

# 3. Ask the running process, from a shell on the node
curl http://localhost:10249/proxyMode

Port 10249 is kube-proxy's metrics port, bound to localhost by default. Port 10256 is its health check: curl http://localhost:10256/healthz.

Example · See the rules for one Service

The Service web in namespace shop has ClusterIP 10.96.0.20 and three Pods. On any node (use ssh), in iptables mode:

sudo iptables-save | grep shop/web
-A KUBE-SERVICES -d 10.96.0.20/32 -p tcp -m comment --comment "shop/web cluster IP" -m tcp --dport 80 -j KUBE-SVC-OI3ES3UZPSOHBDZQ
-A KUBE-SEP-EQTZ7ECNTNFMRWQG -p tcp -m comment --comment "shop/web" -m tcp -j DNAT --to-destination 10.244.1.7:8080
-A KUBE-SEP-LB2HZ4QWZ6YTDKN3 -p tcp -m comment --comment "shop/web" -m tcp -j DNAT --to-destination 10.244.1.9:8080
-A KUBE-SEP-V5MOCTPKXBXQLH2A -p tcp -m comment --comment "shop/web" -m tcp -j DNAT --to-destination 10.244.2.4:8080

Every rule carries the Service's namespace/name as a comment, which is why the grep works. One KUBE-SERVICES rule and one DNAT rule per ready Pod means kube-proxy has done its job on this node. In the other modes, look with sudo nft list table ip kube-proxy (nftables) or sudo ipvsadm -Ln (IPVS).

Example · Repair a kube-proxy that will not start

Pods on node01 cannot reach any Service, not even DNS. The other nodes are fine.

kubectl -n kube-system get pods -l k8s-app=kube-proxy -o wide
NAME               READY   STATUS             RESTARTS      NODE
kube-proxy-4xk2p   0/1     CrashLoopBackOff   5 (40s ago)   controlplane
kube-proxy-9d7mz   0/1     CrashLoopBackOff   5 (35s ago)   node01

All the Pods are failing, so the cause is shared: the DaemonSet or the ConfigMap, not one node. (The other nodes still work only because their old rules are still in the kernel.) The logs say why:

kubectl -n kube-system logs kube-proxy-9d7mz
# ... failed to read config file "/var/lib/kube-proxy/configuration.conf": no such file or directory

The ConfigMap's key is config.conf, so the path in the DaemonSet's command is wrong. Fix it there:

kubectl -n kube-system edit ds kube-proxy
#   - --config=/var/lib/kube-proxy/config.conf
kubectl -n kube-system rollout status ds kube-proxy

Editing the DaemonSet's Pod template starts a rollout by itself. Finish by testing a Service from a Pod on node01.

Example · Change the proxy mode

Set the mode in the ConfigMap, then restart the Pods so that they read it:

kubectl -n kube-system edit configmap kube-proxy        # in config.conf, set   mode: "nftables"
kubectl -n kube-system rollout restart ds kube-proxy
kubectl -n kube-system rollout status ds kube-proxy
kubectl -n kube-system logs ds/kube-proxy | grep -i proxier

A ConfigMap change alone does nothing: kube-proxy reads its configuration once, when it starts.

Tricks

Common mistakes

  • Editing the ConfigMap and stopping there. The running Pods keep the old configuration until kubectl -n kube-system rollout restart ds kube-proxy.
  • Thinking traffic flows through kube-proxy. It does not, so kube-proxy's CPU and memory are not a bottleneck for your application, and restarting it does not drop connections.
  • Expecting it to connect Pods. Pod-to-Pod connectivity by Pod IP is the network plugin's job. If Pods cannot reach each other directly by IP, kube-proxy is not the cause.
  • Confusing it with kubectl proxy. That command opens a local tunnel to the API server from your workstation.
  • Looking for a listening node port. ss -tlnp shows nothing on 30080 even when the NodePort works. Look at the rules, or just curl the port.
  • Flushing iptables to "reset" a node. iptables -F removes kube-proxy's rules along with everything else. kube-proxy writes them again on its next sync, but until then Services on that node are down.

Recap

  • One kube-proxy runs on every node and watches Services and EndpointSlices in the API server.
  • It writes kernel rules; the kernel on the client's node redirects each new connection to a ready Pod.
  • Modes: iptables (default), nftables (the successor), ipvs (deprecated). ClusterIP and NodePort both rely on these rules.
  • On kubeadm: DaemonSet kube-proxy plus ConfigMap kube-proxy in kube-system. Restart the DaemonSet after a configuration change.
  • Debug in order: endpoints, kube-proxy Pod on the node, its logs, then the rules for the Service.

Official docs

Quiz: check what you learned

5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.

Sign in to take the quiz and keep your score.

Practise: try it on a real cluster

A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.

  • A Service created today has endpoints and a cluster IP, yet nothing answers on it, while older Services work. Find the missing piece between the Service and the node's packet rules.

    Fix · Level 2 · standard · target 15 min

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.