CareerByteCode
ByteBlogby CareerByteCode

Kubernetes Deployments Explained: Rolling Updates and Rollbacks

🟢 Beginner

Learn how Kubernetes Deployments manage Pods, scale applications, perform rolling updates, roll back releases and troubleshoot common Pod issues.

Kubernetes Deployments Explained: Rolling Updates and Rollbacks

✍️ Translate this blog into Hindi, Tamil or Telugu


Introduction

Imagine you have an application running on Kubernetes. Everything is working correctly, but now you want to release a newer version.

How do you update the application without manually deleting the existing Pods?

What happens if the new version does not work?

🛍️✨ From the author · ByteStore1:1 DevOps Project & Learning Discussionโ‚น500View offer →

And how can you return to the previous working version?

This is where Kubernetes Deployments help.

A Deployment manages application Pods and helps us:

  • Create and maintain the required number of Pods.

  • Scale applications up or down.

  • Update applications gradually.

  • Check whether an update has completed.

  • Roll back to a previous version when needed.

A ReplicaSet maintains Pods, but how do we safely update an application? That's where Deployments help.

1. What Is a Kubernetes Deployment?

A Kubernetes Deployment is a resource that manages the lifecycle of application Pods.

Instead of creating and managing individual Pods manually, we tell Kubernetes what we want.

For example:

  • Run 3 copies of an NGINX application.

  • Keep all 3 copies available.

  • Use NGINX version 1.27.

  • Update to version 1.28 when required.

The Deployment continuously works to maintain the desired state.

If one Pod fails, Kubernetes works to replace it.

Think of a Deployment as a manager who makes sure the required number of workers are available.

2. Understand Deployment, ReplicaSet and Pods

Before creating a Deployment, let's understand how these three resources are connected.

Deployment โ†’ ReplicaSet โ†’ Pods

  • Deployment: Manages application updates and the desired state.

  • ReplicaSet: Makes sure the required number of Pods are running.

  • Pods: Run the actual application containers.

For example, when we create a Deployment with 3 replicas:

  1. The Deployment creates a ReplicaSet.

  2. The ReplicaSet creates 3 Pods.

  3. If a Pod fails, the ReplicaSet works to replace it.

The Deployment manages the ReplicaSet, and the ReplicaSet manages the Pods.

Kubernetes Deployment Architecture

3. Prerequisites

Before starting, make sure you have:

  • A running Kubernetes cluster.

  • kubectl installed and configured.

  • Basic knowledge of YAML.

We'll use Minikube in the examples, but any suitable Kubernetes cluster works.

Check your cluster:

kubectl get nodes

Expected output will show your cluster node with the status Ready.

4. Create Your First Deployment

Let's create a Deployment that runs three NGINX Pods.

Create a file named nginx-deployment.yaml.

apiVersion: apps/v1
kind: Deployment

metadata:
  name: nginx-deployment

spec:
  replicas: 3

  selector:
    matchLabels:
      app: nginx

  template:
    metadata:
      labels:
        app: nginx

    spec:
      containers:
        - name: nginx
          image: nginx:1.27
          ports:
            - containerPort: 80

Let's understand the important fields:

FieldMeaning
apiVersionAPI version used to create the resource
kindType of Kubernetes resource
metadata.nameName of the Deployment
replicasNumber of Pods we want
selectorIdentifies the Pods managed by the Deployment
templateDescribes how the Pods should be created
containersDefines the container inside the Pod
imageContainer image to run
containerPortPort the container is configured to use

Important: The labels in template.metadata.labels must match the selector in spec.selector.matchLabels.

Important: containerPort: 80 documents the port used by the container. It does not expose the application outside the Pod or make it accessible to users. A Kubernetes Service provides a way to expose the application.

The Deployment selector is generally immutable after creation. If you need to change it, you normally create a new Deployment with the correct selector rather than trying to update the existing selector.

Also, containerPort: 80 documents the port used by the container. It does not expose the application outside the Pod or make it accessible to users. We will learn how Kubernetes Services provide access in the next article.

Apply the YAML file

