0
0
Kubernetesdevops~5 mins

Upgrading and rolling back releases in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
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.