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:
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 isNetworkPolicy.metadata— Names the policy and sets thenamespace. 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.
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.
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: []oregress: []— means “there are no rules for this direction”.
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:
- What
policyTypesare specified? (Ingress, Egress, or both.) - Which pods are targeted? (Look at
podSelectorand the policy’snamespace.) - For ingress rules: who is allowed to connect? (Pod selectors, namespace selectors,
ipBlock, etc.) - When you see
podSelector: {}oringress: [], what is the effective behavior?
ingress or egress contents. For example:
- A file targeting pods labeled
role: dbwith aningressthat contains anipBlock(CIDR) explicitly permits traffic from that IP range. - A file targeting the same pods but having
ingress: []andpolicyTypesincludingIngressdefines no allowed ingress peers — effectively denying all incoming traffic to those pods.
ingress: []together withpolicyTypesthat includeIngress= explicitly deny all incoming traffic to the selected pods.- An
ingresslist that containsfrom:clauses = allow only the specified sources.
- 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 whenIngressis included inpolicyTypes.) - Compare that file to the one that uses
ipBlock. Which sources are permitted in each?
- Kubernetes NetworkPolicy docs: https://kubernetes.io/docs/concepts/services-networking/network-policies/
- Examples and patterns for common NetworkPolicy use cases in the official docs.
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).