Kubernetes Network Policies: Isolation Examples and Best Practices
In Kubernetes, the default connectivity is surprisingly open: every Pod can communicate with every other Pod, in any namespace, unless blocked externally. This makes development very fast, but introduces significant risks for security and compliance. Network Policies fill this gap, allowing you to define granular rules for who can talk to whom, on which ports, and in which directions. In this article, you’ll learn how they work, how to set up effective isolation policies, and which best practices to adopt to avoid incidents and simplify maintenance.
What a NetworkPolicy is and how it works
A NetworkPolicy is a Kubernetes resource that defines traffic rules at the Pod level. You can filter ingress and egress traffic based on label selectors for Pods and Namespaces, ports, and protocols. Policies are additive: applying multiple policies to the same Pod combines the allowed rules. If there is at least one policy that selects a Pod for Ingress, then that Pod becomes “isolated” for ingress; the same applies to egress. Without any policy, traffic is allowed by default.
It is essential to know that Network Policies apply to Pods and require a CNI that supports them (Calico, Cilium, Weave Net, and others). They do not directly regulate traffic to hostNetwork resources, nor do they apply to nodes or to Services per se (they act on traffic destined to the selected Pods).
Mental model and key components
To design good policies, think in three dimensions:
- Selectors: podSelector and namespaceSelector define the allowed peers. Use consistent and meaningful labels.
- Direction: policyTypes specifies whether you regulate Ingress, Egress, or both. Forgetting one leaves unexpected openings.
- Ports and protocols: restrict traffic to only the necessary ports/protocols. Named ports simplify maintenance.
Practical examples of isolation policies
Default namespace isolation (deny-all) with exceptions for DNS
A solid approach starts by blocking everything and allowing only what is needed. The two policies below fully isolate Pods in a namespace, first for Ingress and then for Egress. Afterwards, we allow Egress to DNS to enable name resolution.
1) Deny all Ingress in the namespace:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-ingress
namespace: app
spec:
podSelector: {}
policyTypes:
- Ingress
2) Deny all Egress in the namespace:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-egress
namespace: app
spec:
podSelector: {}
policyTypes:
- Egress
3) Allow Egress to DNS (CoreDNS in kube-system) on ports 53 UDP/TCP:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns-egress
namespace: app
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: 53
This triplet creates a minimal working perimeter: everything is blocked except name resolution.
Tier isolation: allow only traffic from frontend to backend
Typical scenario: you want Pods with label app=frontend to reach Pods with app=backend only on a specific port (for example gRPC on 8080), and no other Pod should be able to contact them. Create an Ingress policy that selects the backends and limits sources and ports.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: backend-ingress-from-frontend
namespace: app
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 8080
If the frontends are in a different namespace (for example frontend in the web namespace and backend in data), you can combine namespaceSelector and podSelector in the same “from” element to require both criteria:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: backend-ingress-from-web-frontend
namespace: data
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
name: web
podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 8080
Limit egress to external APIs with IPBlock
When you need to allow calls only to a set of external addresses, use ipBlock. Note: ipBlock applies to destinations outside the cluster.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-payment-egress-external
namespace: app
spec:
podSelector:
matchLabels:
app: payment
policyTypes:
- Egress
egress:
- to:
- ipBlock:
cidr: 203.0.113.0/24
except:
- 203.0.113.8/32
ports:
- protocol: TCP
port: 443
Testing and validation strategies
Before applying policies in production, test in a staging environment. Here are some practical actions:
- List and verify the active policies in the namespace: kubectl get networkpolicy -n app
- Check the Pod/Namespace labels to ensure selectors match what you intend.
- Use a debug Pod (for example netshoot or busybox) to attempt allowed and disallowed connections: curl, nc, dig.
Examples of useful commands:
kubectl -n app get pods -o wide kubectl -n kube-system get pods -l k8s-app=kube-dns kubectl -n app run tester --rm -it --image=nicolaka/netshoot -- bash # From the tester Pod: dig kubernetes.default.svc.cluster.local nc -vz backend.app.svc.cluster.local 8080 nc -vz 203.0.113.10 443
Best practices for effective Network Policies
- Set a default deny for Ingress and Egress on every application namespace; allow only what is needed.
- Design for the principle of least privilege: restrict sources, destinations, ports, and protocols.
- Consistent and meaningful labels: define standards (for example app, tier, env, team) and enforce them in CI/CD.
- Prefer Pod/Namespace selectors over ipBlock for intra-cluster traffic; use ipBlock only for external destinations.
- Use named ports in Pods to avoid drift when numeric ports change.
- Minimal infrastructure permissions: remember to allow DNS and other essential services (e.g., time servers, corporate proxies) if needed.
- Verify CNI support and available features (some plugins have extensions such as L7-level policies).
- Version policies alongside application code and include automated network tests in the pipeline.
- Roll out gradually: start with non-critical environments, monitor, then expand coverage.
- Do not rely on Services for security: policies filter at the Pod level, not at the Service level.
- Beware of Pods with hostNetwork: they are not protected by Network Policies.
- Audit and visibility: use CNI observability tools (e.g., flow logs) to understand what is allowed or blocked.
Common mistakes and how to avoid them
- Forgetting policyTypes: if you specify only Ingress but expect to block Egress, it will remain open.
- Mismatched selectors: a misspelled label is equivalent to a wide-open door. Automate label application.
- Policy seemingly “not working”: remember that policies are additive. Another policy might allow what you intended to block.
- Focusing on the wrong ports: always verify the app’s actual ports/protocols (TCP vs UDP, named ports).
- Relying on static internal IPs: Pods are ephemeral; prefer selectors over IPs in-cluster.
Conclusions and next steps
Network Policies turn an open cluster into a finely segmented environment, more secure and governable. Starting from a deny-all and opening only what is needed, shaping rules around stable labels and a clear namespace design, will lead you to solid, long-lasting results. Integrate tests into the pipeline, observe flows, iterate and refine: network security will become an accelerator, not a brake.
Ready to secure your cluster? Start today: create a pilot namespace, apply the deny-all, add the minimal exceptions, and test critical flows. If you want hands-on support, contact us for a review of your policies and a tailored gradual adoption plan for your team.

Comments