Kubernetes_network_policies
Kubernetes Network Policies
Section titled “Kubernetes Network Policies”Overview
Section titled “Overview”Network Policies are Kubernetes resources that control the traffic between pods and network endpoints. By default, all pods can communicate with each other (flat network), but Network Policies allow you to implement zero-trust security by explicitly defining which pods can communicate.
Why Network Policies?
Section titled “Why Network Policies?”Without Network Policies:
- Any pod can access any other pod
- Lateral movement is easy for attackers
- No isolation between namespaces
- Compliance requirements cannot be met
With Network Policies:
- Pods only receive traffic they’re authorized to receive
- Defense in depth strategy
- Namespace isolation
- Compliance with security standards (PCI-DSS, SOC2, etc.)
Network Policy Concepts
Section titled “Network Policy Concepts”┌─────────────────────────────────────────────────────────────────┐│ Network Policy Model ││ ││ ┌──────────┐ ││ │ Ingress │ ──▶ Controls incoming traffic TO a pod ││ │ Rules │ ││ └──────────┘ ││ ││ ┌──────────┐ ││ │ Egress │ ──▶ Controls outgoing traffic FROM a pod ││ │ Rules │ ││ └──────────┘ ││ ││ ┌─────────────────────────────────────────────────────────┐ ││ │ podSelector: Which pods this policy applies to │ ││ │ namespaceSelector: Which namespaces │ ││ │ ipBlock: CIDR ranges for external traffic │ ││ └─────────────────────────────────────────────────────────┘ │└─────────────────────────────────────────────────────────────────┘Basic Network Policy
Section titled “Basic Network Policy”Deny all ingress traffic to selected pods:
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: default-deny-ingress namespace: productionspec: podSelector: matchLabels: {} # Empty = all pods in namespace policyTypes: - IngressThis is the “deny by default” approach - pods won’t receive any traffic until explicitly allowed.
Allow Specific Ingress
Section titled “Allow Specific Ingress”Allow traffic from specific pods:
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: allow-api-from-webspec: podSelector: matchLabels: app: api policyTypes: - Ingress ingress: - from: - podSelector: matchLabels: app: web ports: - protocol: TCP port: 8080This allows pods with label app: api to receive traffic on port 8080 from pods with label app: web.
Allow from Namespace
Section titled “Allow from Namespace”Allow traffic from specific namespace:
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: allow-from-monitoringspec: podSelector: matchLabels: app: myapp policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: name: monitoring ports: - protocol: TCP port: 9090Allow from Multiple Sources
Section titled “Allow from Multiple Sources”Combine multiple sources with AND/OR logic:
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: allow-from-multiplespec: podSelector: matchLabels: app: api policyTypes: - Ingress ingress: # Allow from web pods OR from monitoring namespace - from: - podSelector: matchLabels: app: web - namespaceSelector: matchLabels: name: monitoring ports: - protocol: TCP port: 8080Egress Policies
Section titled “Egress Policies”Control outgoing traffic:
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: api-egress-policyspec: podSelector: matchLabels: app: api policyTypes: - Egress egress: # Allow DNS - to: - namespaceSelector: {} ports: - protocol: UDP port: 53 - protocol: TCP port: 53 # Allow to database namespace - to: - namespaceSelector: matchLabels: name: database ports: - protocol: TCP port: 5432Complete Example: Three-Tier Application
Section titled “Complete Example: Three-Tier Application”Frontend Policy
Section titled “Frontend Policy”apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: frontend-policyspec: podSelector: matchLabels: tier: frontend policyTypes: - Ingress - Egress ingress: # Allow from ingress controller / load balancer - from: - namespaceSelector: matchLabels: name: ingress-nginx egress: # Allow to backend - to: - podSelector: matchLabels: tier: backend ports: - protocol: TCP port: 8080 # Allow DNS - to: - namespaceSelector: {} ports: - protocol: UDP port: 53Backend Policy
Section titled “Backend Policy”apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: backend-policyspec: podSelector: matchLabels: tier: backend policyTypes: - Ingress - Egress ingress: # Allow from frontend only - from: - podSelector: matchLabels: tier: frontend ports: - protocol: TCP port: 8080 egress: # Allow to database - to: - podSelector: matchLabels: tier: database ports: - protocol: TCP port: 5432 # Allow DNS - to: - namespaceSelector: {} ports: - protocol: UDP port: 53Database Policy
Section titled “Database Policy”apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: database-policyspec: podSelector: matchLabels: tier: database policyTypes: - Ingress - Egress ingress: # Allow from backend only - from: - podSelector: matchLabels: tier: backend ports: - protocol: TCP port: 5432 egress: []IP Block for External Traffic
Section titled “IP Block for External Traffic”Control traffic to/from external IP ranges:
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: allow-external-apispec: podSelector: matchLabels: app: api policyTypes: - Egress egress: # Allow to specific external IP ranges (e.g., external API) - to: - ipBlock: cidr: 203.0.113.0/24 except: - 203.0.113.1/32 ports: - protocol: TCP port: 443 # Allow DNS - to: - namespaceSelector: {} ports: - protocol: UDP port: 53Using port and endPort
Section titled “Using port and endPort”Specify a range of ports:
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: allow-http-portsspec: podSelector: matchLabels: app: webserver policyTypes: - Ingress ingress: - ports: - protocol: TCP port: 80 endPort: 8080 # Allows ports 80-8080Note: endPort requires the CNI plugin to support it.
Default Policies
Section titled “Default Policies”It’s common to apply these two policies to start with zero-trust:
# Deny all ingressapiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: default-deny-ingressspec: podSelector: {} policyTypes: - Ingress---# Deny all egressapiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: default-deny-egressspec: podSelector: {} policyTypes: - EgressThen add specific allow policies as needed.
Network Policy with Cilium
Section titled “Network Policy with Cilium”Cilium supports additional features:
apiVersion: cilium.io/v2kind: NetworkPolicymetadata: name: cilium-http-policyspec: endpointSelector: matchLabels: app: api ingress: - fromEndpoints: - matchLabels: app: web toPorts: - ports: - port: "8080" protocol: TCP rules: http: - method: GET path: /api/v1.*Supported CNI Plugins
Section titled “Supported CNI Plugins”Not all CNI plugins support Network Policies:
| CNI Plugin | Ingress | Egress | Notes |
|---|---|---|---|
| Calico | ✅ | ✅ | Full support, rich policy |
| Cilium | ✅ | ✅ | Full support, L7 policy |
| Canal | ✅ | ✅ | Uses Calico policy engine |
| Weave | ✅ | ✅ | Full support |
| Kube-router | ✅ | ✅ | Full support |
| Flannel | ❌ | ❌ | No network policy support |
| AWS VPC CNI | ❌ | ❌ | Use Security Groups instead |
Debugging Network Policies
Section titled “Debugging Network Policies”# Check if network policy is appliedkubectl get networkpolicy
# Describe network policykubectl describe networkpolicy <name>
# Check pod network statuskubectl get pods -o wide
# Test connectivity between podskubectl exec -it <source-pod> -- wget -O- <target-pod>:<port>
# View NetworkPolicy logs (Cilium)kubectl logs -n kube-system -l k8s-app=cilium-operatorBest Practices
Section titled “Best Practices”- Start with deny-all: Apply default deny policies first
- Use namespace isolation: Separate workloads into namespaces
- Implement least privilege: Only allow necessary traffic
- Document policies: Comment on why each policy exists
- Use labels consistently: Organize pods by tier, environment
- Test thoroughly: Verify policies work as expected
- Monitor and iterate: Adjust based on observed traffic
Summary
Section titled “Summary”Network Policies are essential for:
- Zero-trust security: Assume breach, deny by default
- Compliance: Meet regulatory requirements
- Microsegmentation: Isolate workloads
- Defense in depth: Layer security controls
Key concepts:
podSelector: Selects pods to apply the policy toingress: Controls incoming trafficegress: Controls outgoing trafficnamespaceSelector: Selects pods in specific namespacesipBlock: Controls traffic to/from CIDR ranges