Sub topic 12
Building a default-deny baseline
This chapter assumes you know how pods, labels, and namespaces work, and that your cluster runs a network plugin that enforces NetworkPolicy. You should also know how a policy isolates the pods it selects.
Why start from deny
Pods accept all traffic until a policy selects them. A default-deny policy flips that. Every pod in the namespace becomes isolated, and you then add allow rules for the flows the application needs. Traffic you forgot to think about stays blocked.
Deny everything in a namespace
An empty podSelector selects every pod in the namespace. Listing both policy types with no rules means nothing is allowed in either direction.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
namespace: demo
spec:
podSelector: {}
policyTypes:
- Ingress
- EgressApply it with kubectl apply -f default-deny-all.yaml. The policy only affects the demo namespace. Other namespaces keep their current behavior.
Add allow rules on top
Policies are additive. The deny policy stays in place, and each new policy adds permitted traffic. This policy lets app=frontend pods reach app=backend pods on TCP 8080.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-to-backend
namespace: demo
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 8080Under full deny, this rule is not enough on its own. The frontend pods are also egress-isolated, so you need an egress rule on them that allows traffic to the backend. A connection must be allowed at both ends: by the sender's egress and by the receiver's ingress.
Do not forget DNS
Once egress is denied, pods can no longer resolve service names. This policy allows DNS queries to the cluster DNS pods in kube-system.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns-egress
namespace: demo
spec:
podSelector: {}
policyTypes:
- 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: 53The selectors share one list item, so both must match (AND). DNS uses UDP by default and falls back to TCP for large responses, so allow both.
Verify the baseline
Start a temporary pod with the label that your allow rule expects, and request the backend. The -T 3 flag sets a 3-second timeout, so a blocked connection fails quickly.
kubectl run test --rm -it --image=busybox:1.36 --restart=Never --labels=app=frontend -n demo -- wget -qO- -T 3 http://backend:8080If egress from the frontend is still denied, this command times out even though the backend allows the connection. Change the label or remove the allow policy to confirm the policy is what changes the result.
Common mistakes
- Applying the policy in the wrong namespace, so it selects nothing useful.
- Running on a network plugin that does not enforce policies. The object is accepted and has no effect.
- Denying egress without a DNS rule.
- Allowing ingress on the receiver but forgetting egress on the sender.
Exercise
After you apply default-deny-all in the demo namespace, a frontend pod can no longer reach backend:8080 even though allow-frontend-to-backend exists. Name two things you must still allow.
Show answer
The frontend pods need an egress rule permitting traffic to the backend pods on TCP 8080, and they need DNS egress (such as allow-dns-egress) so the name "backend" resolves. The ingress rule on the backend is already in place.