Kubernetes NetworkPolicy: A Safe Default-Deny Rollout Guide

Kubernetes pods can usually communicate freely unless the cluster has policies that isolate them. That default is convenient during development, but it leaves lateral movement largely unconstrained after a workload is compromised. A NetworkPolicy can limit which peers may connect to selected pods and which destinations those pods may reach. A sound rollout starts with a traffic map and an implementation check, not a cluster-wide deny manifest applied on Friday afternoon.

NetworkPolicy rules operate at layer 3 and layer 4. They are useful for controlling pod and namespace selectors, IP ranges, and ports. They are not a complete service-mesh policy system: the standard API does not express TLS identity, hostname-based destinations, or application paths. Treat the policy as one layer in a broader segmentation design.

Confirm the network plugin enforces policies

Figure: Kubernetes NetworkPolicy: A Safe Default-Deny Rollout Guide
Figure: Kubernetes NetworkPolicy: A Safe Default-Deny Rollout Guide

Creating a NetworkPolicy object is not proof that traffic is filtered. Enforcement depends on the cluster network plugin implementing the NetworkPolicy API. Verify the actual CNI and vendor behavior in each cluster, including any limitations around host-network pods, service address translation, and egress. Use a disposable namespace and test allowed and denied connections from real pods before treating policies as a control.

The standard model has separate ingress and egress isolation. A policy selecting a pod for ingress makes only explicitly allowed ingress flows available; another policy selecting it for egress controls outbound flows. Applicable policies are additive: they combine their allowed traffic. A broad allow rule in one policy can therefore defeat the intended narrowing of another policy. Review the full set of policies and selected labels, not one YAML file in isolation.

Inventory flows before writing rules

Map the dependencies for each application: ingress controller to frontend, frontend to API, API to database, workloads to DNS, metrics and logging agents, identity providers, and external services. Distinguish required connections from merely observed traffic. Flow logs can reveal behavior, but they may include retries, debugging probes, or old dependencies that should not become permanent allowances.

Document namespace and pod labels as security-relevant selectors. A typo or overly broad selector can leave a tier unprotected, while labels that application teams can freely change may let them evade intended boundaries. Protect critical labels through deployment review or admission policy. Use namespace selectors carefully: a selector that matches all namespaces can be just as broad as allowing every pod.

Introduce deny policies gradually

Start with one low-risk namespace or service. Capture the baseline, create a policy in audit or staging, and compare observed traffic with the intended dependency map. Apply ingress and egress isolation separately where possible so that failures are attributable. Add the smallest explicit allowances needed, validate readiness checks and background jobs, and monitor blocked flows during rollout. Expand only after owners confirm application behavior.

A default-deny ingress policy for all pods in one namespace has this shape:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-ingress
  namespace: payments
spec:
  podSelector: {}
  policyTypes:
    - Ingress

To isolate egress too, add Egress to policyTypes and create explicit egress allowances. A deny-all egress policy can block DNS resolution, so add a narrow rule for the actual cluster DNS pods and ports. DNS service labels and ports vary by cluster; discover them rather than copying a guessed selector from another environment.

Scope each allowance to the intended service

An API tier that only needs a database connection should not receive a broad to: {} egress rule. Select the database pods or a carefully governed namespace and limit the port and protocol. Similarly, allow ingress to a database from the API workload, not from the entire application namespace if other pods share it. Make labels stable and meaningful so rules describe trust relationships clearly.

Remember that NetworkPolicy uses allow rules; policies are not ordered firewall ACLs with an explicit deny that overrides an allow. If any matching policy permits a flow in a direction, that flow is allowed, subject to isolation at the other endpoint. This means review must include the union of all applicable rules and both the source egress and destination ingress sides.

Test the behavior from both sides

Use short-lived test pods with the same labels as the application to validate selected traffic paths. Confirm a frontend can reach its API on the expected port, an unrelated namespace cannot, and the API can reach its database but not arbitrary destinations. Test DNS by querying the cluster's resolver, and test external dependencies through the actual egress path. Keep these checks in deployment validation so label or policy changes do not silently expand access.

Observe application health after each rollout. A blocked dependency can present as timeouts, degraded readiness, or repeated retries rather than an obvious “policy denied” error. Correlate network-plugin telemetry, pod events, DNS logs, and application traces. Roll back a policy change only through a controlled process; a temporary broad allow-all rule can become a forgotten permanent exception.

Know what the API does not cover

Standard NetworkPolicy does not provide hostname allowlists, TLS inspection, or detailed HTTP method and path decisions. IP-based egress rules can be brittle when third-party addresses change. Use an egress gateway, proxy, service mesh, or a CNI-specific extension when you need those controls, and make clear which behavior is portable Kubernetes API versus vendor capability. The policy also does not replace cloud firewall rules, node hardening, workload identity controls, or application authentication.

Operational checklist

Before enforcement, confirm policy support in the CNI, list required flows, protect selector labels, and test both allowed and forbidden connections. Preserve DNS, health checks, telemetry, and required external calls with narrow rules. Track exceptions with an owner and review date. After deployment, monitor denies and application error rates, then revisit policies when dependencies change. A deny-by-default design is effective when it reflects real application architecture and is continuously tested, not when the number of policy objects increases.

References