kubectl apply -f nginx-deployment.yaml

This command creates the Deployment from the YAML file.

Check the Deployment:

kubectl get deployments

Wait until the READY column shows 3/3.

Check the Pods:

kubectl get pods -l app=nginx

You should see three NGINX Pods running.

Check the ReplicaSet:

kubectl get rs

You should see a ReplicaSet with three desired and ready replicas for this Deployment.

To check the Deployment in more detail:

kubectl describe deployment nginx-deployment

Kubernetes Deployment Selector & Pod Labels

5. Scale a Deployment

Suppose your application initially runs 3 Pods.

Now more users are accessing the application, so you want to increase the number of Pods to 5.

This is called scaling up.

Run:

kubectl scale deployment nginx-deployment --replicas=5

Verify:

kubectl get deployments

Wait until READY shows 5/5.

Now let's reduce the number of Pods to 2.

kubectl scale deployment nginx-deployment --replicas=2

Verify:

kubectl get deployments

Wait until READY shows 2/2.

Restore the count to 3:

kubectl scale deployment nginx-deployment --replicas=3

Verify again:

kubectl get deployments

Wait until READY shows 3/3 before continuing.

Remember:

  • Scaling up increases the number of Pods.

  • Scaling down decreases the number of Pods.

  • Scaling changes the replica count; it does not change the Pod template, so it does not create a new Deployment revision by itself.

6. Understand Deployment Update Strategies

Kubernetes supports different Deployment update strategies.

Two commonly used strategies are:

RollingUpdate

A RollingUpdate gradually replaces old Pods with new Pods.

For example, when updating NGINX from 1.27 to 1.28, Kubernetes creates new Pods and gradually removes the old ones.

This helps maintain application availability during an update.

Recreate

The Recreate strategy terminates the existing Pods before creating new Pods with the updated configuration.

This can cause downtime because the old Pods are removed before the new Pods become available.

StrategyHow it works
RollingUpdateGradually replaces old Pods with new Pods
RecreateTerminates old Pods before creating new Pods

For applications that should remain available during updates, RollingUpdate is commonly preferred.

Understand RollingUpdate settings

A RollingUpdate strategy can be configured with:

strategy:
  type: RollingUpdate
  rollingUpdate:
    maxSurge: 25%
    maxUnavailable: 25%

These settings will be included in the complete Deployment YAML in Section 12, so you don't need to edit your file separately at this stage.

Let's understand the fields:

FieldMeaning
maxSurgeMaximum additional Pods allowed during an update
maxUnavailableMaximum Pods allowed to be unavailable during an update

For 3 replicas:

  • maxSurge: 25% rounds up to 1 additional Pod.

  • maxUnavailable: 25% rounds down to 0 unavailable Pods.

These settings help Kubernetes control how the update happens.

Note: Changing the update strategy alone does not create a new Deployment revision if the Pod template remains unchanged.

Kubernetes Rolling Update Architecture

7. Perform a Rolling Update

Imagine your application is running NGINX version 1.27.

Now you want to upgrade it to NGINX version 1.28.

Before updating, open another terminal and run:

kubectl get pods -w

The -w option means watch. It continuously displays changes in Pod status.

Keep this terminal open while performing the update.

In your first terminal, add a change-cause annotation:

kubectl annotate deployment nginx-deployment \
  kubernetes.io/change-cause="Update NGINX to 1.28" \
  --overwrite

This adds a description that can be displayed in the rollout history.

Now update the container image:

kubectl set image deployment/nginx-deployment nginx=nginx:1.28

Here:

  • deployment/nginx-deployment identifies the Deployment.

  • nginx is the container name.

  • nginx:1.28 is the new container image.

Check the rollout:

kubectl rollout status deployment/nginx-deployment

When the update finishes, Kubernetes reports:

deployment "nginx-deployment" successfully rolled out

Verify the image:

kubectl get deployment nginx-deployment \
  -o jsonpath='{.spec.template.spec.containers[0].image}'

Expected output:

nginx:1.28

Return to the second terminal and observe the Pod changes. Press Ctrl+C to stop watching.

