Kubernetes NetworkPolicy Guide: Connectivity Testing, Ingress Rules, and Safe Policy Rollouts
KubernetesNetwork SecurityNetworkPolicyContainersPlatform EngineeringTroubleshooting

Kubernetes NetworkPolicy Guide: Connectivity Testing, Ingress Rules, and Safe Policy Rollouts

DDetails Cloud Editorial Team
2026-11-21
7 min read

A practical Kubernetes NetworkPolicy checklist for ingress, egress, DNS, selectors, connectivity tests, troubleshooting, and safe rollouts.

Kubernetes NetworkPolicy can improve workload isolation, but a small selector or port mistake can make a healthy service appear broken. This guide provides a reusable checklist for designing ingress and egress rules, testing connectivity safely, diagnosing blocked traffic, and rolling out policy changes without exposing workloads unnecessarily.

Overview

A Kubernetes NetworkPolicy describes which traffic is allowed to or from selected pods. The policy object is part of the Kubernetes API, but enforcement is performed by the cluster’s network plugin or CNI. As a result, the exact capabilities and diagnostic behavior can differ between environments. Before troubleshooting a rule, confirm which CNI is installed, which NetworkPolicy features it supports, and whether additional controls such as service-mesh authorization, cloud security groups, or host firewalls are also involved.

NetworkPolicy rules are directional. Ingress controls traffic arriving at selected pods, while egress controls traffic leaving them. A policy can select pods for one direction, the other direction, or both. Policies generally add allowed sources or destinations; adding a second policy does not normally override an existing restriction with a broader rule. This makes policy composition useful, but it also means that a troubleshooting change should be reviewed across every policy selecting the affected pod.

AreaIngressEgress
Traffic directionClient, service, or peer to the selected podSelected pod to a service, peer, DNS server, or external destination
Typical questionsIs the caller selected? Is the namespace allowed? Is the destination port correct?Can the pod resolve names? Is the destination allowed? Is the protocol and port correct?
Common failureRequests reach the node or service but not the application podApplication starts but cannot reach DNS, a database, an API, or another dependency
Useful evidenceSource pod labels, source namespace, destination port, and service endpointsDNS configuration, destination address, egress rule, and CNI enforcement logs

Start with a clear traffic map: source workload, source namespace, destination workload or address, protocol, port, and expected reason for access. Do not begin by adding a broad allow rule. A precise map makes it easier to write a narrow policy and prove that it works.

Checklist by scenario

Before writing or changing a policy

  • Record the namespace and labels of the pods that should be protected.
  • Confirm the application’s listening port inside the pod, the Service port, and the target port. These values can be different.
  • List required callers, such as an ingress controller, frontend workload, batch job, monitoring agent, or administrative tool.
  • List required outbound dependencies, including DNS, databases, queues, identity endpoints, object storage, and external APIs.
  • Identify whether callers and dependencies are in the same namespace, another namespace, or outside the cluster.
  • Check whether the workload has multiple policies already selecting it.
  • Confirm the CNI implementation and its documented support for the policy features you plan to use.

For namespace and workload isolation

A common design is to allow a workload only from pods with a specific label in a specific namespace. Namespace and pod selectors have important scope. A namespace selector selects namespaces by namespace labels; it does not select pods by their namespace name. A pod selector selects pods, usually within the policy’s namespace unless it is combined with a namespace selector.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-frontend-to-api
  namespace: app
spec:
  podSelector:
    matchLabels:
      app: api
  policyTypes:
    - Ingress
  ingress:
    - from:
        - namespaceSelector:
            matchLabels:
              purpose: application
          podSelector:
            matchLabels:
              app: frontend
      ports:
        - protocol: TCP
          port: 8080

Use a separate, explicitly named policy for each meaningful relationship when that improves reviewability. Verify that the expected namespace label exists and that the calling pod actually has the expected label. A selector that matches zero pods can look syntactically correct while allowing no traffic.

For default-deny preparation

Before tightening a namespace, inventory traffic rather than guessing. A baseline policy can isolate ingress and egress, but it may also block DNS and health-related dependencies. Apply changes in a test namespace or controlled workload first, and make sure there is a tested recovery path.

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

This policy selects every pod in the namespace and declares both directions isolated. It is a starting point, not a complete application policy. Add narrowly scoped allow rules for required traffic, including DNS where the application uses service names.

For DNS-dependent egress

Many applications need access to the cluster DNS service before they can reach any named dependency. Find the DNS service and its backing pods in the cluster rather than assuming a fixed address. Then allow the protocols and port used by your environment. DNS commonly uses UDP and may use TCP, so test both if your resolver path requires them.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-dns
  namespace: app
