Rollouts and Versioning in deployments

When you first create a deployment, it triggers a rollout, a new rollout creates a new deployment.

Understanding Updates and Rollbacks in Kubernetes Deployments

Introduction

Kubernetes deployments provide a mechanism to manage application updates and rollbacks effectively. This document explains key concepts, strategies, and commands related to rollouts, updates, and rollbacks in a clear and detailed manner.

Key Concepts

1. Rollouts and Versions:

  • A rollout occurs when a deployment is created or updated. Each rollout creates a deployment revision, enabling tracking of changes and rollbacks.

2. Deployment Strategies:

  • Recreate Strategy: Stops all old versions and starts the new ones, causing downtime.
  • Rolling Update Strategy (default): Gradually replaces old versions with new ones, ensuring application availability.

Commands and Their Usage

Creating a Deployment

Starts a new deployment.

Command:

kubectl create -f <deployment-file.yaml>

Example:

kubectl create -f my-deployment.yaml

Check Rollout Status

Shows the progress of a deployment rollout.

Command:

kubectl rollout status deployment/<deployment-name>

Example:

kubectl rollout status deployment/my-app

View Rollout History

Lists all revisions of a deployment, showing changes made.

Command:

kubectl rollout history deployment/<deployment-name>

Example:

kubectl rollout history deployment/my-app

Update the Deployment (Using Apply)

Apply changes from a new YAML file.

Command:

kubectl apply -f <updated-deployment-file.yaml>

Example:

kubectl apply -f updated-deployment.yaml

Update the Deployment (Using Set Image)

Set a new container image directly.

Command:

kubectl set image deployment/<deployment-name> <container-name>=<new-image>

Example:

kubectl set image deployment/my-app my-container=my-image:v2

Rollback the Deployment

Reverts to the previous version of the deployment.

Command:

kubectl rollout undo deployment/<deployment-name>

Example:

kubectl rollout undo deployment/my-app

Inspect Deployment Details

Provides detailed information about the deployment.

Command:

kubectl describe deployment/<deployment-name>

Example:

kubectl describe deployment/my-app

List Replica Sets

Shows all replica sets for the deployment.

Command:

kubectl get replicasets

Example:

kubectl get replicasets

Delete a Deployment

Removes the deployment entirely.

Command:

kubectl delete deployment/<deployment-name>

Example:

kubectl delete deployment/my-app

Deployment Lifecycle and Flow

1. Deployment Creation:

  • Trigger rollout → Create ReplicaSet → Scale Pods.

2. Update Process:

  • Modify configuration → Apply changes → Create new ReplicaSet → Rolling update.

3. Rollback:

  • Undo rollout → Scale down current ReplicaSet → Scale up previous ReplicaSet.
Aspectkubectl setkubectl apply (Update)
PurposeUsed for imperative changes (quick updates)Used for declarative updates (updates the configuration in YAML files)
Use CaseQuick, temporary changes like updating image, replicas, etc.Updating the resource definition, keeping it in sync with the YAML file
Changes to YAMLDoes not modify the local YAML file. Updates are applied directly to the running resources.Modifies the YAML file (if you update the file locally) and applies changes to the cluster.
Version ControlNot tracked in source control. Only affects live resources.Tracked in source control as part of the YAML file.
When to UseWhen you need a quick update or when working in a testing or development environment.When you need to manage changes declaratively, track them over time, and maintain configuration in source control.
Examples of ChangesChanging container image, scaling replicas, setting environment variables directly in the cluster.Modifying container image, replicas, or any other setting in the YAML file and applying the update using kubectl apply.
ReusabilityUpdates are not stored in YAML files, so it is not easy to reapply the same changes later.The updated configuration is stored in YAML files, making it easy to apply changes across environments.
Consistency Across ClustersChanges are applied to the current cluster only; they need to be manually replicated elsewhere.Changes are part of version-controlled YAML files, which can be applied across multiple clusters for consistency.
Automation/ScriptingIdeal for use in scripts and automated processes where the local file does not need to be modified.Better for managing configuration as code, especially in a CI/CD pipeline or a multi-cluster environment.
Example Commandkubectl set image deployment/my-app my-container=my-app:v2kubectl apply -f deployment.yaml (after editing the YAML file)