The exact sequence and timing of Pod changes can vary depending on the cluster.

8. Understand ReplicaSets During a Rolling Update

What happens to the ReplicaSets when we update the image?

Suppose the original Deployment runs NGINX 1.27.

When we update it to NGINX 1.28:

  1. Kubernetes creates a new ReplicaSet for the updated Pod template.

  2. The new ReplicaSet creates the new Pods.

  3. The old ReplicaSet gradually scales down.

  4. The old ReplicaSet is retained for possible rollback.

Check the ReplicaSets:

kubectl get rs

You may see output similar to:

NAME                         DESIRED   CURRENT   READY
nginx-deployment-old-hash    0         0         0
nginx-deployment-new-hash    3         3         3

The actual ReplicaSet names contain generated hashes.

The old ReplicaSet has zero replicas, while the new ReplicaSet manages the three running Pods.

This is how Kubernetes keeps the previous Deployment configuration available for rollback.

9. Check Deployment Rollout History

Kubernetes keeps a history of Deployment revisions.

A revision represents a change to the Deployment's Pod template.

For example, changing the container image or adding a readiness probe creates a new revision.

Check the history:

kubectl rollout history deployment/nginx-deployment

Example output:

REVISION  CHANGE-CAUSE
1         <none>
2         Update NGINX to 1.28

The actual revision numbers and change-cause values depend on the changes made to your Deployment.

To view details of a particular revision:

kubectl rollout history deployment/nginx-deployment --revision=2

Replace 2 with a revision that exists in your Deployment history.

Why can CHANGE-CAUSE appear on multiple revisions?

The kubernetes.io/change-cause annotation is stored in the Deployment's metadata.

When a later revision is created, that annotation may still be present. As a result, the same change-cause description can appear against more than one revision.

It is a useful description, but it should not be treated as a unique revision identifier.

Important: Add or update the annotation before making a Pod-template change if you want to record a meaningful description for that change.

10. Roll Back a Deployment

Suppose you updated your application to NGINX 1.28.

After the update, you discover that the new version is causing problems.

You want to return to the previous working version.

This is called a rollback.

Kubernetes allows us to undo a Deployment rollout.

Run:

kubectl rollout undo deployment/nginx-deployment

Check the rollout:

kubectl rollout status deployment/nginx-deployment

Verify the image:

kubectl get deployment nginx-deployment \
  -o jsonpath='{.spec.template.spec.containers[0].image}'

If the previous version was NGINX 1.27, the output should be:

nginx:1.27

Roll back to a specific revision

You can also select a particular revision.

First, check the available history:

kubectl rollout history deployment/nginx-deployment

Then use a revision that exists in the history:

kubectl rollout undo deployment/nginx-deployment --to-revision=2

Here, 2 is only an example. Check your actual history before running the command.

Important for this lab: Do not run the example command above. In the sequence used in this article, revision 2 represents the NGINX 1.28 template. Running it would restore NGINX 1.28 instead of keeping the rolled-back NGINX 1.27 version.

A rollback creates a new revision. Revision numbers may change after a rollback, and older revision numbers may disappear from the displayed history.

11. Understand YAML Drift

Let's understand an important situation that can happen when using both YAML files and kubectl commands.

Imagine you had updated your local YAML file to use NGINX 1.28, but later rolled back the live Deployment to NGINX 1.27.

Your configurations would look like this:

Local YAML file:

image: nginx:1.28

Live Deployment:

image: nginx:1.27

This difference is called configuration drift.

What happens if you apply the YAML file again?

kubectl apply -f nginx-deployment.yaml

Kubernetes will apply the configuration from the file. The image can change back to NGINX 1.28.

Do not apply this hypothetical example in our lab. Our YAML file still contains nginx:1.27, which matches the live Deployment after the rollback.

To avoid configuration drift, keep your YAML file updated whenever you intentionally change the Deployment configuration.

This is especially important when managing Kubernetes resources through Git and CI/CD pipelines.

12. Add a Readiness Probe

So far, Kubernetes has been checking whether the Pods are running.

But is a running container always ready to receive application traffic?