spec:
  podSelector: {}
  policyTypes:
    - Egress
  egress:
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: kube-system
          podSelector:
            matchLabels:
              k8s-app: kube-dns
      ports:
        - protocol: UDP
          port: 53
        - protocol: TCP
          port: 53

Label keys and DNS pod labels vary by installation. Inspect them with kubectl before using this as a template. If the CNI does not support the selector combination or the DNS pods use different labels, adapt the rule to the documented environment.

For temporary connectivity testing

  1. Inspect the selected pods and labels: kubectl get pods -n app --show-labels.
  2. Inspect namespace labels: kubectl get ns --show-labels.
  3. List policies and their YAML: kubectl get networkpolicy -A and kubectl describe networkpolicy -n app <name>.
  4. Run a short-lived diagnostic pod in the same namespace and, where possible, with the same service account or security context as the caller.
  5. Test DNS resolution, the destination port, and the application protocol separately. For example, distinguish a failed name lookup from a refused connection or an application-level error.
  6. Delete the diagnostic pod when testing ends and record the image, commands, source namespace, and results.

Use approved, minimal diagnostic images and do not place credentials or production tokens in command arguments. A connectivity test should validate a hypothesis, not become an undocumented permanent workload.

What to double-check

  • Selectors: Confirm exact spelling, label values, namespace scope, and whether a selector matches the intended pods at rollout time.
  • Ports: Check the application container port, Service port, and target port. NetworkPolicy evaluates traffic at the pod network boundary, so testing the Service port alone can hide a mismatch.
  • Protocol: TCP and UDP are distinct. A rule for one does not automatically authorize the other.
  • Service behavior: Confirm that the Service has endpoints and that traffic is reaching the expected pods. NetworkPolicy does not repair a selector or readiness problem.
  • Return traffic: Check the policy direction and the CNI’s documented behavior for connection tracking. Avoid assuming that an ingress rule also authorizes unrelated egress.
  • External destinations: An IP-based rule, a domain-based requirement, and a cloud-provider endpoint may be handled differently. Confirm what the installed implementation supports.
  • Other controls: Review service-mesh policies, cloud firewalls, security groups, node firewall rules, and application authentication separately from NetworkPolicy.
  • Policy status: A policy object being accepted by the API does not prove that the intended traffic path is allowed. Test from the actual source to the actual destination.

Common mistakes

Using a namespace name as a pod selector. Namespace names and labels are different inputs. If you need both conditions, use a namespace selector and pod selector with deliberate scope.

Allowing ingress but forgetting egress. A server may accept inbound traffic while its response path, DNS lookup, database connection, or identity request is blocked by egress isolation.

Testing from the wrong place. A test from an administrator’s workstation does not prove that the production caller is allowed. Test from a pod with the relevant namespace and labels.

Changing several variables at once. Do not simultaneously change labels, Services, ports, and policies unless an emergency requires it. Make one controlled change, test it, and preserve the before-and-after manifests.

Assuming every CNI behaves identically. Kubernetes defines the policy API, while enforcement details and supported extensions depend on the network plugin. Check implementation documentation and use plugin-specific observability when available.

Leaving broad temporary access in place. A namespace-wide allow rule may restore service quickly, but it can become a security gap. Time-box emergency policies, annotate the reason, and create a removal task before applying them.

When to revisit

Revisit NetworkPolicy whenever a workload, namespace, label, Service port, DNS path, CNI, or external dependency changes. Include policy validation in deployment reviews and platform upgrade readiness work. A Kubernetes or CNI upgrade is a useful trigger to confirm supported features, enforcement behavior, logging, and test procedures rather than assuming that existing manifests tell the whole story.

Before a seasonal planning cycle or security review, run this short checklist:

  1. Export and review all policies by namespace.
  2. Identify policies selecting no pods and policies selecting more pods than intended.
  3. Test critical ingress paths, egress dependencies, and DNS from representative workloads.
  4. Compare observed traffic with documented application dependencies.
  5. Remove temporary exceptions and update policy ownership and rollback notes.
  6. Repeat tests after label, namespace, CNI, or workflow changes.

The safest rollout is incremental: define the intended traffic, validate selectors, apply the narrowest rule, test from the real workload path, observe failures, and document the result. Treat NetworkPolicy as part of the application’s reliability contract, not just a final security control.

Related Topics

#Kubernetes#Network Security#NetworkPolicy#Containers#Platform Engineering#Troubleshooting
D

Details Cloud Editorial Team

Cloud Security and Reliability Editors

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.