Kubernetes network policies: red vs blue

Vinnie, on the red team, shows how one weak Pod reaches every other Pod, and out of the cluster, because Kubernetes networking is open by default; Space Llama, on the blue team, closes it with a default-deny NetworkPolicy, explicit allows for ingress and egress, and a network plugin that enforces them.

GazeekKUBERNETES SECURITY: RED VS BLUE·EPISODE 4
EPISODE 4

Kubernetes network policies: red vs blue

Vinnie, on the red team, shows how one weak Pod reaches every other Pod, and out of the cluster, because Kubernetes networking is open by default; Space Llama, on the blue team, closes it with a default-deny NetworkPolicy, explicit allows for ingress and egress, and a network plugin that enforces them.

0:00 / 3:27

Diagrams in this video · 1Free in archhy

Copy and edit in archhy: How one Pod reaches every Pod (new tab)

Now try it on a real cluster

The videos are free. Gazeek's hands-on labs give you a real Kubernetes cluster in your browser for every lab, with checks that tell you when you've got it right: the practice the CKA and CKAD exams need.

The diagrams, full size

Copy any of them into archhy, free, and change it for your own system.

After this lesson you can

  • Explain why Pods are open to each other by default, and what lateral movement is
  • Write a default-deny NetworkPolicy and the allows an app needs, including name lookups
  • Check that your network plugin enforces NetworkPolicy
Transcript

Kubernetes network policies: red vs blue

Space Llama. Blue team. Today, the network. Vinnie. Red team. Say I've got into one Pod. Where can I go? By default? By default, anywhere I like.

How one Pod reaches every Pod

Here's the map my nose draws. One. A public web Pod, with a weakness. That's my way in. Two. By default, a Pod accepts connections from every other Pod. So from web, I can reach the database. Even in another namespace. Three. Every other Pod, on every port. Hopping sideways like this is called lateral movement. Four. Out of the cluster. Outbound is open too. The internet, and your cloud's metadata API. The Kubernetes security checklist says to filter that one. And web only ever needed one thing: the orders Pod. Blue allows that, and nothing else. You say that like it's easy.

Blue closes it: deny by default

It is, mostly. Start with default deny, in every namespace. An empty Pod selector picks every Pod in it. Ingress and egress, both. Now nothing gets in or out. One limit: a Pod can't block traffic from its own node.

Blue closes it: allow what's needed

Then allow what's needed, by label. This one selects the orders Pods. Traffic in, from Pods labeled web, on port 8080. Nothing else. And web needs a matching egress rule. A connection needs both ends to allow it. Web's own visitors need an ingress rule too.

Blue closes it: the rule everyone forgets

One rule everyone forgets. Denying egress also blocks name lookups, the Domain Name System, DNS. So allow port 53 to the cluster's DNS Pods. These labels suit kubeadm clusters; check what your DNS Pods carry. Then names resolve, and nothing else gets out.

Red asks, blue answers

Question for blue. Who actually enforces these? The network plugin. The Container Network Interface plugin, or CNI. Without one that supports NetworkPolicy, the policy has no effect at all. Calico, Cilium, Antrea and Kube-router all support it. Check yours. So the YAML applies fine, and nothing happens. Silently. Question for red. What do you sniff out first? A namespace with no policies. Policies only add allows. If none selects a Pod, everything's allowed.

Check your own cluster

Now check your own cluster. Kubectl get networkpolicy, all namespaces. Any namespace missing from that list is wide open. Check that your CNI plugin enforces policy. Then test it: from a Pod that shouldn't reach the database, try to connect, and expect it to fail. And make sure egress to the cloud metadata API is blocked, unless a Pod truly needs it.

Who won?

Round four to blue. If their plugin enforces it. Ours does. We tested it. Everyone says that. My nose stays skeptical.

The full article, and what's next

Next time, images you didn't build. The supply chain. My favorite. Other people's code.

Hands-on labs

Now try it on a real cluster

A real Kubernetes cluster in your browser for every lab, checked as you go.

See the labs

Read the article

Network Policies: the whole idea in writing, free.

Read it ↗