Pcornel
Containers · Kubernetes Networking & Services

NetworkPolicy

Cheat sheet · https://pcornel.dk/topic.php?topic=6150

Back to the Topic

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.

Remember

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

First 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

Commands

kubectl run --rm -it --image=busybox

Verify in this order

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

Where it usually breaks

Forgetting DNS egress makes every connection fail with name errors.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=6150

Confirmation

Confirm action

Continue?