Deploying Without Downtime in Kubernetes

Introduction

Ensuring zero downtime during deployment updates is a crucial requirement for modern applications. Kubernetes provides a RollingUpdate strategy that allows you to update applications seamlessly without disrupting users.

Why Use Rolling Updates?

A rolling update gradually replaces old pods with new ones, ensuring that your application remains available during deployment.

Key features:

  • Readiness Probes: Prevents traffic from reaching new pods until they are fully ready.
  • Liveness Probes: Detects and restarts unresponsive containers to maintain application health.
  • MaxUnavailable: 1: Ensures at least one pod is always running.
  • MaxSurge: 1: Allows one extra pod to be created during the rollout.

Example: Kubernetes Deployment with Rolling Updates, Readiness, and Liveness Probes

Here’s a detailed example of how to deploy an application using a rolling update strategy with both readiness and liveness probes.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3  # Ensuring high availability
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1  # At most, 1 pod is unavailable at a time
      maxSurge: 1  # Allows 1 extra pod to be created
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: my-app:v2  # New version
        ports:
        - containerPort: 80
        readinessProbe:
          httpGet:
            path: /health
            port: 80
          initialDelaySeconds: 3
          periodSeconds: 5
        livenessProbe:
          httpGet:
            path: /health
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 10

Explanation of Configuration:

  1. Replicas: At least 3 pods are maintained to ensure availability.
  2. Rolling Update Strategy:
    • maxUnavailable: 1 ensures at least one pod is always running.
    • maxSurge: 1 allows Kubernetes to create a new pod before removing an old one.
  3. Readiness Probe: Prevents traffic from reaching pods until they pass health checks.
  4. Liveness Probe: Ensures that unresponsive containers are restarted automatically.
  5. Image Update (my-app:v2): This defines the new version being deployed.

Real-World Use Case

E-commerce Application Deployment

Consider an online shopping platform that cannot afford downtime, especially during sales events. Using the above strategy:

  • The backend services can be updated without affecting customer transactions.
  • Readiness probes ensure users only interact with fully initialized pods.
  • Liveness probes restart unresponsive containers, preventing failures.
  • Users experience zero disruption while the new version is deployed.

Best Practices for Zero Downtime Deployment

  1. Monitor Deployments: Use kubectl rollout status deployment my-app to track updates.
  2. Rollback on Failure: Use kubectl rollout undo deployment my-app to revert if issues arise.
  3. Use Canary Deployments: Gradually route traffic to the new version to test performance before full rollout.
  4. Test Probes Regularly: Ensure the liveness and readiness probes correctly reflect application health.

Conclusion

A rolling update strategy, combined with readiness and liveness probes, ensures that deployments occur smoothly without downtime. By leveraging these techniques, you can deliver updates with confidence while keeping services online at all times.

Would you like to explore blue-green deployments or canary releases for even more controlled rollouts? Let us know in the comments!

Leave a Reply

Your email address will not be published. Required fields are marked *