CKAD · 5 November 2026 · 5 min read
Kubernetes NetworkPolicies explained
How NetworkPolicies isolate Pods, why one dash changes AND to OR, and how to write default deny and DNS egress policies that work.
A Kubernetes NetworkPolicy controls which Pods may talk to which. All traffic is allowed until a policy selects a Pod; from then on that Pod only accepts (or sends) what some policy explicitly allows. Policies only allow, never deny, and they add up. NetworkPolicies appear in both the CKAD and the CKA, and they catch people out because the rules are short but the behavior is not obvious: a policy that looks right can block DNS, or allow far more than intended, because of one missing dash in the YAML.
TL;DR
- No policy selects a Pod: everything is allowed. A policy selects it: only what is allowed gets through, per direction.
- Default deny ingress:
podSelector: {}withpolicyTypes: [Ingress]and no rules. - One
fromitem with bothpodSelectorandnamespaceSelectormeans AND; two items (two dashes) mean OR. - Restricting egress? Allow DNS on port 53 (UDP and TCP), or names stop resolving.
- The CNI plugin enforces policies; without one that supports them, they do nothing.
- Exam: Services and Networking, 20% of both the CKAD and the CKA.
This article works through the model step by step, with policies you can apply and test.
Everything is allowed until a policy selects a Pod
By default, Kubernetes allows all traffic. Any Pod can connect to any other Pod in any namespace, and any Pod can connect out.
That changes Pod by Pod. When a NetworkPolicy's podSelector matches a Pod, and the policy covers a direction (Ingress, Egress or both), that Pod becomes isolated for that direction. From then on, only traffic that some policy explicitly allows is let through. Pods that no policy selects stay fully open.
Three consequences follow:
- Policies only ever allow. There is no deny rule in a NetworkPolicy. You deny by isolating a Pod and not allowing the traffic.
- Policies are additive. If two policies select the same Pod, the allowed traffic is the union of both. Order does not matter.
- Isolation is per direction. A policy with only
IngressinpolicyTypesdoes nothing to outgoing traffic.
Replies to an allowed connection are always permitted, so you do not need a matching egress rule for the response to an allowed ingress connection.
The CNI plugin must enforce them
The API server accepts any valid NetworkPolicy, but it is the network plugin (CNI) that enforces it. Plugins such as Calico and Cilium do. A plugin that does not support NetworkPolicies will accept the objects and silently ignore them, and every connection keeps working. If your policies seem to have no effect, check which CNI the cluster runs before you debug the YAML.
Default deny for ingress
The most common starting point is to isolate every Pod in a namespace for incoming traffic:
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: default-deny-ingress namespace: shopspec: podSelector: {} policyTypes: - IngressAn empty podSelector ({}) selects every Pod in the namespace. There are no ingress rules, so nothing is allowed in. Egress is untouched.
For a full lockdown in both directions, list both types:
policyTypes: - Ingress - EgressAlways set policyTypes explicitly. If you leave it out, Kubernetes assumes Ingress, and adds Egress only if the policy has egress rules, which is easy to misread.
Allowing traffic in
With the default deny in place, allow the web Pods to receive traffic on port 8080 from Pods labeled app=frontend in the same namespace:
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: web-from-frontend namespace: shopspec: podSelector: matchLabels: app: web policyTypes: - Ingress ingress: - from: - podSelector: matchLabels: app: frontend ports: - protocol: TCP port: 8080A podSelector inside from on its own matches Pods in the policy's own namespace only.
podSelector and namespaceSelector: AND or OR
To allow traffic from other namespaces you add a namespaceSelector. Every namespace carries the label kubernetes.io/metadata.name with its own name, so you can select a namespace by name without adding labels yourself.
How you write the selectors decides whether they are combined with AND or OR. This is one item in the from list, with both selectors in it:
ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: monitoring podSelector: matchLabels: app: prometheusBoth selectors belong to the same list item, so they are ANDed: only Pods labeled app=prometheus in the monitoring namespace are allowed.
Now add a single dash in front of podSelector:
ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: monitoring - podSelector: matchLabels: app: prometheusThese are two items, so they are ORed: any Pod in the monitoring namespace, or any Pod labeled app=prometheus in the policy's own namespace. That is a much wider rule, and the only visible difference is one character. When you read or write a policy, count the dashes under from and to.
kubectl describe networkpolicy prints the rules in a form that makes this easier to check.
Egress and DNS
When you isolate Pods for egress, they lose DNS. Almost every application looks up Service names, so the Pods appear broken even though the connection you meant to allow is in the policy. Allow DNS to the cluster DNS Pods on port 53, over both UDP and TCP (DNS falls back to TCP for large responses):
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: allow-dns-egress namespace: shopspec: 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: 53The k8s-app=kube-dns label is what CoreDNS carries in kubeadm clusters and many others, but check yours with kubectl get pods -n kube-system --show-labels. Then add further egress policies for the connections each application actually needs.
Testing a policy
Do not trust a policy until you have tested it from both an allowed and a blocked Pod. A throwaway Pod with a short timeout is enough:
kubectl -n shop run test --rm -it --image=busybox:1.36 --restart=Never \ --labels=app=frontend -- wget -qO- -T 2 http://web:8080kubectl -n shop run test --rm -it --image=busybox:1.36 --restart=Never \ -- wget -qO- -T 2 http://web:8080With the default deny and web-from-frontend policies in place, the first should succeed and the second should time out. If you have also isolated egress in the namespace, the test Pod needs an egress rule to web on port 8080 as well. If both succeed, check the CNI and the labels. If both fail, check DNS egress and the port number in the rule.
The Network Policies page on kubernetes.io has the full reference, including ipBlock rules and port ranges.
Keep going
NetworkPolicies are best learned by applying them and watching connections succeed or fail. The Gazeek CKAD course gives you a real cluster for each lab, with a CNI that enforces policies and guided tasks with checks. The free practice questions in the Gazeek Memory Gym are a quick way to drill the AND and OR rules.