Tutorials · DevSecOps & Cloud
How to Isolate Kubernetes Workloads With Network Policies
By default every pod in a Kubernetes cluster can talk to every other pod. Network policies are the cluster's firewall: deny by default, then allow only the flows each application needs.
Before you start
- A Kubernetes cluster with a CNI that enforces NetworkPolicy (Calico, Cilium and others)
- kubectl access to a namespace
Step by step
Deny all traffic by default
Apply a default-deny policy in each application namespace.
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: default-deny namespace: shop spec: podSelector: {} policyTypes: [Ingress, Egress]
Allow DNS
Pods need DNS to find services; allow egress to kube-dns.
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: { name: allow-dns, namespace: shop } spec: podSelector: {} policyTypes: [Egress] egress: - to: - namespaceSelector: {} podSelector: { matchLabels: { k8s-app: kube-dns } } ports: [{ protocol: UDP, port: 53 }, { protocol: TCP, port: 53 }]
Allow the application flow
Let only the web pods reach the API pods on the API port.
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: { name: web-to-api, namespace: shop } spec: podSelector: { matchLabels: { app: api } } ingress: - from: - podSelector: { matchLabels: { app: web } } ports: [{ protocol: TCP, port: 8080 }]
Apply and test
Apply the files and test from a temporary pod that allowed traffic works and everything else times out.
kubectl apply -f policies/ kubectl -n shop run t --rm -it --image=busybox -- wget -qO- -T 3 http://api:8080/health
Control egress to the outside
Allow outbound traffic only to the services the app needs (database, payment API); block the rest to stop data exfiltration.
Commands are generic, industry-standard examples. Exact syntax and menu names vary by model and firmware; check your device's manual on the Documentation page.
Pro tips
- Label pods consistently (app, tier) so policies stay readable.
- Keep policies in Git next to the application manifests.
FAQ
Frequently asked questions
My policies have no effect. Why?
The CNI plugin does not enforce NetworkPolicy. Check that your cluster uses Calico, Cilium or another enforcing plugin.
Keep learning
Related guides
Write firewall rules that protect
Read more →Start zero trust networking
Read more →Scan IaC with Trivy
Read more →How to Scan Container Images, Code and IaC for Vulnerabilities With Trivy
Trivy is a free scanner for container images, file systems, Git repositories and infrastructure-as-code. Find vulnerable packages and misconfigurations before they reach production.
Read more →How to Generate an SBOM for Your Software and Scan It With Grype
A Software Bill of Materials lists every component in your software. Customers and regulations (such as the EU Cyber Resilience Act) increasingly ask for it. Generate SBOMs with Syft and check them for vulnerabilities with Grype.
Read more →How to Add Security Checks to a CI/CD Pipeline (Secrets, SAST, Dependencies, Images)
Shift security left: catch leaked secrets, insecure code, vulnerable dependencies and risky infrastructure settings automatically on every commit, before anything is deployed.
Read more →Stuck on a step?
Ask Navijin technical support or an authorised partner for help with your configuration.