Not necessarily.

A container can be running while the application inside it is still starting.

A readiness probe helps Kubernetes determine whether a container is ready to receive traffic.

Let's add a readiness probe to our NGINX Deployment.

Update the complete nginx-deployment.yaml file with the following content:

apiVersion: apps/v1
kind: Deployment

metadata:
  name: nginx-deployment

spec:
  replicas: 3

  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600

  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%

  selector:
    matchLabels:
      app: nginx

  template:
    metadata:
      labels:
        app: nginx

    spec:
      containers:
        - name: nginx
          image: nginx:1.27
          ports:
            - containerPort: 80

          readinessProbe:
            httpGet:
              path: /
              port: 80
            initialDelaySeconds: 5
            periodSeconds: 5

Let's understand the readiness probe settings:

FieldMeaning
readinessProbeDefines the readiness check
httpGetChecks the application using an HTTP request
path: /Sends the request to the root path
port: 80Uses port 80
initialDelaySeconds: 5Waits 5 seconds before the first check
periodSeconds: 5Performs the check every 5 seconds

Before applying the YAML, add a change-cause annotation:

kubectl annotate deployment nginx-deployment \
  kubernetes.io/change-cause="Add readiness probe" \
  --overwrite

Apply the updated YAML:

kubectl apply -f nginx-deployment.yaml

Check the rollout:

kubectl rollout status deployment/nginx-deployment

Check the rollout history:

kubectl rollout history deployment/nginx-deployment

The readiness probe changes the Pod template, so Kubernetes creates a new revision.

Verify the readiness probe:

kubectl get deployment nginx-deployment \
  -o jsonpath='{.spec.template.spec.containers[0].readinessProbe}'

You should see the HTTP GET configuration in the output.

Finally, check the Pods:

kubectl get pods -l app=nginx

The three Pods should show 1/1 under READY and Running under STATUS once they are ready.

main Deployment

Remember: A readiness probe controls whether a Pod is considered ready to receive traffic. A failed readiness probe does not restart the container. Liveness probes are used for checking whether a container should be restarted.

13. Troubleshooting: ImagePullBackOff

What happens if Kubernetes cannot download the container image?

For example, imagine you provide an image name that does not exist.

Let's create a separate namespace for troubleshooting.

kubectl create namespace deployment-troubleshooting

Create a Deployment with an incorrect image:

kubectl create deployment nginx-broken \
  --image=nginx:does-not-exist \
  -n deployment-troubleshooting

Check the Pods:

kubectl get pods -n deployment-troubleshooting

You may see:

NAME                              READY   STATUS
nginx-broken-xxxxxxxxxx-xxxxx     0/1     ImagePullBackOff

The generated Pod name will be different in your cluster.

Sometimes ErrImagePull appears first. After repeated attempts, Kubernetes commonly changes the status to ImagePullBackOff.

To understand the problem, describe the Pod:

kubectl describe pod POD_NAME -n deployment-troubleshooting

Replace POD_NAME with the actual Pod name from kubectl get pods.

Look at the Events section. You may find an error indicating that the image could not be found.

Fix the incorrect image

Update the Deployment with a valid image:

kubectl set image deployment/nginx-broken nginx=nginx:1.27 \
  -n deployment-troubleshooting

Check the rollout:

kubectl rollout status deployment/nginx-broken \
  -n deployment-troubleshooting

Verify:

kubectl get pods -n deployment-troubleshooting

The new Pod should eventually become 1/1 Running.

14. Troubleshooting: CrashLoopBackOff

Another common problem is CrashLoopBackOff.

Imagine your application starts but exits with an error.

Kubernetes tries to restart the container, but the container keeps failing.

After repeated failures, Kubernetes uses an increasing delay between restart attempts. This is called back-off.

Let's create a Deployment that intentionally fails.

Create a file named crash-demo.yaml:

apiVersion: apps/v1
kind: Deployment

metadata:
  name: crash-demo
  namespace: deployment-troubleshooting

