Home KubernetesKubernetes NetworkPolicies: Isolate Pods by Default

Kubernetes NetworkPolicies: Isolate Pods by Default

by Knowledge Base Linux
Kubernetes NetworkPolicies isolating pods with firewall walls, only allowed traffic passing between specific pods

By default, every pod in Kubernetes can talk to every other pod, regardless of namespace or purpose. That flat network is convenient for getting started and dangerous in production, because one compromised pod can reach databases, internal APIs, and other tenants. Kubernetes NetworkPolicies change that default: they are firewall rules for pods, allowing only the traffic you explicitly approve and denying the rest.

Kubernetes NetworkPolicies grouping pods by labels with a policy shield allowing only selected connections

This guide explains Kubernetes NetworkPolicies practically: how they work, how to write ingress and egress rules, how to test them without breaking your application, and the common pitfalls. For access control at the API level, see our Kubernetes RBAC guide, and for pod health while you change networking, our liveness and readiness probes guide.

Prerequisites

  • Access to a Kubernetes cluster with a network plugin that enforces NetworkPolicies, such as Calico, Cilium, or kube-ovn. The default kubenet setup does not enforce them.
  • kubectl configured, and a test namespace where you can break connectivity safely.
  • Basic familiarity with pods, labels, namespaces, and kubectl apply.

Kubernetes NetworkPolicies Basics: Default Deny

A NetworkPolicy selects pods by label and lists the traffic allowed to or from them. Anything not listed is denied, but only for the selected pods and only for the traffic types named in policyTypes. A policy that selects no pods does nothing, and a namespace with no policies allows all traffic, which is why the first policy most teams write is a default-deny policy for a namespace.

This default-deny policy selects every pod in the namespace and blocks all ingress and egress. Apply it only in a test namespace first, because it will break any pod that depends on outside traffic until you add explicit allow rules:

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

An empty podSelector means all pods in the namespace. With no ingress or egress rules listed, all traffic of both types is denied for those pods. DNS deserves special attention: pods need egress to the cluster DNS service on port 53, or name resolution fails even when the application policy looks correct.

Writing Ingress Rules for Kubernetes NetworkPolicies

Ingress rules allow traffic into selected pods from specific sources: pods with matching labels, namespaces with matching labels, IP blocks, or a combination. This policy allows the frontend pods to receive traffic only from the backend pods in the same namespace, on port 8080:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: frontend-ingress
  namespace: demo
spec:
  podSelector:
    matchLabels:
      app: frontend
  policyTypes:
    - Ingress
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: backend
      ports:
        - protocol: TCP
          port: 8080

Combine podSelector and namespaceSelector in one peer to allow traffic from pods with a label in namespaces with a label. Use ipBlock for traffic from outside the cluster, and always add an except range when you allow a broad CIDR but need to exclude a sensitive subnet.

Writing Egress Rules for Kubernetes NetworkPolicies

Egress rules control where selected pods can connect. This policy allows the backend pods to reach the database pods on port 5432 and to reach cluster DNS, and nothing else. Without the DNS egress rule, the backend would fail to resolve the database hostname even though the database rule is correct:

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

The exact DNS label depends on your cluster: check it with kubectl get pods -n kube-system before copying this example. For external APIs, allow egress to the specific IP blocks or use a fully qualified domain name feature if your network plugin supports it, because plain NetworkPolicies match IP addresses, not domain names.

Testing Kubernetes NetworkPolicies Safely

Test in a staging namespace with a copy of the workload, not in production. Apply policies one at a time, then test connectivity from a temporary debug pod in the same namespace using basic network tools. If traffic fails, check the policy selectors first: a label typo means the policy selects nothing, and a policy that selects nothing is silently useless.

kubectl run debug --rm -it --image=busybox -- /bin/sh
kubectl get networkpolicy -n demo
kubectl describe networkpolicy frontend-ingress -n demo

From the debug pod, test the service by name and port, and check DNS separately. If DNS fails, the problem is the egress DNS rule. If DNS works but the service fails, the problem is the ingress or egress application rule. Keep a rollback plan: deleting a NetworkPolicy immediately restores the previous behaviour for its selected pods, which makes policies safer to test than most network changes.

Troubleshooting Kubernetes NetworkPolicies

Nothing is blocked at all. Your network plugin may not enforce NetworkPolicies, or no policy selects the pods you are testing. Confirm enforcement with your plugin’s documentation, and check podSelector labels against the actual pod labels.

Everything is blocked after a default-deny policy. That is the policy working as written. Add allow rules for DNS, for the application’s dependencies, and for health probes, then retest step by step instead of removing the default-deny rule entirely.

Health probes fail. Kubernetes probes originate from the node, not from a pod, so they are not subject to NetworkPolicies in most plugins. If probes still fail, the cause is usually the application itself or a service misconfiguration, not the policy.

External traffic is blocked unexpectedly. Check whether egress is restricted and whether the destination IP changed. NetworkPolicies use IP blocks, so a cloud service that changes addresses can break an egress rule that worked yesterday.

Kubernetes NetworkPolicies Best Practices

Start with default-deny in each namespace, then add the smallest allow rules that make the application work. Label pods and namespaces consistently, because policies depend on labels. Keep policies in version control alongside the application manifests, review them when services change, and test them in staging with the same labels as production. Document the intended traffic flow for each namespace so the next person can tell an intentional block from a mistake.

Kubernetes NetworkPolicies turn a flat pod network into a segmented one, where a compromised pod cannot freely reach everything else. Start with default-deny in a test namespace, add ingress and egress rules deliberately, include DNS, and test each change. For the official reference, see the Kubernetes NetworkPolicy documentation.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.