Skip to main content
Networking & service meshLesson 4 of 11

Blocking and Allowing Traffic (NetworkPolicy)

The shop namespace has frontend, backend, and db Pods. At first anyone can connect to db. Let's change it so that "frontend can only call backend, and only backend can call db".

frontend → backendallowed
backend → dballowed
others → dballowed
Namespace shopAdminwrites YAMLPolicyNetworkPolicyCiliumenforces rulesfrontendapp=frontendbackendapp=backenddbapp=dbdebugPod in ops
RequestResponse / reportWatchAppears in a later step

What you will learn

  • Why all Pods can talk to each other without a policy
  • The network plugin is what actually enforces policies
  • How labels select what is allowed
  • Why blocked requests look like timeouts

Symptoms and where to look​

SymptomSuspectWhere to look
Traffic that worked suddenly times outA new policy now selects the target PodPOD-SELECTOR in kubectl get networkpolicy vs the target Pod’s labels
Every external call fails with "name not found"Egress is blocked and DNS (port 53) was not openedEgress rules in kubectl describe networkpolicy
The policy has no effect at allNo Pod matches the selector, or the network plugin does not support policiesCompare the policy’s Pod selector with the Pod labels

Try it yourself​

kubectl get networkpolicy -n <namespace> # POD-SELECTOR of each policy
kubectl describe networkpolicy <name> -n <namespace> # Allowed Ingress and Egress
kubectl get pods -n <namespace> -l app=backend --show-labels # Pods the selector matches
kubectl get pods -n kube-system -l k8s-app=cilium # Is the enforcing CNI (Cilium) running?

Check yourself

  1. Right after a default-deny policy is applied, frontend's calls to backend hang until they time out. Why a timeout and not a "refused" error?
  2. After blocking both Ingress and Egress, every external call fails with "name not found". What was forgotten?
  3. You create a NetworkPolicy and it has no effect at all, with no error. Which is a likely cause?

Further reading: Network Policies · Cilium docs