liveBook · Manning

hostPort

As explained in Kubernetes in Action, Second Edition

A Kubernetes container port field that exposes a container port through a network port on the hosting node while the pod retains its own network.

hostPort forwards a port on the hosting node to a port in a container while the pod keeps its own network. The container listens on its container port; Kubernetes makes that process reachable through the selected host port. section 17.3.1

Use hostPort when a client needs to reach the daemon pod local to a particular node.

  • A request to a node's host port reaches only the agent pod on that same node. section 17.3.1
  • If no matching daemon pod runs on the node, connecting to that host port fails. section 17.3.1
  • Unlike a NodePort Service, hostPort does not forward to a random pod elsewhere in the cluster. section 17.3.1
  • A daemon pod can instead use hostNetwork, but that gives it the node's network environment and no separate pod IP; hostPort preserves the pod's own network. section 17.2.3 section 17.4

Forwarding port 11559 to a node agent

A DaemonSet can configure its node-agent container to listen on port 80 and expose that port through host port 11559. After the pods run on the nodes, request a node's address at port 11559, such as `curl 172.18.0.2:11559`. The response comes from the agent on that node. If a firewall blocks the address, access the node and request `curl localhost:11559`.

How Kubernetes in Action, Second Edition covers it

How deep it goes
Explains manifest configuration, packet routing, local placement, connectivity checks, and contrasts hostPort with NodePort and hostNetwork.
Examples
Uses a node-agent DaemonSet, port 80 inside the container, host port 11559, curl requests, and a kind worker node.
What it assumes
Assumes familiarity with pod manifests, container ports, DaemonSets, node addresses, and basic kubectl and curl usage.
What you can do after
Operate/configure a DaemonSet with hostPort and verify its local node connectivity using curl.
How it connects
Connects hostPort with DaemonSets, pods, container ports, node addresses, NodePort Services, hostNetwork, and the Downward API.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.