Kubernetes Deployment Testing: Strategies for Zero-Downtime Releases

Kubernetes Deployment Testing: Strategies for Zero-Downtime Releases

Kubernetes makes zero-downtime deployments possible but doesn't make them automatic. Without the right configuration and testing, a Kubernetes rolling update can drop requests, deploy a broken version to 100% of traffic, or leave pods in a failed state without alerting anyone.

This guide covers how to configure and test Kubernetes deployments so that releases are genuinely zero-downtime and failures are caught before users see them.

How Kubernetes Rolling Updates Work

By default, Kubernetes performs a rolling update when you change the image on a Deployment. It brings up new pods, waits for them to be ready, and terminates old pods — one at a time.

The critical question: what does "ready" mean? Without a readiness probe, Kubernetes considers a pod ready as soon as the container starts. Your application may need 10 seconds to initialize, connect to the database, and warm up caches. During those 10 seconds, if traffic is routed to the new pod, requests will fail.

Readiness and Liveness Probes

Configure probes correctly:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1        # Allow 1 extra pod during update
      maxUnavailable: 0  # Never take a pod down before replacement is ready
  template:
    spec:
      containers:
      - name: app
        image: my-app:v2.0.0
        readinessProbe:
          httpGet:
            path: /ready
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 5
          failureThreshold: 3
          successThreshold: 1
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 15
          periodSeconds: 10
          failureThreshold: 3

Readiness probe: Gates traffic routing. Pod doesn't receive traffic until this passes. Use for application initialization.

Liveness probe: Restarts the pod if it fails. Use for detecting deadlocks or crashes. Set initialDelaySeconds higher than readiness to avoid restarting during startup.

Implement a meaningful /ready endpoint:

app.get('/ready', async (req, res) => {
  try {
    // Check database connectivity
    await db.query('SELECT 1');
    
    // Check external dependencies
    await redis.ping();
    
    res.status(200).json({ status: 'ready' });
  } catch (err) {
    res.status(503).json({ status: 'not ready', error: err.message });
  }
});

Testing Readiness Probe Configuration

A misconfigured readiness probe is worse than none — it may pass even when the application is broken, or fail-fast when it shouldn't.

Test the probe directly:

# Deploy new version
kubectl set image deployment/my-app app=my-app:v2.0.0 -n staging

# Watch readiness in real time
kubectl get pods -n staging -w

# Verify the probe endpoint manually
POD=$(kubectl get pods -n staging -l app=my-app -o jsonpath='{.items[0].metadata.name}')
kubectl exec $POD -n staging -- curl -s localhost:8080/ready

Test that a broken pod fails readiness correctly:

# Simulate a database connection failure
kubectl exec $POD -n staging -- kill -STOP $(kubectl exec $POD -n staging -- pgrep node)

# Wait for readiness probe to fail
sleep 30

# Verify pod shows 0/1 ready (not receiving traffic)
kubectl get pod $POD -n staging
# Expected: STATUS=Running, READY=0/1

Pod Disruption Budgets

A Pod Disruption Budget (PDB) sets a minimum availability requirement during voluntary disruptions — including rolling updates:

apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: my-app-pdb
spec:
  selector:
    matchLabels:
      app: my-app
  minAvailable: 2  # Always keep at least 2 pods available

Test that the PDB works:

# Try to manually evict all pods
kubectl drain node-1 --ignore-daemonsets

# PDB should block eviction if it would violate minAvailable
kubectl get pdb my-app-pdb -n production
# DISRUPTIONS ALLOWED should be > 0

Deployment Verification Tests

After a Kubernetes rolling update, run verification tests before considering the deployment complete:

#!/bin/bash
# scripts/verify-deployment.sh

DEPLOYMENT=$1
NAMESPACE=${2:-production}
IMAGE=$3

echo "Verifying deployment: $DEPLOYMENT in $NAMESPACE"

# 1. Check rollout completed
kubectl rollout status deployment/$DEPLOYMENT -n $NAMESPACE --timeout=300s
if [ $? -ne 0 ]; then
  echo "FAIL: Rollout did not complete"
  kubectl rollout undo deployment/$DEPLOYMENT -n $NAMESPACE
  exit 1
fi

