Kubernetes Container Port Model
Kubernetes Container Port Model defines how containers expose ports, enabling communication between pods and external services.
Kubernetes Container Port Model is the detailed structure and semantics of a container's ports field, covering not just the basic act of declaring a listening port but the distinct roles of containerPort, hostPort, hostIP, protocol, and name within that declaration, and clarifying precisely what each does and does not affect regarding actual network accessibility.
containerPort as Pure Declaration
Documentation, Not Enforcement
containerPort states which port the container's process listens on inside its own network namespace, but this declaration has no enforcement effect on ordinary Pod networking — a container can listen on a port never declared here and still receive traffic normally, since nothing in the standard Pod network path consults this field to decide whether to allow or block traffic.
Why Declaration Still Matters
Despite carrying no enforcement weight, containerPort remains useful as structured documentation that tooling, dashboards, and Service definitions with named port references rely on, and it becomes functionally significant specifically when combined with hostPort or hostNetwork configuration, where the declared port takes on real operational consequence.
hostPort and Its Node-Level Binding
Reserving a Port on the Node Itself
When hostPort is set, the kubelet reserves that specific port directly on the node's own network interface and forwards traffic arriving there to the container's containerPort, meaning the Pod becomes reachable at the node's IP address on that port, entirely independent of the cluster's internal Pod networking.
The One-Pod-Per-Node-Per-Port Constraint
Because hostPort binds a port on the node itself, only one Pod using that specific hostPort can be scheduled to any given node at a time, since the node's own network stack cannot bind the same port twice; this constraint directly affects scheduling, effectively limiting how many replicas of a hostPort-using Pod can run on a single node regardless of that node's other available resources.
Why hostPort Is Used Sparingly
Because of this scheduling constraint and its exposure of the Pod directly on node-level networking (bypassing Service abstraction and load balancing entirely), hostPort is reserved for specific cases — certain DaemonSet-based infrastructure components needing a fixed, well-known node port — rather than general application usage, which is better served by Services.
hostIP for Interface-Specific Binding
Restricting Which Node Interface Is Used
hostIP, used together with hostPort, restricts the reservation to a specific network interface on the node rather than all interfaces, useful when a node has multiple network interfaces and the hostPort binding should apply only to a particular one, such as an internal management interface rather than a publicly reachable one.
protocol Selection
TCP, UDP, and SCTP Support
The protocol field, defaulting to TCP when omitted, can also be set to UDP or SCTP, and a container needing to expose the same numeric port for both TCP and UDP traffic must declare two separate port entries differing only in protocol, since a single ports entry covers exactly one protocol.
name as a Reference Anchor
Naming Ports for Service Reference
The optional name field gives a port a symbolic identifier that a Service's targetPort can reference instead of a bare numeric port, which is particularly useful when a Deployment's Pod template might use a different actual numeric port than what a Service's own numeric port suggests, or when multiple containers in a Pod expose ports that need to be distinguished by name rather than number alone.
Name Uniqueness Within a Pod
Because a Service can select a named port without knowing the exact number, port names must be unique across all containers within the same Pod, since an ambiguous or duplicated name would leave the Service's targetPort resolution unable to determine which specific container port was actually intended.