Default Open, Then Lock Down
Default Open, Then Lock Down
By default every pod can reach every other pod — the flat network model with no firewall. NetworkPolicy objects flip that for selected pods: once a pod is selected by any policy, traffic not explicitly allowed is dropped (for the directions covered by policyTypes).
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: api-allow }
spec:
podSelector: { matchLabels: { app: api } }
policyTypes: [Ingress]
ingress:
- from:
- podSelector: { matchLabels: { app: web } }
ports:
- { protocol: TCP, port: 8080 }
The example allows only web pods to reach api on 8080. Everything else touching api's ingress is denied — including other namespaces unless you add namespaceSelector.
Selectors and Directions
Selectors and Directions
Policy combinations pack the real meaning:
podSelectoralone — pods in the policy's own namespace.namespaceSelector— pods in matched namespaces (any node).namespaceSelector+podSelectorin one peer — pods matching both.ipBlock— CIDR ranges, for external destinations or exceptions.
policyTypes must include Egress before egress rules apply — listing only Ingress leaves outbound traffic untouched:
policyTypes: [Ingress, Egress]
egress:
- to:
- namespaceSelector: { matchLabels: { env: prod } }
Empty ingress: [] with the pod selected means deny all ingress. DNS needs an explicit egress rule to port 53 or resolution breaks cluster-wide.
Pitfalls and Interview Angles
Pitfalls and Interview Angles
Pitfall 1 — assuming policies apply without a supporting CNI. Enforcement requires a plugin that implements them (Calico, Cilium, and most modern CNIs do; basic Flannel does not). Objects apply cleanly and silently do nothing.
Pitfall 2 — breaking DNS or health checks. Locking egress without allowing UDP/TCP 53 to kube-system makes pods fail in confusing ways; allowing health probes from the node (ipBlock or namespace selectors matching kubelet source) avoids false positives.
Interview-ready: state "selected = restricted; unselected = unrestricted," describe default-deny as a baseline posture (select everything, empty rules), and give your verification test — run a client pod and wget the target before and after applying the policy.
Quiz
1. What happens to a pod selected by a NetworkPolicy with empty ingress rules?
2. Which CNI capability is required for NetworkPolicy to have effect?
3. Why do pods stop resolving DNS after a strict egress policy?
Flashcards
Question
Default posture
Click to reveal answer
Answer
Without policies, all pod traffic is allowed; selecting a pod with any policy restricts it.
Question
policyTypes
Click to reveal answer
Answer
Ingress and/or Egress — egress rules are inert unless Egress is listed.
Question
Peer selector forms
Click to reveal answer
Answer
podSelector (same ns), namespaceSelector, both combined, ipBlock for CIDRs.
Question
Deny-all baseline
Click to reveal answer
Answer
podSelector: {} with policyTypes and empty rule lists = deny everything in those directions.
Revision Notes
Key Takeaways
- 1. Policies restrict selected pods; everything else stays open
- 2. Direction matters: declare Egress before writing egress rules
- 3. Enforcement is a CNI feature, not an API-server feature
Interview Tips
- • Describe building default-deny then opening minimum paths
- • Always mention allowing DNS (53) when tightening egress
Cheat Sheet
Selected = restricted; unselected = open. policyTypes must list the direction. Peers: podSelector/namespaceSelector/ipBlock. Needs policy-aware CNI; allow DNS egress.