hostNetwork and hostPort
Some Pods, like kube-proxy, answer right on their Node address via the field hostNetwork or hostPort. How does each field do it, what does the Pod give up, and why can no second copy run on that Node?
Play the animation 4 steps, animated, plays on a computer or a tablet
hostNetwork and hostPort, step by step
-
With hostNetwork true the Pod gets no network namespace of its own. It runs inside the Node network namespace, so there is no veth, no Pod IP of its own and no bridge in the path: the container binds straight to the Node interfaces. A client that dials 192.168.1.20:80 is served by the Pod with no NAT anywhere. That is how kube-proxy, the CNI agent and node-exporter run.
Play from step 1 -
The price is the Node port space and the isolation. The container listens on the Node itself, so a second Pod that wants the same port cannot run here, and the Pod sees every Node interface with nothing of its own between it and the host. That is a privilege for the agents that must see the Node, not for applications.
Play from step 2 -
The hostPort field is the smaller hammer. The Pod keeps its own namespace, its Pod IP and its veth, and the CNI portmap plugin only adds a port mapping on the Node. Anything arriving at 192.168.1.20:8080 is DNAT-ed to 10.244.1.5:80 and then delivered down the ordinary bridge and veth. The Pod is reachable from the LAN, and its socket still only ever sees its own address and port.
Play from step 3 -
Both fields spend the same scarce thing, a port on the Node, so the Scheduler counts a hostPort as a Node resource and only one replica of that Pod can land here. The difference is what you give up: hostNetwork hands the Node network namespace to the container and suits the agents that must see it, while hostPort keeps the Pod isolated and punches a single port through to it. Everything else belongs behind a Service.
Play from step 4