Bird
Raised Fist0
Jenkinsdevops~5 mins

Canary deployment pattern in Jenkins - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a canary deployment in Jenkins?
easy
A. To release updates to a small group first to reduce risk
B. To deploy all changes to production at once
C. To rollback all changes immediately after deployment
D. To skip testing and deploy directly

Solution

  1. Step 1: Understand canary deployment concept

    Canary deployment means releasing updates to a small subset of users first to catch issues early.
  2. Step 2: Compare options with this concept

    Only To release updates to a small group first to reduce risk describes releasing to a small group to reduce risk, matching the canary pattern.
  3. Final Answer:

    To release updates to a small group first to reduce risk -> Option A
  4. Quick Check:

    Canary deployment = small group release [OK]
Hint: Canary means small group first to catch problems early [OK]
Common Mistakes:
  • Thinking canary deploys to all users at once
  • Confusing canary with rollback process
  • Assuming canary skips testing
2. Which Jenkins Pipeline syntax correctly defines separate stages for canary and full deployment?
easy
A. steps { stage('Canary') { echo 'Deploying canary' } stage('Full') { echo 'Deploying full' } }
B. stage('Deploy') { steps { echo 'Deploying canary and full together' } }
C. stage('Canary') { steps { echo 'Deploying canary' } } stage('Full') { steps { echo 'Deploying full' } }
D. stage('Canary and Full') { steps { echo 'Deploying both' } }

Solution

  1. Step 1: Identify correct Jenkins Pipeline stage syntax

    Each stage must be defined with stage('name') { steps { ... } } blocks.
  2. Step 2: Check which option uses separate stages for canary and full deployment

    stage('Canary') { steps { echo 'Deploying canary' } } stage('Full') { steps { echo 'Deploying full' } } correctly defines two separate stages named 'Canary' and 'Full' with steps inside.
  3. Final Answer:

    stage('Canary') { steps { echo 'Deploying canary' } } stage('Full') { steps { echo 'Deploying full' } } -> Option C
  4. Quick Check:

    Separate stages = stage('Canary') { steps { echo 'Deploying canary' } } stage('Full') { steps { echo 'Deploying full' } } [OK]
Hint: Separate canary and full deploy in different stages [OK]
Common Mistakes:
  • Putting stages inside steps block incorrectly
  • Combining canary and full in one stage
  • Missing steps block inside stage
3. Given this Jenkins Pipeline snippet, what will be the output during the canary stage?
pipeline {
  agent any
  stages {
    stage('Canary') {
      steps {
        echo 'Starting canary deployment'
      }
    }
    stage('Full') {
      steps {
        echo 'Starting full deployment'
      }
    }
  }
}
medium
A. Starting canary deployment\nStarting full deployment
B. Starting full deployment
C. Starting canary deployment
D. No output

Solution

  1. Step 1: Understand Jenkins Pipeline stage execution

    Stages run sequentially. The 'Canary' stage runs first and prints its echo message, then the 'Full' stage runs and prints its message.
  2. Step 2: Identify output during the entire pipeline run

    When the pipeline runs fully, both 'Starting canary deployment' and 'Starting full deployment' are printed sequentially.
  3. Final Answer:

    Starting canary deployment Starting full deployment -> Option A
  4. Quick Check:

    Pipeline output = both stage messages in order [OK]
Hint: Each stage prints its own echo message when running sequentially [OK]
Common Mistakes:
  • Assuming all stages print output at once
  • Confusing canary stage output with full stage
  • Thinking no output appears during stages
4. You wrote this Jenkins Pipeline snippet for canary deployment but it fails with syntax error:
pipeline {
  agent any
  stages {
    stage('Canary')
      steps {
        echo 'Deploying canary'
      }
    }
  }
}
What is the error?
medium
A. Extra closing brace after steps block
B. Incorrect agent declaration
C. Missing echo command inside steps
D. Missing curly braces after stage('Canary') declaration

Solution

  1. Step 1: Check stage syntax in Jenkins Pipeline

    The stage('Canary') must be followed by a block enclosed in curly braces { ... }.
  2. Step 2: Identify missing braces causing syntax error

    The snippet misses opening curly brace after stage('Canary'), causing syntax error.
  3. Final Answer:

    Missing curly braces after stage('Canary') declaration -> Option D
  4. Quick Check:

    Stage needs braces {} after declaration [OK]
Hint: Always add braces {} after stage('name') [OK]
Common Mistakes:
  • Forgetting braces after stage declaration
  • Misplacing steps block outside stage
  • Incorrect indentation causing confusion
5. You want to implement a canary deployment in Jenkins that deploys to 10% of users first, then 100% if no errors. Which approach best fits this requirement?
hard
A. Deploy 100% immediately and rollback if errors occur
B. Use separate Jenkins stages: one for 10% canary deploy, monitor, then full deploy stage
C. Deploy 10% and skip monitoring to save time
D. Deploy 50% first, then 50% later without monitoring

Solution

  1. Step 1: Understand canary deployment best practice

    Canary deployment requires deploying to a small subset (10%) first, then monitoring for errors before full rollout.
  2. Step 2: Match approach to requirement

    Use separate Jenkins stages: one for 10% canary deploy, monitor, then full deploy stage uses separate stages for canary and full deployment with monitoring in between, fitting the requirement best.
  3. Final Answer:

    Use separate Jenkins stages: one for 10% canary deploy, monitor, then full deploy stage -> Option B
  4. Quick Check:

    Canary deploy + monitor + full deploy = Use separate Jenkins stages: one for 10% canary deploy, monitor, then full deploy stage [OK]
Hint: Deploy small group, monitor, then full deploy [OK]
Common Mistakes:
  • Skipping monitoring after canary deploy
  • Deploying full immediately without canary
  • Choosing wrong canary percentage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard