Skip to content

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.

  • 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.

  • Ingress policies go in the destination namespace and select the destination pods.
  • Egress policies go in the source namespace and select the source pods.

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/v1
kind: NetworkPolicy
metadata:
name: allow-egress-external-api
namespace: <tenant>-prod
spec:
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: 443

Example: 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/v1
kind: NetworkPolicy
metadata:
name: allow-from-other-tenant
namespace: <tenant>-prod
spec:
podSelector:
matchLabels:
app: api
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: <other-tenant>-prod
ports:
- protocol: TCP
port: 8080
Terminal window
kubectl get networkpolicy -n <tenant>-prod # default-deny is the platform policy
kubectl describe networkpolicy <policy-name> -n <tenant>-prod
kubectl get pods -n <tenant>-prod -l <selector> # what a selector actually matches
kubectl get pods -n <tenant>-prod --show-labels
kubectl 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.

MistakeSymptomFix
Egress policy without a DNS rule, or with one that selects kube-dns podsSelected pods can’t resolve namesAdd the ports-only UDP/TCP 53 rule shown above
namespaceSelector and podSelector in separate - itemsRule is wider than intended (OR instead of AND)Put both under the same list item
Policy in the wrong namespaceTraffic still blockedIngress: destination namespace. Egress: source namespace
Direction missing from policyTypesPolicy has no effect in that directionList Ingress, Egress, or both
Label typo in selectorSelector matches zero podsCompare kubectl get pods --show-labels against the policy