# 2. Verify correct image is running
RUNNING_IMAGE=$(kubectl get deployment $DEPLOYMENT -n $NAMESPACE \
  -o jsonpath='{.spec.template.spec.containers[0].image}')
if [ "$RUNNING_IMAGE" != "$IMAGE" ]; then
  echo "FAIL: Expected $IMAGE but found $RUNNING_IMAGE"
  exit 1
fi

# 3. Verify all pods are ready
READY=$(kubectl get deployment $DEPLOYMENT -n $NAMESPACE \
  -o jsonpath='{.status.readyReplicas}')
DESIRED=$(kubectl get deployment $DEPLOYMENT -n $NAMESPACE \
  -o jsonpath='{.status.replicas}')
if [ "$READY" != "$DESIRED" ]; then
  echo "FAIL: Only $READY/$DESIRED pods ready"
  exit 1
fi

# 4. Run smoke tests
robot smoke/ --variable ENV_URL:$PRODUCTION_URL
if [ $? -ne 0 ]; then
  echo "FAIL: Smoke tests failed — rolling back"
  kubectl rollout undo deployment/$DEPLOYMENT -n $NAMESPACE
  exit 1
fi

echo "PASS: Deployment verified — $IMAGE is running and healthy"

Testing With Traffic During Deployment

The real test of a rolling update is whether in-flight requests succeed. Use a load tester to generate constant traffic during the deployment:

# Terminal 1: Generate continuous traffic
while true; do
  STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://app.example.com/health)
  echo "$(date): $STATUS"
  if [ "$STATUS" != "200" ]; then
    echo "FAILURE DETECTED: HTTP $STATUS"
  fi
  sleep 0.5
done

# Terminal 2: Trigger rolling update
kubectl set image deployment/my-app app=my-app:v2.0.0 -n production
kubectl rollout status deployment/my-app -n production

Any non-200 responses in Terminal 1 during the rolling update indicate that traffic is reaching pods that aren't ready — a configuration bug in your deployment or probes.

Namespace-Based Staging for Deployment Testing

Test deployments in a staging namespace that mirrors production configuration:

# Apply the same deployment config to staging namespace
kubectl apply -f k8s/deployment.yaml -n staging

# Run full deployment verification in staging
./scripts/verify-deployment.sh my-app staging my-app:v2.0.0

# If staging passes, deploy to production
kubectl apply -f k8s/deployment.yaml -n production
./scripts/verify-deployment.sh my-app production my-app:v2.0.0

This gives you a deployment test that uses the exact same Kubernetes manifests as production, catching configuration bugs before they reach real traffic.

Health Check Monitoring After Deployment

Deploy and monitor — don't deploy and move on. Use HelpMeTest health checks to monitor your production endpoints continuously after each deployment:

helpmetest health production-api 30s

This sends a heartbeat every 30 seconds. If your deployment causes degradation that the initial smoke tests didn't catch — a memory leak that surfaces under traffic, a cache that performs poorly after warm-up — the health check will alert you within 30 seconds.

Combine with a post-deployment observation window: declare a deployment complete only after health checks have been green for 30 minutes.

Common Kubernetes Deployment Testing Failures

Pod scheduled but image not found: The container registry doesn't have the target tag. Test: docker pull registry.example.com/my-app:v2.0.0 before deploying.

Container starts but exits immediately: Application crashes on startup. Test: kubectl logs -n staging -l app=my-app --previous after a failed rollout.

Readiness probe path wrong after routing change: Application moved /health to /api/health. Test: verify probe paths match after any routing refactor.

Resource limits cause OOMKilled: New version uses more memory than the limit allows. Test: deploy to staging first and monitor kubectl top pods -n staging.

ConfigMap or Secret not updated: Application references new env var that's not in the ConfigMap. Test: validate required env vars are present on pod startup.

Summary

Zero-downtime Kubernetes deployments require correct readiness probe configuration, appropriate maxUnavailable settings, Pod Disruption Budgets for high-availability services, and post-deployment smoke testing. Testing the deployment process itself — not just the application code — is what makes releases reliable. Build verification scripts, test rollbacks, and monitor the deployment window rather than declaring victory at kubectl apply.

Read more

Start now free