(Day 30) Task : Understanding Deployment Objects and Rollbacks in Kubernetes :-

Aditya SharmaAditya Sharma
5 min read

In our journey through Kubernetes, we’ve already explored pods, replication controllers, and services. Today, let’s dive deep into a powerful Kubernetes object that plays a vital role in managing application lifecycles: the Deployment object. We'll also understand how Kubernetes helps us to rollback in case of failure, making it a reliable platform for continuous delivery.

What is a Kubernetes Deployment?

  • A Deployment in Kubernetes is a higher-level abstraction that manages ReplicaSets and, in turn, manages Pods. It provides declarative updates to applications along with capabilities like rolling updates and rollbacks.Think of it as a way to describe the desired state for your application, and Kubernetes ensures the cluster eventually reaches that state.

  • A deployment is an object. It's an updated version of RS and it has more facilities and power.

  • Deployment objects act as task supervisors for pods. It won't supervise itself, but it has to supervise RS and RS control pods. So this is how deployment objects have to control over how and when a new pod is rolled out, updated or rolled back to a previous state.

  • Replication Controller and Replica set is not able to do updates and rollback application.

    • So , Deployment object comes into Play it manages Rs and Rs manages pods.

Why Use a Deployment?

  • Roll out replicas of an app in a controlled manner

  • Support for self-healing (via ReplicaSets)

  • Enable rolling updates to prevent downtime

  • Easy rollback to a previous version

  • Declarative configuration (stored in version control)

Use CaseCommand / MethodPurpose
Rollout ReplicaSetkubectl apply -fCreate/Manage replicas
Change Port/ImageEdit Deployment YAMLApply new state
Rollbackkubectl rollout undoRevert to stable version
Scale Upkubectl scaleHandle high traffic
Pause Deploymentkubectl rollout pause/resumeApply multiple changes safely
Clean Old RAskubectl delete rsFree up space, reduce clutter

Deployment YAML Manifest Example

Here’s a sample Deployment :

kind: Deployment
apiVersion: apps/v1
metadata:
  name: mydeployment
spec:
  replicas: 2
  selector:
    matchLabels:
      name: deployment
  template:
    metadata:
      name: testpod
      labels:
        name: deployment
    spec:
      containers:
        - name: c00
          image: ubuntu
          command: ["/bin/bash", "-c" "while true; do echo aditya-sharma; sleep 5; done"]

Breakdown of the YAML

FieldDescription
apiVersionUses apps/v1, the current stable version for Deployments.
kindType of object, here it's Deployment.
metadataName and labels for identifying the Deployment.
replicasNumber of desired Pod replicas.
selectorDefines how the Deployment finds Pods to manage (must match the template labels).
templateThe Pod template to create Pods.
containersDetails of container including name, image, and exposed ports.

Rollback Strategies in Kubernetes Deployments :-

Kubernetes provides built-in rollback mechanisms to ensure application reliability during updates.

There are two rollback types in Kubernetes:

  1. Automatic Rollback (on failure)

  2. Manual Rollback (to previous or specific revision)

1. Automatic Rollback on Failure :

If a rollout fails due to a crash, failed probes, or deployment errors (like a bad image), Kubernetes can automatically stop the rollout and revert to the last known good state.

This depends on the health checks (readiness/liveness probes). If your new Pods aren’t healthy, the Deployment will pause and potentially roll back.

Can monitor this with:

kubectl rollout status deployment/<deployment-name>

If the rollout fails, the system may trigger a rollback, or you can do it manually (recommended for full control).

2. Manual Rollback to Previous Version :

If you simply want to undo the most recent rollout and go back to the last stable version, use:

kubectl rollout undo deployment/<deployment-name>

This will revert the Deployment to the immediately previous revision.

3. Manual Rollback to a Specific Revision :

Sometimes, you might want to roll back to a specific version instead of just the last one. Kubernetes tracks the revision history, and you can rollback to any revision number.

  1. Check Deployment History:
kubectl rollout history deployment/<deployment-name>
  1. Rollback to Specific Revision:
kubectl rollout undo deployment/<deployment-name> --to-revision=2

Inspecting a Deployment :-

To inspect the status of your deployments, you can run:

kubectl get deployments

You'll see an output like:

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           5m

Column Breakdown

ColumnMeaning
NAMEName of the Deployment
READYHow many Pods are running and healthy (<ready>/<desired>)
UP-TO-DATEHow many Pods have been updated to match the latest Deployment
AVAILABLEHow many Pods are available to serve traffic
AGEHow long the Deployment has existed

Kubernetes Deployment: Essential Command Cheatsheet :-

1. Check if the Deployment Was Created :

kubectl get deployments

This lists all deployments in the current namespace.

To get a specific deployment :

kubectl get deployment <deployment-name>

To view detailed information :

kubectl describe deployment <deployment-name>

2. Check ReplicaSet Created by Deployment :

kubectl get rs

3. Check the Pods Created by the Deployment :

kubectl get pods

4. Rollout Commands :

Check Rollout Status :

kubectl rollout status deployment/<deployment-name>

This shows whether the rollout is complete or still in progress.

View Rollout History

kubectl rollout history deployment/<deployment-name>

To view a specific revision:

kubectl rollout history deployment/<deployment-name> --revision=2

Undo the Last Deployment

kubectl rollout undo deployment/<deployment-name>

Undo to a Specific Revision

kubectl rollout undo deployment/<deployment-name> --to-revision=2

0
Subscribe to my newsletter

Read articles from Aditya Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Sharma
Aditya Sharma

DevOps Enthusiast | Python | Chef | Docker | GitHub | Linux | Shell Scripting | CI/CD & Cloud Learner | AWS