Skip to content

Kubernetes Deployment Strategies: Rolling Updates, Canary, and Blue-Green

Published: at 12:00 AM

Kubernetes offers several deployment strategies to manage application updates with minimal downtime and risk. Understanding these strategies—rolling updates, canary deployments, and blue-green deployments—is crucial for effective application lifecycle management in a Kubernetes environment.

Rolling Updates: Gradual and Controlled

Rolling updates are the default deployment strategy in Kubernetes. They gradually replace old versions of your application’s pods with new ones, ensuring continuous availability and minimizing downtime. This method is ideal for regular, low-risk changes.

To trigger a rolling update:

# Method 1: Edit the deployment directly
kubectl edit deployment hello
# Change image: myapp:1.0.0 to image: myapp:2.0.0

# Method 2: Set a new image
kubectl set image deployment/hello hello=myapp:2.0.0

Commands to control the rollout:

# Check rollout status
kubectl rollout status deployment/hello

# Pause a rollout if issues are detected
kubectl rollout pause deployment/hello

# Resume a paused rollout
kubectl rollout resume deployment/hello

# Rollback to previous version if needed
kubectl rollout undo deployment/hello

Pros:

Cons:

Canary Deployments: Test the Waters

Canary deployments allow you to test a new version of your application with a small subset of users before a full rollout. This strategy helps in identifying potential issues in a production environment with minimal impact.

Example: 25% of traffic (1 out of 4 pods) will go to the new version.

For consistent user experience, add session affinity:

kind: Service
apiVersion: v1
metadata:
  name: hello
spec:
  sessionAffinity: ClientIP  # Same user gets same version
  selector:
    app: hello
  ports:
    - port: 80
      targetPort: 80

Pros:

Cons:

Blue-Green Deployments: Instant Switch

Blue-green deployments involve running two identical production environments, “blue” (current version) and “green” (new version). Traffic is switched from the blue environment to the green environment all at once, providing an instant cutover. This strategy is suitable when you need a clean switch with no mixed versions.

Step 1: Create the green deployment (alongside existing blue):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-green
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        app: hello
        track: stable
        version: "2.0.0"  # New version

Pros:

Cons:

Choosing the Right Strategy

The choice of deployment strategy depends on the specific needs and risk tolerance of your application:

You can effectively combine these patterns for different scenarios within the same application to optimize your deployment process.


Next Post
A Practical Guide to `uv`: The Super-Fast Python Package Manager