Skip to main content
Learn how to read and reason about Kubernetes NetworkPolicy YAML manifests. This guide walks through a representative NetworkPolicy, explains the key fields, and shows practical variations you’ll encounter when securing pod-to-pod traffic in a namespace. Reference: Kubernetes NetworkPolicy docs

Example: basic allow-frontend-to-db policy

This minimal policy demonstrates the most common fields you’ll see in NetworkPolicy manifests.

Key fields explained

Pod selector
  • podSelector determines which pods the policy selects by matching pod labels.
  • In the example above, matchLabels: role: db means the policy targets pods labeled role: db in the same namespace (labels are case-sensitive).
policyTypes
  • policyTypes declares 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 the role: db pods; egress remains unaffected.
ingress (rules)
  • ingress is a list of rules that permit incoming connections to the selected pods. Each rule contains a from list of allowed sources.
  • Allowed from entries include:
    • podSelector — select pods by label,
    • namespaceSelector — select all pods in namespaces matching the label(s),
    • ipBlock — permit traffic from specified CIDR ranges (optionally with except ranges).
Interpreting the sample
  • The sample reads: “Pods labeled role: frontend may reach pods labeled role: 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: [] or egress: [] means “no allowed sources/rules” for that direction — effectively denying that direction for the selected pods (if the policy type includes that direction).
Quick reference table

Examples and variations

  1. Ingress-only policy that targets role: db
  • Ingress-only: selects role: db pods and permits traffic exclusively from pods labeled role: frontend.
  1. Checking policies on the cluster
  • List NetworkPolicy resources:
  • Inspect a NetworkPolicy in human-readable form:
  • The kubectl describe output summarizes the pod selector, policy types, and allowed sources in plain language.
  1. Namespace selector trap
  • namespaceSelector matches namespaces by label. This rule permits traffic from any pod in namespaces labeled project: frontend, regardless of the pod labels in those namespaces.
  1. IP block
  • cidr: 172.17.0.0/16 allows any IP address in 172.17.0.0–172.17.255.255.
  1. Policy that selects all pods in a namespace
  • podSelector: {} selects every pod in the current namespace. Ingress is then restricted to traffic from pods with role: frontend.
  1. Explicit deny all ingress to selected pods
  • ingress: [] is an explicit empty list: no incoming sources are allowed. Since Ingress is included in policyTypes, all incoming connections to the selected pods are denied.
  1. Namespace from matching all namespaces
  • namespaceSelector: {} matches every namespace; this rule permits traffic from all pods in all namespaces to the selected pods.
Comparing subtle differences
  • Small syntactic differences can change security posture completely:
    • ingress: [] — denies all incoming traffic (no allowed sources).
    • ingress: with from: - namespaceSelector: {} — explicitly allows all namespaces as sources (i.e., effectively allows all incoming traffic).
Deny-all example (both directions)
  • podSelector: {} selects all pods in the namespace.
  • Including both Ingress and Egress in policyTypes plus 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.
Final notes and best practices
  • 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 netpol and kubectl 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.
Links and references

Watch Video

Practice Lab