Implementing Canary Deployments in Kubernetes

Saurabh AdhauSaurabh Adhau
4 min read

Learning Goal

Learn how to execute canary deployments in Kubernetes to update your applications while reducing risks gradually.

Context

You need to update your application within a Kubernetes cluster, aiming to minimize potential issues by gradually introducing changes to a small group of users before a complete rollout.

Overview

Canary deployments enable you to release a new application version to a limited user base first, helping to identify potential issues early and ensuring minimal impact on users if problems arise.

Key Concepts

Canary Deployment

A strategy that gradually rolls out changes to a small segment of users before full deployment.

Istio

An open-source service mesh that facilitates traffic management, security, and observability, making it ideal for canary deployments.

Traffic Management

Directing network traffic to different application versions during a canary deployment.

Setting Up a Canary Deployment with Istio

Istio can effectively manage traffic between different application versions during a canary deployment.

Installing Istio

  1. Download and Install Istio CLI:

     curl -L https://istio.io/downloadIstio | sh -
     cd istio-1.*
     export PATH=$PWD/bin:$PATH
    
  2. Install Istio in Your Cluster:

     istioctl install --set profile=demo -y
    
  3. Label the Namespace for Istio Injection:

     kubectl label namespace default istio-injection=enabled
    

Deploying the Initial Version of the Application

Deployment YAML for Version 1

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-v1
  labels:
    app: myapp
    version: v1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
      version: v1
  template:
    metadata:
      labels:
        app: myapp
        version: v1
    spec:
      containers:
      - name: myapp
        image: myapp:v1
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  selector:
    app: myapp
  ports:
  - port: 80
    targetPort: 80

Deploying the Canary Version of the Application

Deployment YAML for Version 2

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-v2
  labels:
    app: myapp
    version: v2
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
      version: v2
  template:
    metadata:
      labels:
        app: myapp
        version: v2
    spec:
      containers:
      - name: myapp
        image: myapp:v2
        ports:
        - containerPort: 80

Managing Traffic with Istio

VirtualService for Canary Deployment

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: myapp
spec:
  hosts:
  - myapp
  http:
  - route:
    - destination:
        host: myapp
        subset: v1
      weight: 90
    - destination:
        host: myapp
        subset: v2
      weight: 10

DestinationRule for Canary Deployment

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: myapp
spec:
  host: myapp
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2

Steps to Implement Canary Deployments

  1. Set Up Istio:

    • Install Istio CLI and set up Istio in your Kubernetes cluster.

    • Label the namespace for Istio injection.

  2. Deploy Initial Application Version:

    • Apply the deployment YAML for version 1.
  3. Deploy Canary Version:

    • Apply the deployment YAML for version 2.
  4. Manage Traffic with Istio:

    • Create and apply a VirtualService to route traffic between versions.

    • Create and apply a DestinationRule to configure traffic policies.

  5. Gradually Increase Traffic to the Canary Version:

    • Update the VirtualService to increase traffic to version 2 while monitoring for issues.

Detailed Example Insights

Canary Deployment

Gradually introducing updates to a small user segment allows for early identification of issues, limiting user impact in case of problems.

Istio

Istio enables precise traffic management, allowing fine-grained control over traffic routing between application versions.

Benefits for Enterprise Applications

  • Minimized Risk: Reduces the likelihood of introducing bugs by incrementally rolling out changes.

  • Early Detection: Identifies potential issues before full deployment.

  • Controlled Rollback: Facilitates easy rollback if issues arise during the canary phase.

Additional Concepts

Rollback to the Previous Version

If issues occur with version 2, update the VirtualService to revert all traffic back to version 1.

Example

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: myapp
spec:
  hosts:
  - myapp
  http:
  - route:
    - destination:
        host: myapp
        subset: v1
      weight: 100
    - destination:
        host: myapp
        subset: v2
      weight: 0

Monitoring and Metrics with Istio

Utilize Istio's telemetry features to track performance and health during the canary deployment.

Example

Set up Prometheus and Grafana to visualize Istio metrics for monitoring application performance.

Practical Activity Guide

  1. Set Up Istio:

    • Download and install the Istio CLI, set up Istio in the cluster, and label the namespace.
  2. Deploy Initial Application Version:

    • Apply the deployment YAML for version 1.
  3. Deploy the Canary Version:

    • Apply the deployment YAML for version 2.
  4. Manage Traffic Using Istio:

    • Create and apply the VirtualService and DestinationRule for traffic management.
  5. Gradually Increase Traffic:

    • Update the VirtualService to increase traffic to version 2 while monitoring for issues.
  6. Rollback if Necessary:

    • Modify the VirtualService to direct all traffic back to version 1 if issues arise.
  7. Monitor Application Performance:

    • Use Istio’s telemetry features to assess the application's health and performance.

Engage and Share Insights

Understanding and implementing canary deployments is crucial for safely updating applications and minimizing risks.

We’d love to hear from you! How do you plan to adopt canary deployments in your Kubernetes projects? What challenges have you encountered? Share your experiences and insights!

Stay tuned for more educational content and continue advancing your Kubernetes knowledge to stay competitive in the fast-evolving tech landscape. Let’s keep exploring, innovating, and automating together!

10
Subscribe to my newsletter

Read articles from Saurabh Adhau directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saurabh Adhau
Saurabh Adhau

As a DevOps Engineer, I thrive in the cloud and command a vast arsenal of tools and technologies: ☁️ AWS and Azure Cloud: Where the sky is the limit, I ensure applications soar. 🔨 DevOps Toolbelt: Git, GitHub, GitLab – I master them all for smooth development workflows. 🧱 Infrastructure as Code: Terraform and Ansible sculpt infrastructure like a masterpiece. 🐳 Containerization: With Docker, I package applications for effortless deployment. 🚀 Orchestration: Kubernetes conducts my application symphonies. 🌐 Web Servers: Nginx and Apache, my trusted gatekeepers of the web.