Sub topic 11
Restricting traffic between namespaces
This chapter covers how NetworkPolicy limits traffic to and from Pods, and how to use namespace labels to allow traffic between namespaces. It assumes you know Pods, Services, and labels, and that you can read the namespace and label material from earlier chapters.
The default: everything can talk to everything
A new cluster has no traffic restrictions. A Pod in team-b can reach any Pod in team-a by IP, and by Service DNS name. Namespaces do not change this. They scope names, not network paths.
A NetworkPolicy is a namespaced object that selects Pods and states which traffic is allowed to or from them. Two rules shape everything else:
- A Pod that no policy selects accepts all traffic.
- Once any policy selects a Pod for a direction (ingress or egress), only traffic that some policy allows in that direction is permitted. Policies only add allowances, so they never conflict.
Step 1: deny all ingress in a namespace
An empty podSelector selects every Pod in the policy's namespace. Listing Ingress under policyTypes with no ingress rules means nothing is allowed in.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-ingress
namespace: team-a
spec:
podSelector: {}
policyTypes:
- IngressAfter you apply it, Pods in team-a reject incoming connections from every source, including Pods in the same namespace.
Step 2: allow traffic from another namespace
To admit traffic from team-b, add a second policy. It selects the source namespace with a namespaceSelector. Kubernetes automatically sets the label kubernetes.io/metadata.name on every namespace, with the namespace's name as its value. This makes it a dependable selector, because you do not need to label anything yourself.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-from-team-b
namespace: team-a
spec:
podSelector: {}
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: team-bBecause policies are additive, the deny policy and this one combine. Pods in team-a now accept traffic from team-b Pods and nothing else.
Step 3: test the result
Run a request from each namespace against a Service in team-a. The Service name web here is an example.
kubectl run probe --rm -it --image=busybox:1.36 --namespace team-b \
--restart=Never -- wget -qO- -T 3 http://web.team-a.svc.cluster.local
kubectl run probe --rm -it --image=busybox:1.36 --namespace team-c \
--restart=Never -- wget -qO- -T 3 http://web.team-a.svc.cluster.localThe first request should return a response. The second should time out. A blocked connection usually shows as a timeout, not as a refusal, because the packets are dropped.
Common errors
- Policy has no effect. The network plugin does not enforce NetworkPolicy.
- Everything is blocked, including DNS. Deny-all egress policies also block lookups to cluster DNS in
kube-system. If you add an egress deny, add a rule allowing DNS (port 53, UDP and TCP). - Wrong selector shape. In a
fromlist, anamespaceSelectorand apodSelectorin the same item (same-entry) are combined with AND. As separate items they are combined with OR. A misplaced dash changes who is allowed. - Policy placed in the wrong namespace. A policy protects Pods in its own namespace. To protect
team-a, create it inteam-a, not in the namespace the traffic comes from. - Typo in the label value. A selector that matches no namespace allows nothing, with no error.
Exercise
A cluster has a default-deny-ingress policy in team-a. You add a policy in team-b that has an ingress rule allowing traffic from team-a. Can a Pod in team-b now send requests to a Pod in team-a? Why or why not?
Show answer
No. The policy in team-b only controls ingress to Pods in team-b. Requests from team-b to team-a are ingress traffic for team-a, which the default-deny policy in team-a still blocks. You need an ingress policy in team-a that allows team-b, such as one that uses a namespaceSelector on kubernetes.io/metadata.name: team-b.