Interview Prep/Real-World Interview/Write-a-Kubernetes-manifest tasks

Write a NetworkPolicy that allows only the frontend to reach the backend on port 8080, and denies everything else.

IntermediateHands-onWrite-a-Kubernetes-manifest tasks
Answer
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: default-deny-ingress, namespace: shop }
spec:
  podSelector: {}
  policyTypes: ["Ingress"]
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: backend-from-frontend, namespace: shop }
spec:
  podSelector: { matchLabels: { app: backend } }
  policyTypes: ["Ingress"]
  ingress:
    - from:
        - podSelector: { matchLabels: { app: frontend } }
      ports:
        - { protocol: TCP, port: 8080 }

Key trick → An empty podSelector: {} selects every pod in the namespace, which makes it the default deny. NetworkPolicies only take effect if the CNI enforces them (Calico, Cilium, or the VPC CNI with network policy enabled).

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Kubernetes-manifest tasks