Skip to content

Lesson 7 of 13 · 12m

Everything Talks Until You Say Otherwise

Allow-only NetworkPolicy semantics, the silent no-op of a non-enforcing CNI, and the default-deny skeleton with DNS.

Default state: everything reaches everything

A fresh cluster has no firewall. Every pod can open a connection to every other pod, to any node's ports the CNI path allows, to your cloud metadata endpoint, and to the entire internet. NetworkPolicy is how you change that — and the first thing to know about it is what it doesn't do: there are no deny rules. NetworkPolicies are allow-lists only, and they work by selection.

The mechanics (networking.k8s.io/v1):

kind: NetworkPolicy
spec:
  podSelector: {}            # every pod in this namespace
  policyTypes: [Ingress, Egress]
  ingress:
    - from:
        - podSelector:
            matchLabels: {app: frontend}
        - namespaceSelector:
            matchLabels: {team: platform}
      ports: [{port: 8080}]
  egress:
    - to:
        - ipBlock:
            cidr: 0.0.0.0/0
            except: [10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16]
      ports: [{port: 443}]

Selection semantics: once any ingress policy selects a pod, only explicitly allowed ingress sources reach it; pods selected by no policy remain default-allow. Same for egress. Entries within a from list are OR'd; combining podSelector + namespaceSelector in one item AND's them — which is also the trap: a peer item with both selectors means "pods in that namespace matching those labels," while two separate items mean "that namespace OR those labels anywhere." ipBlock with except is how you carve "the internet but not cluster IPs." ports takes numbers, named ports, and endPort ranges (1.25+).

The enforcement trap

Here is the failure that produces no error: NetworkPolicy is only enforced if your CNI implements it. Calico, Cilium, Antrea, Weave, and kube-router enforce. Plain kubenet doesn't. AWS's VPC CNI added support in 2023; older managed setups vary. If your CNI doesn't enforce policies, kubectl apply succeeds, kubectl get networkpolicy shows the object, and absolutely nothing is blocked. The cluster looks governed and isn't.

Verify before you rely on it: apply a default-deny, then kubectl exec into a pod and curl a peer. "Connection refused/timeout" means the CNI enforces; a response means it doesn't — or that your podSelector didn't select the pod you thought it did. Both failure modes are silent at apply time.

The default-deny skeleton

The working pattern, per namespace:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny
spec:
  podSelector: {}
  policyTypes: [Ingress, Egress]

Then add the two allows every namespace actually needs:

  • DNS egress to the cluster DNS pods (CoreDNS in kube-system, UDP+TCP 53). The universal post-lockdown outage is "everything resolves to nothing," and it's because DNS egress died with the rest.
  • Monitoring ingress from your metrics namespace, plus genuine app peers, by label selector.
egress:
  - to:
      - namespaceSelector:
          matchLabels: {kubernetes.io/metadata.name: kube-system}
    ports: [{port: 53, protocol: UDP}, {port: 53, protocol: TCP}]

kubernetes.io/metadata.name is the label every namespace gets automatically — the stable selector target that doesn't depend on anyone's labeling discipline.

Note what you cannot express: "deny this specific peer." Deny-lists don't exist; you deny by not allowing. If two pods must never talk, ensure no allow rule covers their pairing rather than hunting for a deny primitive that isn't there.

One scope rule that surprises people: a NetworkPolicy selects pods in its own namespace only — podSelector can't reach across namespaces; cross-namespace peers are expressed with namespaceSelector. Cilium and Calico extend the model with their own CRDs — L7 rules, FQDN-aware toFQDNs egress policies, clusterwide policies — useful, but they're vendor surface, not the portable API.

What to actually do

Default-deny ingress and egress per namespace, re-allow DNS, then add rules per workload. Test with exec+curl, not with confidence. And if your network plugin is one of the non-enforcing kind, switching CNIs is the real fix — YAML cannot conjure enforcement the datapath doesn't implement.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.