securityJuly 14, 2026•4 min read
Kubernetes Network Policies - Complete Security Guide | DevOps Duoo
Kubernetes network policies are a crucial component of securing your cluster. In this guide, you'll learn how to implement network policies to restrict ...
D
DevOps Duoo
DevOps Expert
Kubernetes Network Policies - Complete Security Guide
TL;DR
- Implement Kubernetes network policies to secure pod-to-pod communication and enforce a zero-trust security model.
- Use network policy tools like Calico (v3.20.2) or Cilium (v1.11.5) to define and enforce network policies.
- Follow best practices to avoid common mistakes and ensure optimal performance.
What You'll Learn
Kubernetes network policies are a crucial component of securing your cluster. In this guide, you'll learn how to implement network policies to restrict pod-to-pod communication, allow incoming traffic, and enforce a zero-trust security model. We'll cover the basics of network policies, provide step-by-step instructions for implementation, and discuss common mistakes to avoid.Understanding Network Policies
Kubernetes network policies are used to control traffic flow between pods. By default, pods can communicate with each other without restrictions. Network policies allow you to define rules for incoming and outgoing traffic, ensuring that only authorized pods can communicate with each other.Network Policy Components
A network policy consists of the following components:podSelector: specifies the pods to which the policy appliesingress: defines the incoming traffic rulesegress: defines the outgoing traffic rules
Example Network Policy
Here's an example network policy that allows incoming traffic from pods with the labelapp: frontend:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-traffic
spec:
podSelector:
matchLabels:
app: backend
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- 80
This policy applies to pods with the label app: backend and allows incoming traffic on port 80 from pods with the label app: frontend.Implementing Network Policies with Calico
Calico is a popular network policy tool for Kubernetes. To implement network policies with Calico, follow these steps:kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.20.2/manifests/calico.yaml
kubectl apply -f - <<EOF
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-traffic
spec:
podSelector:
matchLabels:
app: backend
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- 80
EOF
kubectl get networkpolicy allow-frontend-traffic -o yamlImplementing Network Policies with Cilium
Cilium is another popular network policy tool for Kubernetes. To implement network policies with Cilium, follow these steps:kubectl apply -f https://raw.githubusercontent.com/cilium/cilium/v1.11.5/install/kubernetes/quick-install.yaml
kubectl apply -f - <<EOF
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-traffic
spec:
podSelector:
matchLabels:
app: backend
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- 80
EOF
kubectl get networkpolicy allow-frontend-traffic -o yamlCommon Mistakes and Troubleshooting
When implementing network policies, be aware of the following common mistakes:- Insufficient pod selectors: Make sure to specify the correct pod selectors to avoid applying the policy to the wrong pods.
- Incorrect port numbers: Double-check the port numbers to ensure that the policy allows traffic on the correct ports.
- Policy ordering: Be aware of the policy ordering, as policies are evaluated in a specific order.
kubectl get networkpolicyto list all network policieskubectl describe networkpolicyto describe a specific network policykubectl logsto check the logs of the Calico or Cilium pods
Key Takeaways
- Implement Kubernetes network policies to secure pod-to-pod communication and enforce a zero-trust security model.
- Use network policy tools like Calico (v3.20.2) or Cilium (v1.11.5) to define and enforce network policies.
- Follow best practices to avoid common mistakes and ensure optimal performance.
- Use the
kubectlcommand to create, verify, and troubleshoot network policies. - For more information on Kubernetes network policies, see and .