NetworkPolicy in practice
Every tenant namespace starts with the platform’s default-deny ingress policy and its four allow rules, described in Network model. This page covers the policies you write yourself.
When you need your own NetworkPolicy
Section titled “When you need your own NetworkPolicy”- You want to restrict a pod’s outbound traffic. Egress is unrestricted until a policy with
policyTypes: [Egress]selects the pod; from then on, the pod can send only what the selecting policies allow. - A pod must accept traffic from another tenant’s namespace. Add an ingress policy in your namespace that admits it; see the example below. The other tenant needs nothing unless it restricts its own egress.
You can’t use an ingress policy to narrow what the platform already allows. Policies are additive, and the platform policy admits traffic from your own namespace, your other tenant namespaces, Traefik and monitoring, on all ports. To limit traffic between your own pods, restrict egress on the sending side.
Where a policy lives
Section titled “Where a policy lives”- Ingress policies go in the destination namespace and select the destination pods.
- Egress policies go in the source namespace and select the source pods.
Example: allow egress to an external CIDR
Section titled “Example: allow egress to an external CIDR”This policy lets pods labelled app: data-loader reach 203.0.113.0/24 on port 443. Every egress policy must also allow DNS with a ports-only rule (no to:), because pods resolve through NodeLocal DNS on the node’s host network, which a rule selecting k8s-app: kube-dns pods doesn’t match.
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: allow-egress-external-api namespace: <tenant>-prodspec: podSelector: matchLabels: app: data-loader policyTypes: - Egress egress: # DNS: ports only, any destination - ports: - protocol: UDP port: 53 - protocol: TCP port: 53 # The external API - to: - ipBlock: cidr: 203.0.113.0/24 ports: - protocol: TCP port: 443Example: allow traffic from another tenant
Section titled “Example: allow traffic from another tenant”This policy, in your namespace, admits traffic from the namespace <other-tenant>-prod to pods labelled app: api on port 8080:
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: allow-from-other-tenant namespace: <tenant>-prodspec: podSelector: matchLabels: app: api policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: <other-tenant>-prod ports: - protocol: TCP port: 8080Debugging dropped traffic
Section titled “Debugging dropped traffic”kubectl get networkpolicy -n <tenant>-prod # default-deny is the platform policykubectl describe networkpolicy <policy-name> -n <tenant>-prodkubectl get pods -n <tenant>-prod -l <selector> # what a selector actually matcheskubectl get pods -n <tenant>-prod --show-labelskubectl exec -n <tenant>-prod <source-pod> -- curl -v --connect-timeout 5 http://<dest-service>:8080/In describe output, PodSelector: <none> means an empty selector, which selects every pod in the namespace.
A connection timeout means a policy dropped the packet. Connection refused means it reached the destination and nothing is listening there, which is a Service or application problem.
Common mistakes
Section titled “Common mistakes”| Mistake | Symptom | Fix |
|---|---|---|
Egress policy without a DNS rule, or with one that selects kube-dns pods | Selected pods can’t resolve names | Add the ports-only UDP/TCP 53 rule shown above |
namespaceSelector and podSelector in separate - items | Rule is wider than intended (OR instead of AND) | Put both under the same list item |
| Policy in the wrong namespace | Traffic still blocked | Ingress: destination namespace. Egress: source namespace |
Direction missing from policyTypes | Policy has no effect in that direction | List Ingress, Egress, or both |
| Label typo in selector | Selector matches zero pods | Compare kubectl get pods --show-labels against the policy |