Interview Prep/Kubernetes/Services, DNS, Ingress and NetworkPolicy

A NetworkPolicy meant to allow "frontend Pods in namespace web" accidentally allows every Pod in namespace web, or blocks everything. What's wrong with the YAML?

AdvancedTroubleshootingServices, DNS, Ingress and NetworkPolicy
Answer

Likely causes → The difference between one list item with two selectors (AND) and two list items (OR):

# AND: Pods labelled app=frontend IN namespace web
- namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: web}}
  podSelector: {matchLabels: {app: frontend}}

# OR: any Pod in namespace web, OR app=frontend Pods in the policy's own namespace
- namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: web}}
- podSelector: {matchLabels: {app: frontend}}

One extra - changes the meaning.

Diagnose →

kubectl describe networkpolicy allow-frontend -n api    # describe prints the parsed rules

Fix → Remove the extra dash to combine selectors in one peer.

Prevent → Use the Cilium/Calico policy editor or kubectl describe in review, and add connectivity tests that assert both allowed and denied paths.

All Kubernetes questions Roadmap: Kubernetes networking, storage & scaling

More on Services, DNS, Ingress and NetworkPolicy