How to Secure Kubernetes Pod Communication Using Network Policies with Ingress, Egress, and Namespace Selectors
Learning how to secure Kubernetes pod communication using network policies with ingress, egress, and namespace selectors is one of the most important skills for anyone running production workloads on Kubernetes. By default, Kubernetes allows all pods to communicate freely with each other. That open access creates serious security risks. Network policies give you precise control over which pods can send and receive traffic. In this tutorial, you will create and apply network policies that restrict pod-to-pod traffic using ingress rules, egress rules, and namespace selectors. You will walk away knowing how to lock down your cluster traffic without breaking your applications.
Prerequisites and Requirements for Securing Pod Communication
Before you start, make sure you have the following in place.
Required tools and access:
- A running Kubernetes cluster (v1.21 or later recommended)
kubectlinstalled and configured on your local machine- A CNI plugin that supports network policies , Calico, Cilium, or Weave Net all work. Note that the default Kubernetes CNI does not enforce network policies.
- Basic familiarity with YAML syntax
- Cluster admin permissions or a role that allows creating NetworkPolicy resources
Assumed knowledge: You should understand basic Kubernetes concepts like pods, namespaces, labels, and deployments. You don’t need to be an expert.
Estimated time: 30–45 minutes.
You can verify your CNI plugin supports network policies by checking the official Kubernetes network policy documentation. If you’re using a managed cluster like GKE or EKS, make sure you’ve enabled the correct CNI add-on before continuing.
Step-by-Step Guide to Configuring Network Policies with Ingress, Egress, and Namespace Selectors
For a related walkthrough, see: How to Automate Server Configuration with Ansible Playbooks on Ubuntu
Follow these steps carefully. Each step builds on the previous one.
Step 1: Create test namespaces and label them
You need two namespaces for this tutorial. Labels are critical , selectors use them to match namespaces.
kubectl create namespace app-ns
kubectl create namespace monitoring-ns
kubectl label namespace app-ns environment=production
kubectl label namespace monitoring-ns environment=monitoring
Labels make your selectors work. Without them, namespace selectors won’t match anything.
Step 2: Deploy a sample application pod
Deploy a simple nginx pod in the app-ns namespace.
kubectl run app-pod --image=nginx --namespace=app-ns --labels="app=webserver"
Also deploy a client pod in the same namespace for testing.
kubectl run client-pod --image=busybox --namespace=app-ns --restart=Never -- sleep 3600
Step 3: Apply a default deny-all policy
Start with a deny-all policy. This blocks all ingress and egress traffic to pods in app-ns by default.
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
namespace: app-ns
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
EOF
An empty podSelector matches all pods in the namespace. This is your security baseline. Always start here.
Step 4: Allow ingress from pods with a specific label
Now allow only pods labeled app=client to send traffic to your webserver pods.
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-client-ingress
namespace: app-ns
spec:
podSelector:
matchLabels:
app: webserver
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: client
ports:
- protocol: TCP
port: 80
EOF
This policy targets pods labeled app=webserver. It only permits TCP traffic on port 80 from pods labeled app=client. All other ingress traffic is still blocked.
Step 5: Allow egress to DNS and a specific namespace
Your pods need DNS resolution to function. You also want to allow traffic to pods in the monitoring-ns namespace.
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-egress-dns-and-monitoring
namespace: app-ns
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- ports:
- protocol: UDP
port: 53
- to:
- namespaceSelector:
matchLabels:
environment: monitoring
EOF
The first egress rule allows UDP port 53 traffic , that’s DNS. Without this, your pods can’t resolve hostnames. The second rule uses a namespaceSelector to allow traffic to any pod in namespaces labeled environment=monitoring.
Step 6: Combine podSelector and namespaceSelector
You can get more specific. This example allows egress only to pods labeled app=prometheus inside the monitoring-ns namespace.
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-egress-to-prometheus
namespace: app-ns
spec:
podSelector:
matchLabels:
app: webserver
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
environment: monitoring
podSelector:
matchLabels:
app: prometheus
ports:
- protocol: TCP
port: 9090
EOF
Note that namespaceSelector and podSelector are under the same list item. That means both conditions must match. If you put them in separate list items, Kubernetes treats them as an OR condition instead.
Step 7: Verify your policies are working
List all network policies in your namespace.
kubectl get networkpolicy -n app-ns
Test connectivity from the client pod to the webserver pod.
kubectl exec -n app-ns client-pod -- wget -qO- http://
Replace <webserver-pod-ip> with the actual pod IP from kubectl get pod -n app-ns -o wide. You should get a response. Traffic from any other pod should be blocked.
For a deeper look at testing network policies, check the Calico network policy guide.
Troubleshooting Common Network Policy Issues
Things don’t always work on the first try. Here are the most common problems and how to fix them.
Problem: Policies aren’t being enforced
Your CNI plugin probably doesn’t support network policies. Run kubectl get pods -n kube-system and confirm you see Calico, Cilium, or Weave Net pods running. The default kubenet plugin does not enforce policies.
Problem: DNS is broken after applying deny-all
You forgot to allow egress on UDP port 53. Apply the DNS egress rule from Step 5. Without it, pods can’t resolve service names or external hostnames.
Problem: namespaceSelector isn’t matching
Check that your namespace has the correct label. Run kubectl get namespace monitoring-ns --show-labels. If the label is missing, re-apply it with kubectl label namespace monitoring-ns environment=monitoring.
Problem: AND vs OR confusion with selectors
When namespaceSelector and podSelector are under the same - list item, Kubernetes applies AND logic. When they’re under separate - list items, it applies OR logic. This is one of the most common mistakes people make. Double-check your YAML indentation carefully.
Warning: Don’t apply a deny-all policy in production without first testing your allow rules in a staging environment. You can accidentally cut off critical services.
Conclusion: Putting Your Kubernetes Network Policies to Work
You now know how to secure Kubernetes pod communication using network policies with ingress, egress, and namespace selectors. You started with a deny-all baseline, then built targeted allow rules for specific pods and namespaces. You combined podSelector and namespaceSelector for fine-grained control. You also learned how to avoid the common AND/OR selector mistake.
From here, you can explore more advanced topics. Try applying policies across multiple namespaces. Look into using Cilium’s extended network policy features for layer 7 HTTP filtering. You can also automate policy creation using tools like Helm or GitOps workflows. Keeping your cluster traffic locked down is an ongoing process. Start strict and loosen only when you have
