By default, all pods in a Kubernetes cluster can talk to every other pod. Network policies restrict that — think of them as firewall rules for pods.
Prerequisites
You need a CNI plugin that supports network policies: - Calico (most popular) - Cilium - Weave Net - Antrea
Default kubenet and AWS VPC CNI (without add-on) do NOT enforce network policies.
Default Deny All
Start with deny-all, then whitelist:
# Deny all ingress in a namespace
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-ingress
namespace: production
spec:
podSelector: {} # Applies to ALL pods in namespace
policyTypes:
- Ingress# Deny all egress in a namespace
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-egress
namespace: production
spec:
podSelector: {}
policyTypes:
- Egress# Deny both
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all
namespace: production
spec:
podSelector: {}
policyTypes:
- Ingress
- EgressMaster this topic with hands-on labs
Go beyond reading — build real projects in sandboxed environments with expert video guidance.
Browse Courses →Allow Specific Traffic
Frontend → API
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-to-api
namespace: production
spec:
podSelector:
matchLabels:
app: api # Apply to api pods
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend # Allow from frontend pods
ports:
- protocol: TCP
port: 8080API → Database
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-api-to-db
namespace: production
spec:
podSelector:
matchLabels:
app: postgres
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: api
ports:
- protocol: TCP
port: 5432Cross-Namespace Policies
# Allow monitoring namespace to scrape metrics
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-prometheus
namespace: production
spec:
podSelector: {} # All pods
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
name: monitoring
podSelector:
matchLabels:
app: prometheus
ports:
- protocol: TCP
port: 9090Note: namespaceSelector and podSelector in the same from item means AND. Separate items mean OR:
ingress:
# AND — pods matching BOTH selectors
- from:
- namespaceSelector:
matchLabels:
name: monitoring
podSelector:
matchLabels:
app: prometheus
# OR — pods matching EITHER selector
- from:
- namespaceSelector:
matchLabels:
name: monitoring
- podSelector:
matchLabels:
app: prometheusEgress Policies
Allow DNS
Almost always needed when using egress deny:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
namespace: production
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to: []
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53Allow External API Access
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-external-api
namespace: production
spec:
podSelector:
matchLabels:
app: api
policyTypes:
- Egress
egress:
# DNS
- ports:
- protocol: UDP
port: 53
# HTTPS to external APIs
- to:
- ipBlock:
cidr: 0.0.0.0/0
except:
- 10.0.0.0/8 # Block internal network
- 172.16.0.0/12
- 192.168.0.0/16
ports:
- protocol: TCP
port: 443Get weekly IT automation tips
Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.
Subscribe Free →Complete Three-Tier Example
---
# 1. Default deny all
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all
namespace: app
spec:
podSelector: {}
policyTypes: [Ingress, Egress]
---
# 2. Allow DNS for all
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
namespace: app
spec:
podSelector: {}
policyTypes: [Egress]
egress:
- ports:
- protocol: UDP
port: 53
---
# 3. Frontend: ingress from internet, egress to API
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: frontend-policy
namespace: app
spec:
podSelector:
matchLabels:
tier: frontend
policyTypes: [Ingress, Egress]
ingress:
- ports:
- port: 3000
egress:
- to:
- podSelector:
matchLabels:
tier: api
ports:
- port: 8080
---
# 4. API: ingress from frontend, egress to database
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-policy
namespace: app
spec:
podSelector:
matchLabels:
tier: api
policyTypes: [Ingress, Egress]
ingress:
- from:
- podSelector:
matchLabels:
tier: frontend
ports:
- port: 8080
egress:
- to:
- podSelector:
matchLabels:
tier: database
ports:
- port: 5432
---
# 5. Database: ingress from API only
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: database-policy
namespace: app
spec:
podSelector:
matchLabels:
tier: database
policyTypes: [Ingress]
ingress:
- from:
- podSelector:
matchLabels:
tier: api
ports:
- port: 5432Testing
# Test connectivity
kubectl exec frontend-pod -- curl -sf http://api:8080/health
kubectl exec frontend-pod -- curl -sf http://postgres:5432 # Should fail
# Debug with netshoot
kubectl run test --rm -it --image=nicolaka/netshoot -n app -- /bin/bash
# Inside: curl, nslookup, traceroute, tcpdumpWhat's Next?
Our SELinux for System Admins course covers OS-level network security. Docker Fundamentals teaches container networking basics. First lessons are free. -e ---
Ready to go deeper? Explore our hands-on DevOps courses — from Docker and Terraform to MLflow on Kubernetes.
Ready to learn by doing?
Stop reading tutorials — start building. Expert video courses with hands-on labs in real sandboxed environments.
Related Articles
Kubernetes RBAC for Beginners
Kubernetes RBAC from scratch. Roles, ClusterRoles, RoleBindings, ServiceAccounts, and practical examples for access control.
Kubernetes Namespaces Multi-Tenancy
Kubernetes multi-tenancy with namespaces. Resource quotas, limit ranges, network policies, and RBAC for isolating teams.
Kubernetes Pod Security Standards
Kubernetes Pod Security Standards for workload hardening. Privileged, Baseline, and Restricted profiles with YAML and migration tips.
Kubernetes Operators Explained
Kubernetes Operators for DevOps teams. CRDs, the controller pattern, popular community operators, and when to build your own.
Kubernetes Pod Troubleshooting
Debug Kubernetes pods systematically. Fix CrashLoopBackOff, ImagePullBackOff, pending pods, and OOMKilled with diagnostic commands.
Kubernetes Probes Liveness Readiness
Configure Kubernetes liveness, readiness, and startup probes. HTTP, TCP, exec checks with proper timing and misconfiguration fixes.
Explore topics
Browse more articles on the topics covered here.