Skip to main content
Cluster operationsLesson 15 of 16

Chaos Engineering on Kubernetes

Failures will happen in production whether you plan for them or not. Chaos engineering breaks things on purpose, in a controlled way, so you find the missing retry or PodDisruptionBudget before your users do.

Steady state99.95% · p99 180 ms
Blast radius—
Hypothesis—
Finding—
Zone aZone bExperimenthypothesis + scopeChaos controllerChaos Mesh / LitmusSLO metricsPrometheusnode-aworkercartPodcartPodnode-bworkercartPodcheckoutcalls cartPDB cartminAvailable: 2Schedulerepeat after deploysSuccess, p99
RequestResponse / reportWatchAppears in a later step

What you will learn

  • Steady state and a falsifiable hypothesis
  • Blast radius and abort conditions tied to SLO metrics
  • What Pod kills, latency and node drains reveal
  • Losing a zone and stressing CPU
  • Game days and automated, repeated experiments

Experiments and what they usually find​

ExperimentChaos MeshLitmusChaos faultTypical findingFix
Kill a PodPodChaos pod-killpod-deleteFailed in-flight requests, slow startupRetries with backoff, readiness probes, graceful shutdown
Inject latencyNetworkChaos delaypod-network-latencySlowness spreads to callersTimeouts, circuit breakers
Packet lossNetworkChaos losspod-network-lossLong hangs, retry stormsTimeouts, retry budgets
Drain a node(use kubectl drain)node-drainAll replicas on one node, eviction gapsPodDisruptionBudget, topology spread
Lose a zoneNetworkChaos partition, cloud chaoscloud faultsNot enough capacity leftHeadroom per zone, autoscaler limits
Stress CPUStressChaos cpupod-cpu-hogThrottling, late autoscalingRequests and limits, HPA tuning

Try it yourself​

# Install Chaos Mesh (containerd runtime) into its own namespace
helm repo add chaos-mesh https://charts.chaos-mesh.org
helm install chaos-mesh chaos-mesh/chaos-mesh -n chaos-mesh --create-namespace \
--set chaosDaemon.runtime=containerd \
--set chaosDaemon.socketPath=/run/containerd/containerd.sock

# Before: check replicas, spread and PDBs
kubectl get pods -n staging -l app=cart -o wide
kubectl get pdb -n staging

# Kill one cart Pod
cat <<'EOF' | kubectl apply -f -
apiVersion: chaos-mesh.org/v1alpha1
kind: PodChaos
metadata:
name: kill-one-cart
namespace: staging
spec:
action: pod-kill
mode: one
selector:
namespaces: [staging]
labelSelectors:
app: cart
EOF
kubectl get pods -n staging -l app=cart -w # watch the replacement

# Add 500 ms latency to one cart Pod for 2 minutes
cat <<'EOF' | kubectl apply -f -
apiVersion: chaos-mesh.org/v1alpha1
kind: NetworkChaos
metadata:
name: cart-delay
namespace: staging
spec:
action: delay
mode: one
selector:
namespaces: [staging]
labelSelectors:
app: cart
delay:
latency: '500ms'
duration: '2m'
EOF

# Stop the experiments early
kubectl delete podchaos kill-one-cart -n staging
kubectl delete networkchaos cart-delay -n staging

Check yourself

  1. Which is the best steady-state metric for a chaos experiment?
  2. Why is a 500 ms delay often more damaging than a crashed dependency?
  3. An experiment starts pushing the checkout error rate above the SLO. What should happen?

Further reading: Principles of Chaos Engineering · Chaos Mesh docs · LitmusChaos · LitmusChaos probes · When a Pod Dies (Self-Healing) · Draining Nodes and Upgrading the Cluster