spec:
  replicas: 1

  selector:
    matchLabels:
      app: crash-demo

  template:
    metadata:
      labels:
        app: crash-demo

    spec:
      containers:
        - name: busybox
          image: busybox
          command: ["sh", "-c"]
          args:
            - echo "Application started"; echo "Database connection failed"; exit 1

This example uses BusyBox to print two messages and then exit with status code 1.

Exit code 1 indicates that the command failed.

Apply the YAML file:

kubectl apply -f crash-demo.yaml

Check the Pods:

kubectl get pods -n deployment-troubleshooting

You should eventually see CrashLoopBackOff in the STATUS column.

Describe the Pod

kubectl describe pod POD_NAME -n deployment-troubleshooting

Replace POD_NAME with the actual Pod name.

Look for:

  • Last State

  • The container's Waiting reason

  • Exit Code

  • Restart Count

  • Events

You may see the container terminated with exit code 1 and a Waiting reason of CrashLoopBackOff.

Check container logs

kubectl logs POD_NAME -n deployment-troubleshooting

The output should contain:

Application started
Database connection failed

To view logs from the previous container instance:

kubectl logs POD_NAME --previous -n deployment-troubleshooting

The --previous option is useful when a container has restarted and you need to inspect its earlier logs.

Important: The Pod's STATUS column can show CrashLoopBackOff, while its Pod phase may still be Running. This is because the Pod exists, but its container is repeatedly failing and restarting.

15. Understand ProgressDeadlineExceeded

A Deployment can sometimes get stuck during an update.

For example:

  • New Pods cannot start.

  • Images cannot be downloaded.

  • Readiness checks keep failing.

  • New replicas do not become available.

Kubernetes monitors Deployment progress.

By default, progressDeadlineSeconds is 600 seconds.

If Kubernetes does not observe sufficient progress within the configured deadline, the Deployment reports a condition with the reason:

ProgressDeadlineExceeded

This does not mean Kubernetes automatically rolls back the Deployment.

You must inspect the problem and decide how to fix it or roll back.

Useful commands:

kubectl describe deployment nginx-deployment
kubectl get pods
kubectl get events

These commands help you investigate the Deployment, Pods and cluster events.

16. Important Kubernetes Deployment Commands

Here is a quick reference for the commands used in this article.

CommandPurpose
kubectl apply -f nginx-deployment.yamlCreate or update a Deployment using YAML
kubectl get deploymentsList Deployments
kubectl get podsList Pods
kubectl get rsList ReplicaSets
kubectl describe deployment nginx-deploymentView Deployment details
kubectl get deployment nginx-deployment -o yamlView the Deployment YAML
kubectl scale deployment nginx-deployment --replicas=5Change the replica count
kubectl annotate deployment nginx-deployment kubernetes.io/change-cause="Update" --overwriteAdd or update a change-cause annotation
kubectl set image deployment/nginx-deployment nginx=nginx:1.28Update the container image
kubectl rollout status deployment/nginx-deploymentCheck rollout progress
kubectl rollout history deployment/nginx-deploymentView revision history
kubectl rollout undo deployment/nginx-deploymentRoll back the latest rollout
kubectl rollout undo deployment/nginx-deployment --to-revision=2Roll back to a selected revision (example; check history first)
kubectl rollout restart deployment/nginx-deploymentRestart Pods managed by the Deployment
kubectl rollout pause deployment/nginx-deploymentPause a rollout
kubectl rollout resume deployment/nginx-deploymentResume a paused rollout
kubectl create namespace deployment-troubleshootingCreate a troubleshooting namespace
kubectl describe pod POD_NAMEInspect a Pod
kubectl logs POD_NAMEView container logs
kubectl logs POD_NAME --previousView logs from the previous container instance

Replace example resource names and Pod names with the actual names in your cluster. Include the namespace option when working with resources outside the default namespace.

17. Clean Up the Resources

After completing the hands-on exercise, delete the resources created for this article.

Delete the main Deployment:

kubectl delete deployment nginx-deployment

Delete the troubleshooting namespace:

kubectl delete namespace deployment-troubleshooting

Deleting the namespace also removes the resources created inside it, including the troubleshooting Deployments and their Pods.

