Review direct hostPort bindings

Configure a node-port binding and its access scope only when hostPort is needed.

Description

Use hostPort only when clients need a port directly on the node running the Pod. A Service of type NodePort is a separate way of providing access.

Potential impact

A node binding can expose the app wherever routing and firewalls permit. A conflicting address, port and protocol can prevent the Pod from being scheduled on a node.

Remediation

If direct access is unnecessary, remove hostPort and configure the required Service or Ingress with authentication and access controls. If you keep it, check the app’s listening port, node binding address and protocol, firewall rules and port conflicts.

Examples

The excerpts omit the separate Service or Ingress.

Before

yaml
apiVersion: v1
kind: Pod
metadata:
  name: web
spec:
  containers:
    - name: nginx
      image: nginx:1.25
      ports:
        - containerPort: 80
          hostPort: 8080

The first Pod maps node port 8080 to container port 80.

After

yaml
apiVersion: v1
kind: Pod
metadata:
  name: web
spec:
  containers:
    - name: nginx
      image: nginx:1.25
      ports:
        - containerPort: 80

With only containerPort: 80, the Pod reserves no node port. Roll out new Pods from the updated template or replace a standalone Pod, then test the intended service route.

References