NetworkPolicy Patterns
Allow a whole namespace, then restrict egress and watch the Pod lose DNS while still reaching the same backend by IP. That one result explains more about NetworkPolicy than any diagram, and the AND-versus-OR distinction in a from block is the bug behind most broken policies.
Services and Networking Guide 62 of 103 Advanced
- Kubernetes1.36.4
- Cluster4 nodes
- Runtimecontainerd 2.2.6
- CNICalico v3.32.1
- TimeAbout 40 min
The kube-dns pod label k8s-app: kube-dns is what the DNS allowance matches. Verify it on your cluster before copying.
- CoreDNSv1.14.2
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| CKA1001 | 192.168.0.175 | Ubuntu 26.04 LTS | Control Plane Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE01 | 192.168.0.176 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE02 | 192.168.0.177 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE03 | 192.168.0.178 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
This guide includes
Use this when a policy has closed more than you intended - which usually means DNS. This matters because restricting egress breaks name resolution before it breaks anything you were actually thinking about, and the fix turns on understanding AND versus OR inside a rule.
- allowing a whole namespace with
namespaceSelector, and seeing policies add up rather than override - restricting egress and watching the Pod lose DNS while still reaching a bare IP
- adding the DNS allowance, and seeing where a rule means AND and where it means OR
- verifying all the policies together at the end, which is the step people skip
Before you start
- The default-deny guide. This one continues from its Pods and policies.
- The CoreDNS guide, because the central lesson here is what happens when a Pod loses DNS.
- A CNI that enforces NetworkPolicy.
-
Allow a whole namespace, and see policies add up
-
Restrict egress, and break the Pod in an unexpected way
-
The DNS allowance, and AND versus OR
-
Everything back on, verified together