Kubernetes Network Policies

NetworkPolicy resources control which Pods can communicate. Ingress rules restrict incoming traffic; Egress rules restrict outgoing. Default: all traffic allowed. With policy: only explicitly allowed traffic permitted. Requires CNI with NetworkPolicy support (Calico, Cilium, Weave).

Overview

NetworkPolicy as a Pod firewall

Kubernetes NetworkPolicy resources act as firewall rules for Pods inside the cluster. They are the primary tool for implementing least privilege at the network layer.

A policy has a few working parts. A label-based podSelector picks the Pods it governs. Being selected puts those Pods into default deny for the policy types the policy covers. Ingress rules then allow traffic in, and egress rules allow traffic out. None of this is enforced by Kubernetes itself: the CNI plugin, the network add-on that wires Pods together, does the enforcing.

Key concepts

  • podSelector
  • namespaceSelector
  • policyTypes
  • Default deny all
  • CNI enforcement requirement
  • DNS egress allow

Interview questions

  • What happens when you create a NetworkPolicy for a Pod that previously had no policy?
  • Write a NetworkPolicy that allows only frontend pods to reach backend pods on port 8080.
  • Why might a NetworkPolicy not be working even though you applied it correctly?
  • How do you implement a zero-trust network posture in a Kubernetes namespace?

Sign up free to read the full topic

More DevOps topics

Start free on Cariara · Coding problems · System design studio