Network Policies: Segmentation and Least-Privilege Pod Communication
Kubernetes network policies: default deny, allow rules, DNS egress, the namespaceSelector trap, CNI support and how to test them.

An attacker exploits a vulnerability in a small internal reporting service. From that pod, they can reach the payments database, the internal admin API, the metadata endpoints of every other service and the monitoring stack, because nothing in the cluster says they cannot. The reporting service only ever needed to talk to one read replica. The flat network turned a minor compromise into a major one.
A NetworkPolicy is a namespaced Kubernetes object that controls traffic at the IP and port level (OSI layers 3 and 4) for the pods it selects. Kubernetes defines the API; your CNI plugin enforces it. This is the cluster-scale version of the internal networks and segmentation shown in Docker networking.
Check that something enforces them
Creating a NetworkPolicy on a cluster whose network plugin does not support policies does nothing, silently. Calico, Cilium and Antrea enforce them; plain Flannel does not. On managed services, check the setting: GKE enforces policies with Dataplane V2 or the Calico option, AKS with Azure or Calico network policy, and Amazon EKS added native policy enforcement in its VPC CNI in August 2023, which must be enabled. Test with a deny-all policy before relying on anything more subtle.
How policies work
- Policies are allow lists. There are no deny rules.
- A pod not selected by any policy allows all traffic.
- Once any policy selects a pod for
Ingress, only the ingress traffic allowed by some policy reaches it. The same applies independently toEgress. - Multiple policies are additive: traffic is allowed if any policy allows it.
- Policies are stateful: reply traffic for an allowed connection is always allowed.
Step 1: default deny
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny
namespace: billing
spec:
podSelector: {} # every pod in the namespace
policyTypes:
- Ingress
- Egress
With no rules, this blocks all traffic to and from every pod in the namespace. Apply it in a test environment first, because the next thing that breaks is DNS.
Step 2: allow DNS
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
namespace: billing
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
Every pod needs to resolve names, and with egress denied, lookups time out and applications fail in confusing ways. The kubernetes.io/metadata.name label is added to every namespace automatically, which makes it a reliable way to select a namespace by name. Check your cluster’s DNS pod labels; CoreDNS usually keeps the k8s-app: kube-dns label for compatibility.
Step 3: allow each real dependency
# The API accepts traffic only from the ingress controller
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-ingress
namespace: billing
spec:
podSelector:
matchLabels:
app: billing-api
policyTypes: [Ingress]
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: ingress-nginx
ports:
- port: 8080
---
# The API may reach Postgres and an external payment provider
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-egress
namespace: billing
spec:
podSelector:
matchLabels:
app: billing-api
policyTypes: [Egress]
egress:
- to:
- podSelector:
matchLabels:
app: postgres
ports:
- port: 5432
- to:
- ipBlock:
cidr: 0.0.0.0/0
except: ["10.0.0.0/8", "169.254.169.254/32"]
ports:
- port: 443
---
# Postgres accepts traffic only from the API
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: postgres-ingress
namespace: billing
spec:
podSelector:
matchLabels:
app: postgres
policyTypes: [Ingress]
ingress:
- from:
- podSelector:
matchLabels:
app: billing-api
ports:
- port: 5432
The ipBlock rule allows HTTPS to the internet while excluding internal ranges and the cloud metadata endpoint at 169.254.169.254, which attackers often use to steal node credentials. NetworkPolicies cannot match domain names, so allowing “only api.stripe.com” needs a CNI with DNS-aware policies, such as Cilium’s or Calico’s extended policies, or an egress proxy.
The AND vs OR trap
This is the most common NetworkPolicy mistake, and it comes down to one dash:
# AND: pods labeled role=client IN namespaces labeled team=payments
ingress:
- from:
- namespaceSelector:
matchLabels: { team: payments }
podSelector:
matchLabels: { role: client }
# OR: ANY pod in team=payments namespaces, OR pods labeled role=client in THIS namespace
ingress:
- from:
- namespaceSelector:
matchLabels: { team: payments }
- podSelector:
matchLabels: { role: client }
In the first, both selectors are in one list item, so both must match. In the second, the dash before podSelector starts a separate item, so either matches. The second form usually allows far more than intended. Read every from and to block carefully, and note that a podSelector on its own only matches pods in the policy’s own namespace.
Testing policies
# From a temporary pod with the same labels as the client
kubectl run test -n billing --rm -it --labels app=billing-api --image=nicolaka/netshoot -- \
sh -c 'nc -zv postgres 5432; nc -zv billing-api 8080; nslookup google.com'
kubectl describe networkpolicy -n billing
Test both what should work and what should not. Tools such as Cilium’s network policy editor and Calico’s policy visualizations help reason about large sets of rules, and Cilium’s Hubble shows dropped flows live, which is invaluable when an application suddenly cannot reach something after a policy change.
Limitations
- Layers 3 and 4 only. Standard policies cannot allow
GET /ordersbut denyDELETE /orders. Layer 7 rules need a service mesh or Cilium’s extended policies. - No explicit deny or priority. Cluster administrators cannot set rules that namespace owners cannot override. The AdminNetworkPolicy API is being developed in the Kubernetes network policy working group to address this, and CNI-specific global policies exist today.
- No logging in the standard API. Visibility into allowed and denied flows comes from the CNI’s own tooling.
Frequently Asked Questions
Can Kubernetes pods talk to each other by default?
Yes. Without NetworkPolicies, every pod can reach every other pod in every namespace on any port. Policies restrict traffic only for the pods they select.
Why are my NetworkPolicies not working?
Most often the CNI plugin does not enforce them, or enforcement is not enabled on your managed cluster. Other causes are selectors that do not match pod labels and namespace selectors using labels the namespaces do not have.
Why did DNS break after adding a default-deny policy?
Denying all egress also blocks DNS queries to CoreDNS in kube-system. Add a policy allowing egress to the DNS pods on UDP and TCP port 53.
Can NetworkPolicies filter by domain name or HTTP path?
Not with the standard API, which works on IP addresses, namespaces, pod labels and ports. DNS-aware and HTTP-aware policies are available through CNI extensions such as Cilium or Calico, or through a service mesh.
Default deny, then allow what is real
A flat cluster network turns any compromise into access to everything. Start each namespace with default deny, allow DNS, then add one policy per real dependency, and test both directions. It is tedious the first time and nearly free afterwards, and it is the network half of least privilege; the other half, controlling who can change things in the cluster, is RBAC. The official documentation is worth keeping open: the NetworkPolicy documentation and the Network Policy Editor. Related reading on this site: Kubernetes Services and Pod Security Standards.
Originally posted on 25 June 2024 and post last modified on 25 June 2024