Review whether workload host_port is needed

Configure node port mappings and access scope only when host_port is required.

Description

Omit host_port for ordinary workloads and use the Service or Ingress your clients need. A host_port binds directly on the Pod’s node and is separate from a Service’s NodePort.

Potential impact

The binding adds an access path and may prevent scheduling where the port is already reserved. A workload that needs direct access can also become unreachable if the mapping or firewall is wrong.

Remediation

When direct access is required, map the node port and protocol to the port where the application actually listens. Test from a client allowed by the node firewall and check for placement conflicts with other workloads.

Examples

Both examples require you to configure nginx to listen on 8080. Use a maintained image for deployment.

Before

hcl
resource "kubernetes_pod" "example" {
  metadata {
    name = "app"
  }

  spec {
    container {
      name  = "web"
      image = "nginx:1.7.9"

      port {
        container_port = 8080
      }
    }
  }
}

After

hcl
resource "kubernetes_pod" "example" {
  metadata {
    name = "app"
  }

  spec {
    container {
      name  = "web"
      image = "nginx:1.7.9"

      port {
        container_port = 8080
        host_port      = 8080
      }
    }
  }
}

The first container_port = 8080 only declares the container port. The second maps node port 8080 to container port 8080 for a workload that needs direct node access.

References