Networking: Services, DNS, Ingress and policies · 6 MIN READ
NetworkPolicies: default deny and label-based allow-lists for pod traffic
Isolate pods with NetworkPolicies, allow traffic by pod label, namespace and IP block, build a default-deny baseline that keeps DNS working, and verify enforcement from a client pod.
Out of the box every pod can connect to every other pod in every namespace, so one compromised pod can reach your database. NetworkPolicies restrict that with allow-lists that the network plugin enforces. The rules are simple but not intuitive: there are no deny rules, and a policy changes the behaviour of the pods it selects, not of the sources it names.
kubernetes · A MAP OF THE IDEA
- Default deny per namespace
- Allow DNS egress
- Allow each flow by label
- Prove it with a timeout
Useful when
- Labels, not IPs, define who may talk, so rules survive churn.
- Default deny limits what a compromised pod can reach.
Watch out
- A CNI that does not enforce accepts policies and ignores them.
- Egress deny without a DNS rule breaks every name lookup.
Implementation & operation cost: A policy per flow, a CNI that enforces them, and a negative test whenever a policy changes.
Default allow, isolation and additive rules
A pod that no NetworkPolicy selects is non-isolated: all traffic in and out is allowed. As soon as one policy with Ingress in its policyTypes selects a pod, that pod is isolated for ingress, and only connections allowed by at least one of the selecting policies get in. Egress works the same way for outgoing connections. Policies only add allowances: there are no deny rules and no ordering, and the union of all matching policies is what is allowed. Reply packets of an allowed connection always pass. A connection between two pods needs both ends to agree, egress from the source and ingress to the destination. Blocking one source while allowing everyone else needs cluster-scoped policies from the Network Policy API project or CNI-specific resources.
An allow-list for one app
This policy selects the pods labelled app=api and allows TCP 8080 only from pods labelled role=frontend in the same namespace; every other connection to api is dropped. Policies act on pod traffic after kube-proxy has translated the Service address, so ports lists the container port 8080, not the Service port 80, a very common mistake. Dropped packets are not rejected: clients see a connection timeout rather than connection refused, a useful signature when debugging. Source labels are evaluated continuously, so labelling or relabelling a pod changes what it may reach within moments, without restarting anything.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-allow-frontend
spec:
podSelector:
matchLabels:
app: api
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
role: frontend
ports:
- protocol: TCP
port: 8080The top-level podSelector chooses the protected pods; the from entry chooses who may connect. policyTypes: [Ingress] makes the intent explicit and leaves egress alone. A from podSelector without a namespaceSelector only matches pods in the policy's own namespace, so a role=frontend pod in another namespace is still blocked.
podSelector, namespaceSelector and ipBlock
Inside one from entry, a namespaceSelector and a podSelector together mean AND: pods with these labels in namespaces with those labels. Written as two separate list items they mean OR, which opens far more than intended, and the YAML differs by a single dash. Every namespace carries the label kubernetes.io/metadata.name with its own name, set by the API server, so you can select a namespace by name without labelling it yourself; an empty namespaceSelector {} means every namespace. ipBlock allows CIDR ranges, with optional except entries, and is meant for addresses outside the cluster, such as an office network. Pod IPs are ephemeral, and source NAT by a load balancer or by externalTrafficPolicy: Cluster may replace the client address you expected to match.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-allow-monitoring
spec:
podSelector:
matchLabels:
app: api
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: monitoring
podSelector:
matchLabels:
app: prometheus
ports:
- port: 8080namespaceSelector and podSelector sit in the same list item, under one dash, so only pods labelled app=prometheus in the monitoring namespace may connect. Put a dash before podSelector and the rule would admit every pod in monitoring plus every app=prometheus pod in the api's own namespace.
Default deny, egress and DNS
A namespace baseline usually starts with one policy whose podSelector is {}, selecting every pod, and no allow rules, which isolates every pod in the namespace; each app then gets small allow policies on top. Egress isolation catches people out: a pod that may not send to kube-dns cannot resolve any name, so every call fails with a DNS error long before it reaches the policy you meant to test. The baseline below therefore allows UDP and TCP 53 to the CoreDNS pods. Put defaults in place when a namespace is created; adding deny-all to a running namespace breaks every flow nobody wrote down. The GopherForge lab stores egress rules but enforces only ingress rules, so practise egress on a real cluster.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
podSelector:
matchLabels:
k8s-app: kube-dns
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53One policy, two effects. podSelector {} with both policyTypes isolates every pod in the namespace in both directions, and with no ingress rules nothing may connect in. The single egress rule allows DNS to pods labelled k8s-app=kube-dns in kube-system over UDP and TCP, because large answers fall back to TCP. Every other connection now needs its own allow policy, on the client side and on the server side.
The network plugin does the enforcing
The API server validates and stores NetworkPolicies but enforces nothing. The CNI plugin does, by programming iptables, nftables or eBPF on every node. Calico, Cilium and Antrea enforce policies; Flannel on its own does not, and on such a cluster a policy is accepted and has no effect, which is dangerous because it looks like protection. Managed clusters often need enforcement switched on explicitly, through a network policy option or add-on. kind has enforced standard NetworkPolicies out of the box since v0.24 through the kube-network-policies project, and the GopherForge lab models that enforcement for ingress rules. Always prove a policy with a negative test, a connection that must fail, and not only with one that must succeed.
Try it in the lab terminal
Run two api pods, which are gopherforge/web listening on 8080, and a curl client pod without any role label. Call the api before any policy exists, apply the allow-list from above and watch the same call time out, then give the client the label role=frontend and call again. Nothing restarts along the way: the policy and the label change, and the decision follows them. --max-time 5 keeps the blocked call from hanging.
kind create cluster
kubectl create deployment api --image=gopherforge/web:v1 --replicas=2 --port=8080
kubectl expose deployment api --port=80 --target-port=8080
kubectl run client --image=curlimages/curl -- sleep 3600
kubectl rollout status deploy/api
kubectl wait --for=condition=Ready pod/client
kubectl exec client -- curl -sS --max-time 5 http://api/version
cat > api-policy.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-allow-frontend
spec:
podSelector:
matchLabels:
app: api
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
role: frontend
ports:
- protocol: TCP
port: 8080
EOF
kubectl apply -f api-policy.yaml
kubectl get networkpolicy
kubectl exec client -- curl -sS --max-time 5 http://api/version
kubectl label pod client role=frontend
kubectl exec client -- curl -sS --max-time 5 http://api/version
kubectl get pods --show-labelsBefore any policy the client gets v1 from http://api/version. After the apply, get networkpolicy lists api-allow-frontend with POD-SELECTOR app=api, and the same curl ends with "curl: (28) Connection timed out after 5001 milliseconds": the packets are dropped, not refused. kubectl label pod client role=frontend changes only the source's labels, not the policy, and the next curl prints v1 again. get pods --show-labels shows role=frontend,run=client on the client and no role label on the api pods.
TAKE THIS WITH YOU
Pods are open until a policy selects them; after that only the union of the allow rules gets through. Start namespaces with default deny plus DNS egress, write ports as container ports, and prove every policy with a connection that must time out.
Check your understanding
A policy allows role=frontend pods into app=api pods. A frontend pod in another namespace, also labelled role=frontend, still times out. Why?
Go to the source
Progress stays in this browser. No account needed.