0
0
Jenkinsdevops~5 mins

Canary deployment pattern in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Canary deployment helps release new software versions to a small group of users first. This way, you can check if the new version works well before giving it to everyone. It reduces the risk of big problems in your app.
When you want to test a new feature with a small group of users before full release.
When you want to reduce downtime by gradually updating your app.
When you want to quickly roll back if the new version has bugs.
When you want to monitor performance and errors of a new release in real time.
When you want to deploy updates safely in a production environment.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Deploy Canary') {
      steps {
        echo 'Deploying canary version to 1 server'
        sh 'kubectl set image deployment/my-app my-app=my-app:v2-canary --record'
        sh 'kubectl scale deployment/my-app --replicas=1'
      }
    }
    stage('Monitor Canary') {
      steps {
        echo 'Waiting and monitoring canary deployment'
        sleep 60
        sh 'kubectl rollout status deployment/my-app'
      }
    }
    stage('Deploy Full') {
      steps {
        echo 'Deploying full version to all servers'
        sh 'kubectl set image deployment/my-app my-app=my-app:v2 --record'
        sh 'kubectl scale deployment/my-app --replicas=10'
        sh 'kubectl rollout status deployment/my-app'
      }
    }
  }
}

This Jenkinsfile defines a pipeline with three stages:

  • Deploy Canary: Updates a small number of pods to the new version (v2-canary) and scales to 1 replica.
  • Monitor Canary: Waits and checks if the canary deployment is successful.
  • Deploy Full: If canary is good, updates all pods to the new version (v2) and scales to 10 replicas.

This gradual rollout helps catch issues early.

Commands
This command updates the image of the my-app deployment to the canary version v2-canary. It changes only a small part of the app to test the new version.
Terminal
kubectl set image deployment/my-app my-app=my-app:v2-canary --record
Expected OutputExpected
deployment.apps/my-app image updated
--record - Records the command in the deployment's rollout history for tracking
This command scales the deployment to 1 replica, so only one pod runs the canary version for testing.
Terminal
kubectl scale deployment/my-app --replicas=1
Expected OutputExpected
deployment.apps/my-app scaled
--replicas - Sets the number of pod replicas to run
This command checks the rollout status of the deployment to see if the canary pod is running successfully.
Terminal
kubectl rollout status deployment/my-app
Expected OutputExpected
deployment "my-app" successfully rolled out
After successful canary testing, this command updates the deployment to the full new version v2 for all pods.
Terminal
kubectl set image deployment/my-app my-app=my-app:v2 --record
Expected OutputExpected
deployment.apps/my-app image updated
--record - Records the update in rollout history
This command scales the deployment to 10 replicas to serve all users with the new version.
Terminal
kubectl scale deployment/my-app --replicas=10
Expected OutputExpected
deployment.apps/my-app scaled
--replicas - Sets the number of pod replicas
Key Concept

If you remember nothing else from this pattern, remember: deploy new versions to a small group first, monitor carefully, then roll out fully.

Common Mistakes
Scaling all replicas to the new version immediately without testing canary.
This risks exposing all users to bugs or failures at once.
Always deploy to a small subset first, verify stability, then scale up.
Not monitoring the canary deployment status before full rollout.
You might miss errors or crashes in the new version.
Use rollout status commands and wait to confirm success before proceeding.
Forgetting to use the --record flag when updating images.
You lose the ability to track deployment history and rollbacks.
Always include --record to keep deployment history.
Summary
Use kubectl set image to update deployment images for canary and full versions.
Scale deployment replicas to control how many pods run the new version.
Check rollout status to monitor deployment success before scaling fully.