What is Kubernetes network policy?
Why Interviewers Ask This
Mid-level Kubernetes (K8s) roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.
Answer
Kubernetes Network Policies are Kubernetes objects that control traffic flow between pods, using pod/namespace selectors and port specifications. Without Network Policies, all pods can communicate with all other pods cluster-wide — the default is "allow all." Network Policy spec: apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: api-network-policy namespace: production spec: podSelector: matchLabels: app: api # Apply to pods with this label policyTypes: [Ingress, Egress] ingress: - from: - namespaceSelector: matchLabels: environment: production # Same namespace - podSelector: matchLabels: role: frontend # From frontend pods - ipBlock: cidr: 203.0.113.0/24 except: [203.0.113.5/32] ports: - protocol: TCP port: 3000 egress: - to: - podSelector: matchLabels: app: postgres ports: - protocol: TCP port: 5432 - to: [] ports: - protocol: TCP port: 443 # Allow HTTPS egress to internet. Default deny-all pattern: create a NetworkPolicy selecting all pods in a namespace with empty ingress/egress rules → blocks all traffic. Then create specific allow policies. This is defense-in-depth. Requirement: Network Policies only work with CNI plugins that enforce them (Calico, Cilium, Weave — NOT Flannel alone). Namespaces and selectors: combine namespaceSelector + podSelector with AND logic (within one from element) vs OR logic (separate from elements). Cilium: extends Network Policy with L7 policy (allow only specific HTTP paths/methods, DNS-based policies).
Pro Tip
This topic has Kubernetes (K8s)-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.
Previous
What is Kubernetes node affinity and pod affinity?
Next
What is Kubernetes resource monitoring and observability?