Skip to main content

More Info:

HostPorts bind container ports directly to the node, bypassing network policy and exposing services on the host. Restrict their use.

Risk Level

Medium

Address

Security

Compliance Standards

  • CIS Kubernetes

Triage and Remediation

Remediation

Manual Steps

  1. Identify current workloads using hostPort
    • Run on: any machine with kubectl access
    • Command:
    • Save/record this list as the baseline of current hostPort usage.
  2. Review each identified pod’s necessity for hostPort
    • For each NAMESPACE/POD from step 1:
    • Manually inspect:
      • Which containers/ports use hostPort and on which nodes.
      • Whether they front internet-facing or sensitive services.
      • Whether equivalent access could be provided via ClusterIP/LoadBalancer/Ingress instead of hostPort.
    • Decide for each: “required (documented justification)” or “can be removed/avoided”.
  3. Review and design Pod Security / admission policy per namespace
    • For each namespace that runs user workloads (and especially those with unjustified hostPort use):
      • If using built-in Pod Security Admission, check labels:
        • Assess whether the effective Pod Security level (e.g., baseline, restricted) and any custom admission policies already limit hostPort.
      • If using another admission controller (e.g., Kyverno, OPA Gatekeeper), list policies that affect hostPort in that namespace using your policy tool (no generic kubectl command exists; follow your policy system’s CLI/docs).
  4. Implement or tighten admission policy to restrict hostPort
    • Decide per namespace:
      • Namespaces where hostPort should be disallowed except for cluster-operator-approved workloads.
      • Namespaces where hostPort is temporarily allowed but must be minimized.
    • Apply or update your admission policy mechanism accordingly (Pod Security Admission level, Kyverno/Gatekeeper rules, or other). Document any namespace-specific exceptions and which service accounts / labels are allowed to use hostPort.
    • This step is policy- and environment-specific; there is no single kubectl command that enforces it generically.
  5. Refactor or remove non-essential hostPort usage
    • For pods marked “can be removed/avoided” in step 2:
      • Update their Deployment/DaemonSet/StatefulSet/Pod manifests (in Git/IaC or via kubectl edit if appropriate) to remove hostPort fields and use Services/Ingress instead.
      • Reapply manifests using your standard deployment process, for example:
    • Ensure any required firewall or ingress changes are made so functionality is preserved without hostPort.
  6. Verify minimized hostPort usage and effective restriction
    • Re-run the discovery from step 1:
    • Confirm that:
      • Only the few, explicitly justified workloads still appear.
      • Creating a test pod with hostPort in a restricted namespace is rejected by admission (use a small test manifest and kubectl apply -f test-hostport.yaml to confirm it is denied).
Problem indication:
Any line of output means that pod uses at least one hostPort. Each such pod must be manually reviewed to decide if hostPort is justified.

Problem indication:
Look for:
  • Unnecessary hostPort use (e.g., 80, 443, 22, or wide port ranges) where a Service/Ingress/LoadBalancer would suffice.
  • hostPort on multi-tenant or shared nodes.
    Any such cases are candidates for remediation.

Problem indication:
If you see NetworkPolicies that appear to “lock down” traffic but many workloads use hostPort, be aware that those policies do not protect the host-bound ports. This is an architectural risk to review.

Problem indication:
Namespaces appearing in “Namespaces with pods” but lacking any security/admission-related policy (and that also contain pods using hostPort from step 1/2) should be prioritized for adding policy to restrict hostPort usage.

Problem indication:
If a namespace is intended to be “locked down” or multi-tenant but this output shows many pods with hostPort, the risk is higher and manual remediation is recommended (redesign to use Services, adjust placement, or introduce admission policy).
How to interpret the output
  • Any line where HOST_PORT (for Pods or workloads) is a non-zero value indicates a potential policy violation that must be reviewed.
  • Pay particular attention to:
    • User/application namespaces (e.g., not kube-system, kube-public, kube-node-lease).
    • HostPorts exposed on broad or privileged workloads (e.g., internet-facing services, DaemonSets).
  • Namespaces with non-zero COUNT in the summary should have admission policies (e.g., Pod Security Admission or Admission Webhooks) evaluated or added to restrict or justify hostPort usage.
This script only reports; creating or tightening admission policies must be done manually based on your risk posture and any legitimate need for hostPorts.