Example: basic allow-frontend-to-db policy
Key fields explained
Pod selectorpodSelectordetermines which pods the policy selects by matching pod labels.- In the example above,
matchLabels: role: dbmeans the policy targets pods labeledrole: dbin the same namespace (labels are case-sensitive).
policyTypesdeclares which traffic directions the policy controls:Ingress— controls incoming traffic to the selected pods.Egress— controls outgoing traffic from the selected pods.
- The example lists only
Ingress, so it restricts only incoming traffic to therole: dbpods; egress remains unaffected.
ingressis a list of rules that permit incoming connections to the selected pods. Each rule contains afromlist of allowed sources.- Allowed
fromentries include:podSelector— select pods by label,namespaceSelector— select all pods in namespaces matching the label(s),ipBlock— permit traffic from specified CIDR ranges (optionally withexceptranges).
- The sample reads: “Pods labeled
role: frontendmay reach pods labeledrole: db.” No other sources are permitted by this policy for ingress.
Empty mappings vs empty lists — critical difference
Use backticks for literal forms:`{}` (empty mapping) and `[]` (empty list).
Semantics:
- An empty
podSelector(`{}`) selects all pods in the namespace. - An empty
namespaceSelector(`{}`) matches all namespaces. - An explicitly empty rule list such as
ingress: []oregress: []means “no allowed sources/rules” for that direction — effectively denying that direction for the selected pods (if the policy type includes that direction).
Examples and variations
- Ingress-only policy that targets
role: db
- Ingress-only: selects
role: dbpods and permits traffic exclusively from pods labeledrole: frontend.
- Checking policies on the cluster
- List NetworkPolicy resources:
- Inspect a NetworkPolicy in human-readable form:
- The
kubectl describeoutput summarizes the pod selector, policy types, and allowed sources in plain language.
- Namespace selector trap
namespaceSelectormatches namespaces by label. This rule permits traffic from any pod in namespaces labeledproject: frontend, regardless of the pod labels in those namespaces.
- IP block
cidr: 172.17.0.0/16allows any IP address in 172.17.0.0–172.17.255.255.
- Policy that selects all pods in a namespace
podSelector: {}selects every pod in the current namespace. Ingress is then restricted to traffic from pods withrole: frontend.
- Explicit deny all ingress to selected pods
ingress: []is an explicit empty list: no incoming sources are allowed. SinceIngressis included inpolicyTypes, all incoming connections to the selected pods are denied.
- Namespace
frommatching all namespaces
namespaceSelector: {}matches every namespace; this rule permits traffic from all pods in all namespaces to the selected pods.
- Small syntactic differences can change security posture completely:
ingress: []— denies all incoming traffic (no allowed sources).ingress:withfrom: - namespaceSelector: {}— explicitly allows all namespaces as sources (i.e., effectively allows all incoming traffic).
podSelector: {}selects all pods in the namespace.- Including both
IngressandEgressinpolicyTypesplus empty lists for each direction results in a true deny-all policy for that namespace: nothing is allowed in or out.
When a pod is selected by at least one NetworkPolicy that covers a specific direction (Ingress or Egress), that direction becomes isolated: only traffic explicitly allowed by the matching rules is permitted. If there are no rules for that direction (for example,
ingress: []), no traffic for that direction is allowed.- Always read every field in a NetworkPolicy before applying it. Small differences (an empty selector
`{}`vs a missing selector, or an empty list[]) can change behavior from “allow some traffic” to “deny everything”. - When testing, use
kubectl get netpolandkubectl describe netpol <name>to confirm the policy you applied matches your intent. - Prefer incremental changes in production: apply a restrictive policy in a staging namespace first and test connectivity with representative clients.