The local YAML files remain on your computer. If you no longer need them, you can remove them separately:

rm nginx-deployment.yaml crash-demo.yaml

Note: Do not delete resources that you still need for other labs.

18. What Did We Learn?

Let's quickly recap what we covered.

  • A Deployment manages application Pods.

  • A ReplicaSet maintains the desired number of Pods.

  • Scaling changes the number of replicas.

  • Rolling Updates gradually replace old Pods with new Pods.

  • Recreate removes old Pods before creating new ones.

  • Rollout history helps us inspect Deployment revisions.

  • Rollbacks help us return to a previous Pod template.

  • A readiness probe checks whether an application is ready to receive traffic.

  • ImagePullBackOff commonly occurs when Kubernetes cannot download an image.

  • CrashLoopBackOff occurs when a container repeatedly fails and restarts.

  • ProgressDeadlineExceeded indicates that a Deployment has not made sufficient progress within its configured deadline.

One important point to remember: A rollback creates a new revision. It does not simply restore the old revision number.

Conclusion

Kubernetes Deployments make application management easier by handling Pod creation, scaling, updates and rollbacks.

Instead of manually replacing Pods whenever an application changes, we can define the desired state and let Kubernetes work towards it.

Understanding Deployments is an important step in learning Kubernetes because many real-world applications use Deployments to manage stateless workloads.

In the next article, we will learn about Kubernetes Services and understand how applications communicate with Pods even when Pod IP addresses change.

What's Next?

Now that we understand how Kubernetes Deployments manage ReplicaSets, update applications and roll back changes, let's move to the next concept: Kubernetes Services.

We will learn:

  • What a Kubernetes Service is and why we need it.

  • How a Service provides a stable way to access Pods.

  • The difference between ClusterIP, NodePort and LoadBalancer.

  • How to expose an application running inside a Kubernetes cluster.

  • How Services continue routing traffic when Pods are replaced.

Kubernetes Learning Series

  1. Kubernetes Basics Through a Restaurant Kitchen

  2. Kubernetes Pods Explained: Architecture, Lifecycle and Practical Examples

  3. Kubernetes ReplicaSets Explained: Maintaining the Desired State

  4. Kubernetes Deployments Explained: Rolling Updates and Rollbacksย - You are here.

  5. How Pods, ReplicaSets and Deployments Work Together - upcoming article.

Remember: Kubernetes helps us manage applications by maintaining the desired state, even when Pods change.

📚 Sources
  1. kubernetes.io/docs/concepts/workloads/controllers/deployment/
  2. kubernetes.io/docs/tasks/run-application/update-deployment-rolling/
  3. kubernetes.io/docs/concepts/workloads/controllers/replicaset/
  4. kubernetes.io/docs/concepts/workloads/pods/probes/
  5. kubernetes.io/docs/concepts/containers/images/
Read next →Kubernetes Pods: Architecture, Lifecycle & Examples⏱ 16 min

🛍️ Malathi Shetty's shelf

Courses, cohorts and more from the author
Malathi Shetty
Written by Malathi Shetty
Cloud & DevOps | AWS โ€ข Azure โ€ข Docker โ€ข Kubernetes โ€ข CI/CD
🏅 Contributor writer

Iโ€™m a Senior Software Quality Engineer learning and transitioning into Cloud & DevOps. Iโ€™m currently working hands-on with AWS, Azure, Linux, Docker, Kubernetes, CI/CD and DevSecOps. I share what I learn through practical projects and real-world examples.

Visit my ByteStoreLearning, building and sharing practical Cloud & DevOps experiences.

🌟 See Malathi Shetty's full profile: blogs, work and verified certificates

Get Malathi Shetty's next blog by email

One email a week when they publish. Add the weekly Best of ByteBlog too. No spam, unsubscribe with one click.

We email you once to confirm. By subscribing you agree to the privacy policy.

💬 Comments (0)

Top · Newest

Sign in to comment

More from Malathi Shetty

🛍️✨ From the author · ByteStore1:1 DevOps Project & Learning Discussionโ‚น500View offer →