Bird
Raised Fist0
Kubernetesdevops~5 mins

Upgrading and rolling back releases in Kubernetes - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Sometimes you need to update your running application to a new version or fix a problem by going back to an older version. Kubernetes lets you upgrade your app smoothly and roll back if something goes wrong.
When you want to update your app to a new version without downtime
When a new app version has bugs and you need to quickly return to the previous stable version
When testing new features in production but want an easy way to undo changes
When automating deployment pipelines that require version control of app releases
When managing multiple app versions and switching between them safely
Config File - deployment.yaml
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: nginx:1.19
        ports:
        - containerPort: 80

This file defines a Deployment named my-app running 2 replicas of an Nginx container version 1.19. The spec.template.spec.containers.image field is what you change to upgrade the app version.

Commands
This command creates or updates the Deployment with the configuration in deployment.yaml. It starts 2 pods running nginx version 1.19.
Terminal
kubectl apply -f deployment.yaml
Expected OutputExpected
deployment.apps/my-app created
This checks the status of the Deployment rollout to confirm the pods are running the desired version.
Terminal
kubectl rollout status deployment/my-app
Expected OutputExpected
deployment "my-app" successfully rolled out
This command upgrades the app by changing the container image to nginx version 1.21. Kubernetes will update pods one by one.
Terminal
kubectl set image deployment/my-app my-app-container=nginx:1.21
Expected OutputExpected
deployment.apps/my-app image updated
Check again to make sure the upgrade completed successfully and all pods run the new version.
Terminal
kubectl rollout status deployment/my-app
Expected OutputExpected
deployment "my-app" successfully rolled out
If the new version has problems, this command rolls back the Deployment to the previous working version.
Terminal
kubectl rollout undo deployment/my-app
Expected OutputExpected
deployment.apps/my-app rolled back
Verify the rollback completed and pods are running the old stable version again.
Terminal
kubectl rollout status deployment/my-app
Expected OutputExpected
deployment "my-app" successfully rolled out
Key Concept

If you remember nothing else from this pattern, remember: Kubernetes lets you upgrade your app version smoothly and roll back instantly if needed using rollout commands.

Common Mistakes
Changing the image in the deployment.yaml file but not applying it with kubectl apply
The cluster does not get the update, so pods keep running the old version.
Always run kubectl apply -f deployment.yaml after editing the file to update the Deployment.
Not checking rollout status after upgrading or rolling back
You might miss errors or pods stuck in crash loops, causing downtime.
Always run kubectl rollout status deployment/my-app to confirm success.
Using kubectl set image without specifying the correct container name
The image update fails silently or updates the wrong container.
Use the exact container name from the Deployment spec when running kubectl set image.
Summary
Use kubectl apply to create or update your Deployment with the desired app version.
Use kubectl set image to upgrade the app container image smoothly.
Use kubectl rollout status to monitor the progress of upgrades or rollbacks.
Use kubectl rollout undo to quickly revert to the previous stable version if needed.

Practice

(1/5)
1. What is the primary purpose of the helm upgrade command in Kubernetes?
easy
A. To create a new Helm release from scratch
B. To delete a Helm release from the cluster
C. To update an existing Helm release with a new version of the application
D. To list all Helm releases in the cluster

Solution

  1. Step 1: Understand the role of helm upgrade

    This command is used to update an existing release with new chart or configuration changes.
  2. Step 2: Differentiate from other Helm commands

    Unlike helm install which creates new releases, helm upgrade modifies existing ones.
  3. Final Answer:

    To update an existing Helm release with a new version of the application -> Option C
  4. Quick Check:

    Upgrade means update existing release [OK]
Hint: Upgrade means update existing release, not create or delete [OK]
Common Mistakes:
  • Confusing upgrade with install
  • Thinking upgrade deletes releases
  • Assuming upgrade lists releases
