Pcornel
Browse topics
Options
Recent
Favorites
Containers · Technical reference

NetworkPolicy

Direct answer

NetworkPolicy restricts pod traffic by labels, namespaces and ports. Once a pod is selected by a policy for a direction, only allowed traffic passes; enforcement requires a CNI that supports it.

When to use this

Use it to segment workloads, or when traffic is blocked after policies are added.

First 30 seconds

1. kubectl get networkpolicy -n <ns>.

2. Which policies select the pod? (podSelector).

3. DNS egress allowed?

4. CNI enforces policies?

5. Test with a debug pod.

Operational verification

pod selected → ingress/egress rules → allowed peers/ports

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
Docker / containerd / Podman / Kubernetes / OCI-compatible platforms
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformDocker / containerd / Podman / Kubernetes / OCI-compatible platforms
Scope / prerequisiteUse it to segment workloads, or when traffic is blocked after policies are added.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 How to verify Verify boundary

Apply a default deny, then add allows one at a time and test each. Confirm the CNI plugin supports NetworkPolicy. Check namespace labels used in namespaceSelector.

Step 2 Common failures Narrow the fault

Forgetting DNS egress makes every connection fail with name errors. Namespace selectors match labels, not names, unless kubernetes.io/metadata.name is used. Policies have no effect with a CNI that does not implement them.

Key points

Start with default-deny per namespace and allow explicitly — including DNS egress to CoreDNS. Policies are additive (union of allows).

Key checks
  • kubectl get networkpolicy -n <ns> — first check for this topic
  • kubectl get pods -o wide — pods, status, node, IP
  • kubectl describe pod <pod> — events and conditions
  • kubectl logs <pod> [-c <c>] [--previous] — container logs (incl. previous crash)
  • kubectl get events --sort-by=.lastTimestamp — recent cluster events
ReferenceWhat it is1 section · depth on demand

How it works

The CNI plugin enforces policies by translating selectors into node-level filters. Ingress and egress are evaluated separately; traffic must be allowed by egress on the source and ingress on the destination when both are restricted.

ReferenceImportant terms & boundaries3 sections · depth on demand

Default-deny example

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: default-deny, namespace: app }
spec:
  podSelector: {}
  policyTypes: [Ingress, Egress]
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: allow-web-to-api, namespace: app }
spec:
  podSelector: { matchLabels: { app: api } }
  ingress: [{ from: [{ podSelector: { matchLabels: { app: web } } }], ports: [{ port: 8000 }] }]

Documented example, not captured output. Remember to allow DNS egress (UDP/TCP 53 to kube-system). Policies only work if the CNI enforces them.

In depth

The three rules that explain most NetworkPolicy behaviour

1. A Pod is unrestricted until at least one policy selects it for a direction (policyTypes: Ingress / Egress).

2. Once selected, only traffic that some policy allows is permitted; policies are additive (a union of allows), there are no deny rules.

3. A connection must be allowed by egress on the source and ingress on the destination; replies to an allowed connection are allowed automatically.

Selector combinations — one entry vs two entries

ingress:
- from:
  - namespaceSelector: { matchLabels: { team: web } }
    podSelector:       { matchLabels: { app: frontend } }   # AND: frontend Pods in team=web namespaces
ingress:
- from:
  - namespaceSelector: { matchLabels: { team: web } }
  - podSelector:       { matchLabels: { app: frontend } }   # OR: anything in team=web, or frontend here

The single dash makes the difference; it is the most common reason a policy allows more than intended.

Allowing DNS after a default deny

egress:
- to:
  - namespaceSelector: { matchLabels: { kubernetes.io/metadata.name: kube-system } }
    podSelector:       { matchLabels: { k8s-app: kube-dns } }
  ports: [{ protocol: UDP, port: 53 }, { protocol: TCP, port: 53 }]

Testing

Test each allowed path and one forbidden path from a throwaway Pod (kubectl run --rm -it --image=busybox), and remember ipBlock rules see the address after any SNAT — traffic through a load balancer may arrive from node IPs, not client IPs.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

Common mistakes

  • Assuming NetworkPolicy works without a CNI that enforces it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terminology layer

Terms & aliases

Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.

Aliases that resolve here
networkpolicykubernetes networkpolicydefault deny policy
Confirmation

Confirm action

Continue?