14.7 Visualizing and Testing Network Policies

Right, so you’ve written a NetworkPolicy. You’ve stared at the YAML, you’ve run kubectl apply -f, and it returned without an error. Fantastic. Now for the multi-million dollar question: is it actually doing what you think it’s doing? This is where most people’s eyes glaze over and they just hope for the best. Don’t be that person. Hope is not a strategy; it’s a prelude to a 3 AM page. Let’s get tactical and verify our work.

14.6 CNI Plugin Requirements: Calico, Cilium, Weave

Right, let’s talk about the plumbing. You’ve got your shiny Network Policies written, a set of perfect, declarative rules telling your pods exactly who they can and can’t talk to. You apply them…and nothing happens. The traffic flows merrily along, completely ignoring your carefully crafted security intentions. Welcome to the most common “oh crap” moment with Network Policies. The truth they don’t lead with in the marketing docs is this: Network Policies are not a feature of Kubernetes itself. They are a specification. It’s a wish list you hand off to the actual entity running your cluster’s network: the Container Network Interface (CNI) plugin.

14.5 Default Deny All: The Secure Baseline

Right, let’s talk about the single most important concept in securing your Kubernetes cluster’s network: starting from a position of “absolutely nothing is allowed.” This isn’t just a good idea; it’s the only sane way to begin. You wouldn’t build a castle and just leave the drawbridge down with a welcome mat, would you? Default Deny All is that raised drawbridge. It’s the security baseline that says, “Until I explicitly say a pod can talk to something, it lives in solitary confinement.”

14.4 Restricting Egress to Specific CIDRs and Ports

Right, so you’ve got your pods running, but letting them talk to anything on the internet is like giving a toddler your credit card and telling them to go wild on Amazon. It’s a bad idea, and you’re going to get a nasty surprise. We need to lock down what they can call out to. This is where egress rules in Network Policies come in, and they’re your first, best line of defense against data exfiltration, crypto-mining pods, or just plain old “oops, that pod called the wrong API 50,000 times a second.”

14.3 Restricting Ingress from Specific Pods and Namespaces

Right, so you’ve got a cluster up and running, pods are chatting away happily, and now you need to introduce some law and order. You don’t want every pod in the dev namespace being able to poke at your super-secret payment-processor pod, do you? Of course not. This is where you stop being a benevolent creator and start being a traffic cop with a badge and a serious attitude. Network Policies are your Kubernetes-native tool for this job. They’re not some add-on; they’re first-class citizens that define how pods are allowed to communicate with each other and other network endpoints. Think of them as firewall rules for your cluster, but way more pod-aware. The crucial thing to remember is that by default, if no Network Policies are present, all traffic is allowed. It’s a “default allow” world. The moment you slap a Network Policy on a pod, you shift it into a “default deny” mode for that direction (ingress/egress), and you must explicitly allow what you want. This trips up everyone at least once.

14.2 NetworkPolicy Spec: podSelector, policyTypes, ingress, egress

Alright, let’s get our hands dirty with the actual spec of a NetworkPolicy. This is where the rubber meets the road. Think of a NetworkPolicy as a very specific, very powerful bouncer for your Pod’s network traffic. It doesn’t just let anything in or out; it checks the guest list. The spec is how you write that list. The core blueprint looks like this: apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: my-detail-oriented-bouncer spec: podSelector: {} # Which Pods does this bouncer guard? policyTypes: # What kind of rules are we defining? - Ingress - Egress ingress: [] # The fine-print rules for incoming traffic egress: [] # The fine-print rules for outgoing traffic Let’s break down each part of this bouncer’s contract.

14.1 Default Allow-All and Why It Is Dangerous

Right out of the box, your Kubernetes cluster is a model of reckless optimism. It operates on a principle of “default allow-all.” This is the networking equivalent of leaving your front door wide open with a sign that says, “All valuables are in the upstairs safe, which is also unlocked.” Every pod can talk to every other pod, and every pod can initiate egress traffic to the entire internet. It’s convenient for getting started, but it’s a security nightmare waiting to happen.

— joke —

...