Control Cross-Namespace Traffic Using NetworkPolicy
Scenario
A multi-tier application runs across two namespaces: application-tier and database-tier. Both namespaces are labeled so NetworkPolicies can select them by name.
Currently there are no restrictions. Any Pod can talk to any other Pod across namespaces. Your task is to lock this down.
Task
Create two NetworkPolicies:
1. deny-ingress-app in application-tier — applies to all Pods. Denies all ingress traffic. No Pod from any namespace can reach Pods inside application-tier.
2.allow-app-to-db in database-tier — applies to all Pods in database-tier. Allows ingress only from Pods in the application-tier namespace. All other namespaces are blocked. Use a namespaceSelector with label name=application-tier to match the source namespace.
# verify app-pod can reach db-pod
kubectl exec -n application-tier app-pod -- \
wget -qO- http://db-pod.database-tier.svc.cluster.local:80
# verify db-pod cannot reach app-pod
kubectl exec -n database-tier db-pod -- \
wget -qO- --timeout=3 http://app-pod.application-tier.svc.cluster.local:80
Hint
For deny-ingress-app: set policyTypes: [Ingress] with no ingress rules — this denies all ingress.
For allow-app-to-db: use ingress[].from[].namespaceSelector matching name=application-tier.
See: Network Policies