Use it to segment workloads, or when traffic is blocked after policies are added.
NetworkPolicy
NetworkPolicy restricts pod traffic by labels, namespaces and ports. Once a pod is selected by a policy for a direction, only allowed traffic passes; enforcement requires a CNI that supports it.
1. kubectl get networkpolicy -n <ns>.
2. Which policies select the pod? (podSelector).
3. DNS egress allowed?
4. CNI enforces policies?
5. Test with a debug pod.
pod selected → ingress/egress rules → allowed peers/ports
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Apply a default deny, then add allows one at a time and test each. Confirm the CNI plugin supports NetworkPolicy. Check namespace labels used in namespaceSelector.
Step 2 Common failures Narrow the fault
Forgetting DNS egress makes every connection fail with name errors. Namespace selectors match labels, not names, unless kubernetes.io/metadata.name is used. Policies have no effect with a CNI that does not implement them.
Start with default-deny per namespace and allow explicitly — including DNS egress to CoreDNS. Policies are additive (union of allows).
kubectl get networkpolicy -n <ns>— first check for this topickubectl get pods -o wide— pods, status, node, IPkubectl describe pod <pod>— events and conditionskubectl logs <pod> [-c <c>] [--previous]— container logs (incl. previous crash)kubectl get events --sort-by=.lastTimestamp— recent cluster events
ReferenceWhat it is1 section · depth on demand
How it works
The CNI plugin enforces policies by translating selectors into node-level filters. Ingress and egress are evaluated separately; traffic must be allowed by egress on the source and ingress on the destination when both are restricted.
ReferenceImportant terms & boundaries3 sections · depth on demand
Default-deny example
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: default-deny, namespace: app }
spec:
podSelector: {}
policyTypes: [Ingress, Egress]
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: allow-web-to-api, namespace: app }
spec:
podSelector: { matchLabels: { app: api } }
ingress: [{ from: [{ podSelector: { matchLabels: { app: web } } }], ports: [{ port: 8000 }] }]
Documented example, not captured output. Remember to allow DNS egress (UDP/TCP 53 to kube-system). Policies only work if the CNI enforces them.
In depth
The three rules that explain most NetworkPolicy behaviour
1. A Pod is unrestricted until at least one policy selects it for a direction (policyTypes: Ingress / Egress).
2. Once selected, only traffic that some policy allows is permitted; policies are additive (a union of allows), there are no deny rules.
3. A connection must be allowed by egress on the source and ingress on the destination; replies to an allowed connection are allowed automatically.
Selector combinations — one entry vs two entries
ingress:
- from:
- namespaceSelector: { matchLabels: { team: web } }
podSelector: { matchLabels: { app: frontend } } # AND: frontend Pods in team=web namespaces
ingress:
- from:
- namespaceSelector: { matchLabels: { team: web } }
- podSelector: { matchLabels: { app: frontend } } # OR: anything in team=web, or frontend here
The single dash makes the difference; it is the most common reason a policy allows more than intended.
Allowing DNS after a default deny
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 }]
Testing
Test each allowed path and one forbidden path from a throwaway Pod (kubectl run --rm -it --image=busybox), and remember ipBlock rules see the address after any SNAT — traffic through a load balancer may arrive from node IPs, not client IPs.
Common mistakes
- Assuming NetworkPolicy works without a CNI that enforces it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.