Kubernetes Network Security
A comprehensive guide to Kubernetes network security covering network policies, zero-trust networking, mTLS, firewall, egress filtering, and practical implementation strategies for securing cluster communication.
Network security is a critical component of Kubernetes security. Without proper network controls, an attacker who compromises one pod can potentially access other pods and services within the cluster. Network security in Kubernetes includes:
- Network Policies: Control pod-to-pod communication
- Zero-Trust Networking: Never trust, always verify
- mTLS: Encrypt and authenticate service-to-service communication
- Egress Filtering: Control outbound traffic from the cluster
- Service Mesh: Advanced networking with mTLS and traffic management
Network Policies are Kubernetes resources that define how pods can communicate with each other and with other network endpoints. They provide network segmentation and enforce the principle of least privilege at the network level.
# Default Deny All Ingress
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-ingress
spec:
podSelector: {} # Applies to all pods
policyTypes:
- Ingress
# Default Deny All Egress
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-egress
spec:
podSelector: {}
policyTypes:
- Egress
# Allow Ingress from Same Namespace
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-namespace-ingress
spec:
podSelector:
matchLabels:
app: backend
ingress:
- from:
- podSelector: {} # All pods in same namespace
ports:
- port: 8080
policyTypes:
- Ingress
# Allow Ingress from Specific Namespace
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-from-monitoring
spec:
podSelector:
matchLabels:
app: backend
ingress:
- from:
- namespaceSelector:
matchLabels:
name: monitoring
ports:
- port: 8080
policyTypes:
- Ingress
# Allow Ingress from Specific Pod with Label
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-from-frontend
spec:
podSelector:
matchLabels:
app: backend
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- port: 8080
protocol: TCP
policyTypes:
- Ingress
# Allow Egress to External Database
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-egress-db
spec:
podSelector:
matchLabels:
app: my-app
egress:
- to:
- ipBlock:
cidr: 10.0.0.0/24
ports:
- port: 5432
protocol: TCP
policyTypes:
- Egress
# Combined Ingress + Egress Policy
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: combined-policy
spec:
podSelector:
matchLabels:
app: my-app
ingress:
- from:
- namespaceSelector:
matchLabels:
name: frontend
ports:
- port: 8080
egress:
- to:
- ipBlock:
cidr: 10.0.0.0/24
ports:
- port: 5432
policyTypes:
- Ingress
- Egress
Zero-trust networking assumes that no network is inherently safe. Every request must be authenticated, authorized, and encrypted. In Kubernetes, zero-trust is achieved through a combination of:
mTLS Authentication
Authorization Policies
Network Segmentation
Observability
# Istio Authorization Policy (Zero-Trust)
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: zero-trust-policy
namespace: default
spec:
selector:
matchLabels:
app: backend
action: ALLOW
rules:
- from:
- source:
principals: ["cluster.local/ns/default/sa/frontend-sa"]
namespaces: ["default"]
to:
- operation:
methods: ["GET", "POST"]
paths: ["/api/*"]
- from:
- source:
principals: ["cluster.local/ns/monitoring/sa/prometheus-sa"]
to:
- operation:
methods: ["GET"]
paths: ["/metrics"]
# Linkerd Authorization Policy
apiVersion: policy.linkerd.io/v1beta1
kind: Server
metadata:
name: backend-server
namespace: default
labels:
app: backend
spec:
podSelector:
matchLabels:
app: backend
port: 8080
proxyProtocol: HTTP/1.1
---
apiVersion: policy.linkerd.io/v1beta1
kind: ServerAuthorization
metadata:
name: backend-auth
namespace: default
spec:
server:
name: backend-server
client:
meshTLS:
identities:
- frontend.default.serviceaccount.identity.linkerd.cluster.local
- prometheus.monitoring.serviceaccount.identity.linkerd.cluster.local
Mutual TLS (mTLS) is the foundation of secure service-to-service communication. It provides both encryption and authentication, ensuring that only authorized services can communicate.
# Istio mTLS Configuration
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
namespace: default
spec:
mtls:
mode: STRICT # STRICT, PERMISSIVE, DISABLE
# mTLS with DestinationRule
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: backend-dr
namespace: default
spec:
host: backend-service
trafficPolicy:
tls:
mode: ISTIO_MUTUAL # Enables mTLS
# Linkerd mTLS (enabled by default)
# Check mTLS status
linkerd viz authz deploy/backend
# Certificate Management with cert-manager
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
name: ca-issuer
namespace: istio-system
spec:
ca:
secretName: istio-ca-secret
# Service Mesh mTLS Flow
# 1. Service A requests Service B
# 2. Sidecar proxy (Envoy/Linkerd) intercepts request
# 3. Proxy establishes mTLS connection using certificates
# 4. Both proxies verify each other's certificates
# 5. Encrypted communication continues
- Enable mTLS for all service-to-service communication
- Use STRICT mode for production (enforce mTLS)
- Automate certificate management with Istio/Linkerd
- Monitor mTLS failures and certificate expiration
- Use service meshes for automatic mTLS management
Egress filtering controls what external resources pods can access. This is critical for preventing data exfiltration and limiting attack surfaces.
# NetworkPolicy for Egress Control
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-specific-egress
spec:
podSelector:
matchLabels:
app: my-app
egress:
- to:
- ipBlock:
cidr: 10.0.0.0/24 # Internal network
ports:
- port: 5432
protocol: TCP
- to:
- ipBlock:
cidr: 0.0.0.0/0 # Allow all external
except:
- 10.0.0.0/8 # Except internal
- 192.168.0.0/16
- 172.16.0.0/12
ports:
- port: 443
protocol: TCP
policyTypes:
- Egress
# Istio Egress Gateway
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
name: egress-gateway
namespace: istio-system
spec:
selector:
istio: egressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
---
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: egress-rule
namespace: istio-system
spec:
host: egress-gateway.istio-system.svc.cluster.local
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
# ServiceEntry for external services
apiVersion: networking.istio.io/v1beta1
kind: ServiceEntry
metadata:
name: external-db
spec:
hosts:
- db.example.com
ports:
- number: 5432
name: tcp
protocol: TCP
resolution: DNS
location: MESH_EXTERNAL
# Deny all egress by default
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-egress
spec:
podSelector: {}
policyTypes:
- Egress
# Allow DNS resolution
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
spec:
podSelector: {}
egress:
- to:
- ipBlock:
cidr: 10.96.0.0/12 # Cluster DNS range
ports:
- port: 53
protocol: UDP
- port: 53
protocol: TCP
policyTypes:
- Egress
- Deny all egress by default, then allow specific destinations
- Always allow DNS resolution (port 53) for service discovery
- Use IP blocks with
exceptto exclude internal networks - Test egress policies thoroughly before enforcing
- Monitor egress traffic for anomalies
Service meshes (Istio, Linkerd) provide advanced network security features beyond what's possible with Network Policies alone.
Automatic mTLS
Identity-Based Security
Observability
Security Policies
# Istio Authorization with mTLS
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: secure-backend
spec:
selector:
matchLabels:
app: backend
action: ALLOW
rules:
- from:
- source:
principals: ["cluster.local/ns/default/sa/frontend-sa"]
to:
- operation:
methods: ["GET", "POST"]
when:
- key: request.headers[Authorization]
values: ["Bearer *"]
# Rate Limiting (EnvoyFilter)
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: rate-limit
namespace: istio-system
spec:
workloadSelector:
labels:
app: backend
configPatches:
- applyTo: HTTP_FILTER
match:
context: SIDECAR_INBOUND
patch:
operation: INSERT_BEFORE
value:
name: envoy.filters.http.local_ratelimit
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.local_ratelimit.v3.LocalRateLimit
stat_prefix: http_local_rate_limiter
token_bucket:
max_tokens: 10
tokens_per_fill: 1
fill_interval: 1s
Network Policies
Encryption
Egress Control
Observability
- Start with default deny all policies
- Gradually allow specific traffic based on application requirements
- Enable mTLS with service mesh
- Implement egress controls
- Set up observability and alerting
- Regularly audit and refine policies
Network security is essential for protecting Kubernetes clusters. Implement defense-in-depth with Network Policies, mTLS, egress filtering, and zero-trust principles to secure your cluster communication.