Lesson 17 of 20 · Practice: Vault, egress and tenant labs
Blocking internet egress by pod label
Block internet access for every pod in the cluster by default and allow a few by label, with one cluster-wide deny policy (Cilium or Calico) that no team can re-open from its own namespace. Plus the gotchas: guarding the label, CoreDNS, host-network pods, proxies and IPv6.
Open the lab on GitHubthe companion repo has this guide as guides/egress-by-label.md
Where this lesson fits
Two lessons already cover egress inside one namespace: Network policies & mTLS (default-deny, then allow DNS and real flows) and, in Kubernetes Administration, Ingress & egress gateways (allow-lists by DNS name, egress gateways with a fixed source IP). This lesson solves a different problem: one rule for the whole cluster that says "pods don't reach the internet unless the platform team said so", and that teams can't undo.
CLUSTER-WIDE DENY (platform team)
pod without label ──✗──► internet (public IPs)
pod without label ──✓──► other pods, Services, DNS, private ranges
pod internet-access=true ──✓──► internet
namespace policies (teams) can't re-open what the deny closed
A school has one rule at the gate: no pupil leaves during the day unless they carry a pass from the office. Each classroom can make its own rules inside the building, but no class teacher can override the gate.
Why a normal NetworkPolicy can't do this
- A standard
NetworkPolicylives in one namespace and only affects pods there. - It has no deny rule: everything is an allow, and allows add up. If the platform team adds a default-deny in a team's namespace, the team can add an allow next to it, and the allow wins.
So you need the CNI's own cluster-wide policy with a real deny that is evaluated before (or above) namespace policies. Both Cilium and Calico have one.
Cilium: CiliumClusterwideNetworkPolicy
apiVersion: cilium.io/v2
kind: CiliumClusterwideNetworkPolicy
metadata: { name: no-internet-by-default }
spec:
endpointSelector:
matchExpressions:
- { key: internet-access, operator: NotIn, values: ["true"] } # block by default, allow a few
- { key: k8s:io.kubernetes.pod.namespace, operator: NotIn, values: [kube-system] }
enableDefaultDeny: { egress: false, ingress: false } # add only this deny; change nothing else for these pods
egressDeny:
- toCIDRSet:
- cidr: 0.0.0.0/0
except: [10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16]
egressDeny beats any allow. enableDefaultDeny: false matters: without it, selecting a pod in a policy switches that pod to default-deny for all its egress, not just the internet. The field exists only in recent Cilium releases (1.16 or later at the time of writing), so check your version.
Calico: GlobalNetworkPolicy in its own tier
apiVersion: projectcalico.org/v3
kind: Tier
metadata: { name: platform }
spec: { order: 100 } # lower order = evaluated first
---
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata: { name: platform.no-internet-by-default }
spec:
tier: platform
selector: internet-access != 'true' && projectcalico.org/namespace != 'kube-system'
types: [Egress]
egress:
- action: Pass # private and in-cluster ranges: let the namespace policies decide
destination: { nets: [10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16] }
- action: Deny # everything else is the internet
The tier runs before the default tier where namespace NetworkPolicy objects live, so a team's allow never gets a chance for internet traffic. Tiers are in recent open-source Calico releases; older ones only had them in the commercial editions, so check your version.
The other way round: allow by default, block a few
Change only the selector:
- Cilium:
{ key: internet-access, operator: In, values: ["false"] } - Calico:
selector: internet-access == 'false'
Allow (or block) one workload
Put the label on the pod template, not on the Deployment:
spec:
template:
metadata:
labels: { internet-access: "true" }
Test it
kubectl run t1 --rm -it --restart=Never --image=curlimages/curl -- curl -m5 -sI https://example.com
kubectl run t2 --rm -it --restart=Never --image=curlimages/curl -l internet-access=true -- curl -m5 -sI https://example.com
# block-by-default: t1 times out, t2 gets a 200
kubectl run t3 --rm -it --restart=Never --image=curlimages/curl -- curl -m5 -s http://kubernetes.default.svc -k -o /dev/null -w '%{http_code}\n'
# in-cluster traffic still works (any HTTP code, not a timeout)
Gotchas
- Guard the label. Anyone who can edit their own Deployment can add
internet-access: "true". Add an admission rule (Kyverno or ValidatingAdmissionPolicy, see Pod Security & admission control) so only platform admins or the GitOps controller may set or change it. - CoreDNS forwards to upstream resolvers. If those are public (8.8.8.8), keep
kube-systemexcluded as above, or DNS breaks for everyone. - Not covered by pod policies:
hostNetworkpods and the nodes themselves (image pulls go from the node). - Proxies. An in-cluster HTTP proxy that has internet access is a way around the rule for every pod that can reach it.
- IPv6. On dual-stack clusters, also deny
::/0. - Private ranges aren't always private. If your cloud uses other internal ranges, or you must block a corporate range too, adjust the
except/Passlist.
For allow-lists by DNS name (only api.partner.example), use Cilium toFQDNs or an egress gateway, covered in Ingress & egress gateways.
Try it
- On a kind cluster with Cilium (or Calico) as the CNI, apply the cluster-wide policy.
- Run the three test pods above and confirm the results.
- In a test namespace, add a normal NetworkPolicy that allows all egress. Run t1 again in that namespace: it must still time out.
- Write a Kyverno or ValidatingAdmissionPolicy rule that rejects
internet-access: "true"unless the request comes from the GitOps controller's ServiceAccount.
Recap
- Namespace
NetworkPolicycan't enforce a cluster-wide rule: it's per namespace and allow-only. - Use CiliumClusterwideNetworkPolicy with
egressDeny, or a Calico GlobalNetworkPolicy in a tier that runs first. - Select pods by a label; flip the selector for "allow by default, block a few".
- Guard the label with admission policy, keep DNS working, and remember host-network pods, nodes, proxies and IPv6.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.