2. Which of the following is the correct syntax to rollback a Helm release named myapp to revision 2?
easy
A. helm upgrade myapp --revision=2
B. helm rollback myapp 2
C. helm rollback --release myapp --rev 2
D. helm revert myapp 2

Solution

  1. Step 1: Recall Helm rollback syntax

    The correct command is helm rollback RELEASE_NAME REVISION.
  2. Step 2: Match syntax with options

    helm rollback myapp 2 matches the correct syntax exactly: helm rollback myapp 2.
  3. Final Answer:

    helm rollback myapp 2 -> Option B
  4. Quick Check:

    Rollback syntax is helm rollback name revision [OK]
Hint: Rollback uses: helm rollback release_name revision_number [OK]
Common Mistakes:
  • Using helm upgrade instead of rollback
  • Using incorrect flags like --revision
  • Using nonexistent command 'helm revert'
3. Given the following commands executed in order:
helm install myapp ./chart
helm upgrade myapp ./chart --set image.tag=v2
helm rollback myapp 1
helm status myapp
What will be the image tag shown in the status output after the rollback?
medium
A. No image tag shown
B. v2 (tag set during upgrade)
C. v3 (latest tag automatically applied)
D. v1 (original tag from install)

Solution

  1. Step 1: Understand the sequence of commands

    First, the app is installed with default image tag (assumed v1). Then upgraded to tag v2. Then rolled back to revision 1 (the original install).
  2. Step 2: Determine image tag after rollback

    Rollback to revision 1 restores the original state, so image tag reverts to v1.
  3. Final Answer:

    v1 (original tag from install) -> Option D
  4. Quick Check:

    Rollback restores previous revision state [OK]
Hint: Rollback returns to previous revision state, undoing upgrades [OK]
Common Mistakes:
  • Assuming rollback keeps upgraded tag
  • Thinking rollback applies latest tag automatically
  • Ignoring rollback effect on release state
4. You ran helm upgrade myapp ./chart --set replicas=3 but the number of pods did not change. What is the most likely cause?
medium
A. The chart does not use the replicas value to set pod count
B. You forgot to run helm rollback first
C. The helm upgrade command syntax is incorrect
D. The Kubernetes cluster is down

Solution

  1. Step 1: Check if the chart supports the replicas value

    Not all charts use the replicas parameter; if the chart template ignores it, no change occurs.
  2. Step 2: Rule out other causes

    Syntax is correct, rollback is unrelated, and cluster down would cause more errors.
  3. Final Answer:

    The chart does not use the replicas value to set pod count -> Option A
  4. Quick Check:

    Chart must support value for upgrade to affect it [OK]
Hint: Check if chart templates use your set values before expecting changes [OK]
Common Mistakes:
  • Assuming all charts respond to replicas value
  • Confusing rollback with upgrade necessity
  • Blaming syntax when command is correct
5. You want to upgrade your Helm release webapp to version 3 of your chart but keep the previous configuration values intact except for changing the image tag to v3. Which command achieves this safely?
hard
A. helm upgrade webapp ./chart --reuse-values --set image.tag=v3
B. helm upgrade webapp ./chart --reset-values --set image.tag=v3
C. helm upgrade webapp ./chart --set image.tag=v3
D. helm rollback webapp 3 --set image.tag=v3

Solution

  1. Step 1: Understand --reuse-values option

    This option keeps existing values from the previous release and applies new overrides.
  2. Step 2: Compare with other options

    --reset-values resets to chart defaults, losing previous config. Omitting reuse-values loses previous config. Rollback does not upgrade.
  3. Final Answer:

    helm upgrade webapp ./chart --reuse-values --set image.tag=v3 -> Option A
  4. Quick Check:

    Use --reuse-values to keep old config and override selectively [OK]
Hint: Use --reuse-values with --set to keep config and update specific values [OK]
Common Mistakes:
  • Using --reset-values and losing config
  • Not using --reuse-values and losing previous settings
  • Trying rollback to upgrade