Skip to main content
So what does a deny-all NetworkPolicy actually say, line by line? Bob copied a file, applied it, and watched Canal block traffic from alpha to beta. That confirmed the file worked for a quick test, but it didn’t teach him how to read or write policies. Soon he’ll need to write policies for his apps — and you can’t write what you can’t read. Here is the deny-all policy Bob used:
This is valid Kubernetes YAML. Indentation shows the hierarchy; the three top-level descriptive fields and the behavioral field are worth separating so reading becomes straightforward.
  • apiVersion — Which API group/version this object belongs to. For NetworkPolicies: networking.k8s.io/v1.
  • kind — The type of object. For every policy you’ll write, this is NetworkPolicy.
  • metadata — Names the policy and sets the namespace. Important: NetworkPolicies are namespace-scoped. A policy only affects pods in its own namespace.
  • spec — Contains the behavior: selectors, policy directions, and the actual rules.
Example header showing those four fields together:
Common fields you’ll see inside spec:
  • podSelector — Chooses which pods the policy applies to. An empty selector selects all pods in the namespace.
  • policyTypes — Lists which directions the policy covers: Ingress, Egress, or both.
  • ingress — A list of rules that allow traffic into selected pods.
  • egress — A list of rules that allow outbound traffic from selected pods.
Quick reference table Two small symbols matter a lot:
  • An empty pair of curly braces: podSelector: {} — commonly used to mean “select all pods in the namespace”.
  • An empty pair of square brackets: ingress: [] or egress: [] — means “there are no rules for this direction”.
Those characters completely change what a policy does. In the deny-all example above, the policy selects all pods and sets both ingress: [] and egress: [] while policyTypes covers both directions. The effect: deny all ingress and egress for the selected pods (all pods in the default namespace). Examples (common patterns)
  • Selector with a pod label that allows only certain sources:
  • Permit traffic only from a CIDR range using ipBlock:
Reading practice: things to ask for each NetworkPolicy file
  • What policyTypes are specified? (Ingress, Egress, or both.)
  • Which pods are targeted? (Look at podSelector and the policy’s namespace.)
  • For ingress rules: who is allowed to connect? (Pod selectors, namespace selectors, ipBlock, etc.)
  • When you see podSelector: {} or ingress: [], what is the effective behavior?
Why two similar-looking files can behave differently Two files may look similar at the top but have different effects based on their ingress or egress contents. For example:
  • A file targeting pods labeled role: db with an ingress that contains an ipBlock (CIDR) explicitly permits traffic from that IP range.
  • A file targeting the same pods but having ingress: [] and policyTypes including Ingress defines no allowed ingress peers — effectively denying all incoming traffic to those pods.
Practical rule of thumb
  • ingress: [] together with policyTypes that include Ingress = explicitly deny all incoming traffic to the selected pods.
  • An ingress list that contains from: clauses = allow only the specified sources.
Inspecting policies on a cluster You can confirm what’s deployed and see the exact YAML with:
Exercises (try these)
  • Open the sixth file in Bob’s set that contains ingress: []. Who does it allow? (Answer: it allows no incoming peers — it denies all ingress to the selected pods when Ingress is included in policyTypes.)
  • Compare that file to the one that uses ipBlock. Which sources are permitted in each?
Further reading
Remember: podSelector: {} selects all pods in the policy’s namespace. An empty rule list such as ingress: [] explicitly denies that direction to the selected pods (when Ingress is included in policyTypes).

Watch Video