
Hook (read this straight into the camera): If you have never applied a NetworkPolicy to your Kubernetes cluster, every pod can talk to every other pod — across namespaces, across teams, across "this should definitely be isolated" boundaries. Kubernetes ships with no pod-to-pod firewall by default. Here's how to actually build one, with three YAML files you can apply right now.
Kubernetes Network Policies — The Firewall Most Clusters Don't Have
This one surprises people who've run Kubernetes in production for years: there is no default network isolation between pods. A pod in your marketing-site namespace can open a raw TCP connection straight to a pod in your payments namespace, with zero configuration, zero warning, and zero line in any audit log that says this shouldn't be allowed. Kubernetes treats the pod network as one flat, fully-routable space unless you tell it otherwise.
Why this isn't already handled for you
NetworkPolicy is a real Kubernetes API object — it's been stable since 1.7. The catch is that the Kubernetes control plane doesn't enforce it. Enforcement is delegated to whatever CNI (Container Network Interface) plugin wires up your pod network, and not every CNI implements it:
- Calico, Cilium, Weave Net, Antrea — implement and enforce
NetworkPolicy. - Flannel in its default configuration — does not enforce it. You can
kubectl applya NetworkPolicy, it will be accepted and stored in etcd, and it will do absolutely nothing. No error, no warning.
That gap is where most clusters end up unprotected: someone applied policies that look correct, moved on, and never confirmed the CNI was actually the enforcing kind. Check yours first:
# Confirm your CNI actually enforces NetworkPolicy before you trust any
# of the YAML below. kubectl apply will succeed even on a CNI that
# silently ignores the object (flannel, in its default config, is the
# classic example -- the policy exists in etcd and does nothing).
kubectl get pods -n kube-system -o wide | grep -Ei 'calico|cilium|weave|antrea'
# If that returns nothing, check what CNI config the nodes actually run
kubectl get cm -n kube-system -o jsonpath=\'{.items[*].metadata.name}\' | tr \' \' \'\\n\' | grep -i cniIf that comes back empty, stop here and fix your CNI before writing a single policy — everything below is inert on a cluster that doesn't enforce it.
The default-deny baseline
The right mental model for NetworkPolicy is the same as any firewall: start closed, then open exactly what's needed. An empty podSelector ({}) means "every pod in this namespace." A policy with a policyTypes entry but no matching rules block means "allow nothing of that type." Put those together and you get a default-deny.
1. Deny all inbound traffic by default — nothing reaches any pod in the namespace unless a later policy explicitly allows it:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all-ingress
namespace: payments
spec:
podSelector: {} # applies to every pod in this namespace
policyTypes:
- Ingress
# no "ingress:" rules block -> nothing is allowed in, from anywhere2. Deny all outbound traffic by default — this is the one teams skip, and it's the one that actually stops a compromised pod from exfiltrating data or reaching out to a C2 server:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all-egress
namespace: payments
spec:
podSelector: {}
policyTypes:
- Egress
# no "egress:" rules block -> nothing is allowed out, to anywhereThe moment you apply egress-deny, DNS breaks for everything in the namespace — pods can't even resolve the services they're allowed to reach. So the very next policy has to carve out an exception for DNS before anything else will work:
3. Allow DNS, and only DNS, out:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns-only
namespace: payments
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- namespaceSelector: {} # kube-system, wherever CoreDNS lives
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53Apply those three and you've gone from "flat network, everything reachable" to "nothing in, nothing out except DNS" — a clean, auditable starting point instead of an implicit allow-all you never chose.
Opening one real, scoped path between namespaces
A default-deny cluster is only useful once legitimate traffic can still get through. Say a checkout service in the payments namespace needs to call a ledger-api service in a separate ledger namespace, on port 8443, and nothing else should be able to reach ledger-api at all.
NetworkPolicy rules are evaluated per-namespace and combine with AND logic across namespaceSelector and podSelector inside the same rule — so the match has to be specific on both the sender's namespace and label, and the policy has to exist on both sides (egress allowed from the caller, ingress allowed on the callee) because each namespace's deny-all only governs traffic in that namespace:
On the payments side — allow checkout pods to reach ledger-api pods in ledger, nowhere else:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-checkout-to-ledger
namespace: payments
spec:
# this policy only governs pods matching this selector
podSelector:
matchLabels:
app: checkout
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: ledger
podSelector:
matchLabels:
app: ledger-api
ports:
- protocol: TCP
port: 8443On the ledger side — allow ingress only from checkout pods in payments, on that same port:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-ingress-from-checkout
namespace: ledger
spec:
podSelector:
matchLabels:
app: ledger-api
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: payments
podSelector:
matchLabels:
app: checkout
ports:
- protocol: TCP
port: 8443Note the label selectors do the real work here, not the namespace name alone. kubernetes.io/metadata.name is a label Kubernetes sets automatically on every namespace since 1.21+, so you don't have to remember to label namespaces by hand for this pattern to work.
Prove it actually works
Don't trust that applying YAML did anything until you've tried to break it. Spin up a pod somewhere unauthorized and confirm it can't reach what it shouldn't:
# After applying the templates, prove the deny actually denies.
# Spin up a throwaway pod in a *different* namespace and try to reach
# something in "payments" that is not explicitly allowed.
kubectl run probe --rm -it --image=busybox:1.36 -n default -- \
wget -T 2 -O- http://checkout.payments.svc.cluster.local
# Expected result once default-deny-all-ingress is applied: timeout.
# If it connects, the CNI isn\'t enforcing the policy -- stop and fix
# that before relying on any of this for isolation.If that probe connects after you've applied default-deny-all-ingress, the policy isn't being enforced — go back to the CNI check above before trusting anything else in this post.
Apply this to your own cluster
The three starter templates above — default-deny-all-ingress, default-deny-all-egress, and allow-dns-only — are copy-paste ready. Swap the namespace field for your own and apply them to one non-critical namespace first:
kubectl apply -f default-deny-ingress.yaml -f default-deny-egress.yaml -f allow-dns-only.yamlThen add scoped allow rules one real traffic path at a time, the way the checkout → ledger example above does, instead of trying to map your entire service mesh's traffic graph before you turn anything on. A namespace with three deny-by-default policies and a handful of explicit allows is dramatically more auditable than a namespace with none — and it's the difference between "a compromised pod can reach my database" and "a compromised pod can reach DNS and nothing else."

Experienced DevOps Engineer proficient in Python, Docker, Kubernetes, CI/CD, and cloud technologies. Specialized in automating processes and building scalable infrastructure for seamless development and deployment.


