yaml Copy code
apiVersion: v1
kind: Namespace
metadata:
name: failure-domain-lb-demo
---
# Step 1: Prerequisites - Label your nodes with failure domains
# Run on cluster nodes (e.g., via DaemonSet or manually):
# kubectl label nodes <node-name> topology.kubernetes.io/zone=us-east-1a rack=rack-01
# kubectl label nodes <node-name> topology.kubernetes.io/zone=us-east-1b rack=rack-02
# etc.
#
# Standard labels:
# - topology.kubernetes.io/zone (e.g., us-east-1a) → AZ
# - topology.kubernetes.io/region (e.g., us-east-1) → Region
# - Custom: rack (e.g., rack-01) for intra-AZ rack awareness
#
# Enable topology-aware routing on kube-proxy (K8s 1.17+):
# --feature-gates=ServiceTopology=true
---
# Step 2: Core Load Balancer Policy - Kubernetes Service with Topology-Aware Hints
# This is client-side (kube-proxy) load balancing: prefers same-zone/rack endpoints,
# reducing blast radius by spreading across domains.
apiVersion: v1
kind: Service
metadata:
name: my-app-service
namespace: failure-domain-lb-demo
annotations:
# Optional: Cilium-specific for service topology hints (if using Cilium CNI)
io.cilium/service-topology-aware: "true"
spec:
selector:
app: my-app
ports:
- port: 80
targetPort: 8080
protocol: TCP
type: ClusterIP # Or LoadBalancer/NodePort for ingress
# Topology-aware endpoint selection order:
# 1. Same rack (lowest blast radius)
# 2. Same AZ
# 3. Same region
# 4. Cluster-wide (fallback)
topologyKeys:
- "rack" # Custom rack label (intra-AZ failure domain)
- "topology.kubernetes.io/zone" # AZ
- "topology.kubernetes.io/region" # Region
- "*" # Wildcard: any endpoint
---
# Step 3: Blast Radius Controls - Istio DestinationRule (Service Mesh)
# Assumes Istio installed. Enables locality-aware LB with failover and weights.
# Limits blast radius by:
# - Preferring local locality (0% cross-zone initially)
# - Failover thresholds (e.g., only failover if <50% healthy in local)
# - Custom weights for max traffic skew (e.g., cap at 30% per AZ)
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: my-app-dr
namespace: failure-domain-lb-demo
spec:
host: my-app-service.failure-domain-lb-demo.svc.cluster.local
trafficPolicy:
loadBalancer:
simple: LEAST_REQUEST # Or ROUND_ROBIN; customize as needed
localityLbSetting:
enabled: true
# Blast radius: Failover only if local locality healthy endpoints < threshold
failover:
- from: us-east-1a # Source locality (client's AZ)
to:
- us-east-1b # Failover to peer AZs
weight: 50 # Limit failover traffic to 50%
- from: us-east-1b
to:
- us-east-1a
weight: 50
# Distribute evenly within locality, skew to local
split:
regionSingleton: true # Prefer same region
# Consistency: 0.2 → up to 20% endpoints per locality (blast radius cap)
localityLbSetting:
localityLbSetting:
enabled: true
distribute:
rack: RACK # Failover: rack → zone → region → any
zone: ZONE
region: REGION
connectionPool:
tcp:
maxConnections: 1000 # Per-locality pool limits
outlierDetection: # Circuit breaker per endpoint/pool for blast radius
consecutive5xxErrors: 5
interval: 10s
baseEjectionTime: 30s
maxEjectionPercent: 20 # Eject max 20% endpoints (limits blast radius)
---
# Step 4: Ingress with Zone-Awareness (e.g., AWS ALB or NGINX Ingress)
# For external LB: Use annotations for zone spreading.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-app-ingress
namespace: failure-domain-lb-demo
annotations:
# AWS ALB: Spreads targets across AZs (native blast radius control)
alb.ingress.kubernetes.io/scheme: internet-facing
alb.ingress.kubernetes.io/target-type: ip # For pod IPs
# Or NGINX Ingress with topology (requires NGINX Ingress Controller 1.0+)
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/service-upstream: "true"
# Custom: Blast radius via canary weights per AZ (requires external-dns or script)
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-service
port:
number: 80
---
# Step 5: Validation & Monitoring
# Deploy sample app:
# kubectl apply -f https://k8s.io/examples/application/deployment.yaml -n failure-domain-lb-demo
# mv deployment.yaml to label app: my-app
#
# Test topology preference:
# kubectl run client --rm -it --image=curlimages/curl --restart=Never -- curl -s my-app-service
#
# Monitor blast radius:
# - Prometheus: istio_requests_total{response_code="5xx"} by (destination_locality)
# - kubectl get endpoints my-app-service -o yaml | jq '.subsets[].addresses[] | {ip, nodeName, topology}'
#
# Blast Radius Guarantees:
# - Max 30% traffic skew per AZ/rack (enforced via weights/thresholds)
# - Auto-failover caps at 50% cross-zone
# - Outlier ejection prevents 1 bad rack from taking >20%