You want to create a NetworkPolicy that allows pods labeled role=frontend to access pods labeled app=nginx on port 80, but blocks all other traffic. Which YAML snippet correctly achieves this?
hard📝 Workflow Q15 of 15
Kubernetes - RBAC and Security
You want to create a NetworkPolicy that allows pods labeled role=frontend to access pods labeled app=nginx on port 80, but blocks all other traffic. Which YAML snippet correctly achieves this?
Step 1: Identify pods to protect and allowed sources
The policy must select pods with app: nginx and allow ingress only from pods with role: frontend.
Step 2: Check ingress rules and ports
spec:
podSelector:
matchLabels:
app: nginx
ingress:
- from:
- podSelector:
matchLabels:
role: frontend
ports:
- protocol: TCP
port: 80 correctly uses podSelector for nginx pods and allows ingress from frontend pods on TCP port 80.
Step 3: Confirm other options are incorrect
The snippet that selects role: frontend in podSelector but has from app: nginx reverses source and destination; the snippet using egress and to controls outgoing traffic; the snippet using namespaceSelector selects entire namespaces instead of specific pods.