Kubernetes and Cloud Native Associate (KCNA)Kubernetes FundamentalsHard
A junior administrator is examining a Kubernetes YAML file for a Pod and sees the following snippet: ```yaml apiVersion: v1 kind: Pod metadata: name: my-app-pod spec: containers: - name: my-app-container image: nginx:latest ports: - containerPort: 80 hostPort: 8080 ``` What is the primary effect of `hostPort: 8080` in this Pod definition?
- AIt maps the container's port 80 to port 8080 on the Node where the Pod is running.
- BIt creates a Kubernetes Service that exposes the Pod on port 8080.
- CIt specifies that the application inside the container should listen on port 8080.
- DIt ensures the container's port 80 is accessible only from within the Pod.
Show answer & explanationAnswer & explanation
Correct answer: A. It maps the container's port 80 to port 8080 on the Node where the Pod is running.
The `hostPort` field in a Pod's container definition specifies that the port on the host (Node) should be mapped to the `containerPort`. This means that traffic sent to port 8080 on the Node's IP address will be forwarded to port 80 inside the `my-app-container`.
Why the other options are wrong
- B. `hostPort` is a Pod-level configuration, not a Service definition. A Service object is required to abstract and expose Pods.
- C. The application inside the container listens on `containerPort: 80`; `hostPort` is a mapping from the Node, not the container's internal listening port.
- D. This statement is incorrect; `hostPort` is for external exposure, not internal access.
Kubernetes hostPort
A configuration in a Pod's container definition that maps a container port to a specific port on the host (Node) machine's network interface.
- Makes the container directly accessible via the Node's IP address and the specified `hostPort`.
- Can lead to port conflicts if multiple Pods on the same Node try to use the same `hostPort`.
- Generally discouraged in production in favor of Services (NodePort, LoadBalancer) or Ingress for better abstraction and load balancing.
Memory trick: `hostPort` is like giving your Pod a 'direct line' to the 'host's phone'.