{"id":22960,"date":"2024-06-22T00:08:25","date_gmt":"2024-06-21T21:08:25","guid":{"rendered":"https:\/\/kifarunix.com\/?p=22960"},"modified":"2024-06-22T21:42:22","modified_gmt":"2024-06-22T18:42:22","slug":"understanding-deployments-in-kubernetes-a-comprehensive-guide","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/understanding-deployments-in-kubernetes-a-comprehensive-guide\/","title":{"rendered":"Understanding Deployments in Kubernetes: A Comprehensive Guide"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1053\" height=\"592\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2024\/06\/kubernetes-deployments.png?v=1719003947\" alt=\"Understanding Deployments in Kubernetes: A Comprehensive Guide\" class=\"wp-image-22975\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2024\/06\/kubernetes-deployments.png?v=1719003947 1053w, https:\/\/kifarunix.com\/wp-content\/uploads\/2024\/06\/kubernetes-deployments-768x432.png?v=1719003947 768w\" sizes=\"(max-width: 1053px) 100vw, 1053px\" \/><\/figure>\n\n\n\n<p>This tutorial provides a comprehensive guide on understanding deployments in Kubernetes. <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/controllers\/deployment\/\" target=\"_blank\" rel=\"noreferrer noopener\">Deployments<\/a> in Kubernetes play a crucial role in managing containerized applications, ensuring scalability, availability, and efficient updates. This comprehensive guide aims to provide a clear understanding of what deployments are, how to create and manage them, and their essential functionalities like rollout and rollback.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#comprehensive-guide-understanding-deployments-in-kubernetes\">Comprehensive Guide Understanding Deployments in Kubernetes<\/a><ul><li><a href=\"#what-is-a-deployment-in-kubernetes\">What is a Deployment in Kubernetes?<\/a><\/li><li><a href=\"#key-concepts-in-kubernetes-deployments\">Key Concepts in Kubernetes Deployments?<\/a><\/li><li><a href=\"#creating-a-kubernetes-deployment\">Creating a Kubernetes Deployment<\/a><ul><li><a href=\"#creating-kubernetes-deployment-using-declarative-approach\">Creating Kubernetes Deployment using Declarative Approach<\/a><\/li><li><a href=\"#creating-kubernetes-deployment-using-imperative-approach\">Creating Kubernetes Deployment using Imperative Approach<\/a><\/li><\/ul><\/li><li><a href=\"#viewing-and-managing-deployments\">Viewing and Managing Deployments<\/a><ul><li><a href=\"#listing-available-deployments\">Listing Available Deployments:<\/a><\/li><li><a href=\"#describing-a-deployment\">Describing a Deployment<\/a><\/li><\/ul><\/li><li><a href=\"#scaling-deployments\">Scaling Deployments<\/a><ul><li><a href=\"#scaling-up-a-deployment\">Scaling Up a Deployment:<\/a><\/li><li><a href=\"#scaling-down-a-deployment\">Scaling Down a Deployment:<\/a><\/li><\/ul><\/li><li><a href=\"#deployment-rollout-and-rollback\">Deployment Rollout and Rollback<\/a><ul><li><a href=\"#rolling-updates\">Rolling Updates:<\/a><\/li><li><a href=\"#monitoring-rollout-status\">Monitoring Rollout Status:<\/a><\/li><li><a href=\"#rollback-to-previous-version\">Rollback to Previous Version:<\/a><\/li><\/ul><\/li><li><a href=\"#deleting-kubernetes-deployment\">Deleting Kubernetes Deployment<\/a><\/li><li><a href=\"#conclusion\">Conclusion<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"comprehensive-guide-understanding-deployments-in-kubernetes\">Comprehensive Guide Understanding Deployments in Kubernetes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-is-a-deployment-in-kubernetes\">What is a Deployment in Kubernetes?<\/h3>\n\n\n\n<p>In Kubernetes, <strong>Deployment<\/strong> is a high-level controller object that defines the desired state for a set of replica Pods. It manages the deployment and scaling of a set of Pods, ensuring that the specified number of replicas are running at any given time. Deployments are declarative and provide an abstraction layer above Pods, allowing for automated rollouts and rollbacks, scaling, and self-healing capabilities.<\/p>\n\n\n\n<p>Deployments are categorized as <strong>workloads<\/strong> in Kubernetes, representing a higher-level abstraction for managing Pods and ReplicaSets. As a workload type, Deployments encapsulate the logic for managing the lifecycle of application instances, ensuring they run as intended and can be scaled, updated, or rolled back seamlessly.<\/p>\n\n\n\n<p>With the Kubernetes Deployment all you need to do is to either declaratively or imperatively define the desired state of your application by specifying details like the <strong>name<\/strong> of the deployment, <strong>container image<\/strong>, <strong>number of replicas<\/strong> (number of application to run concurrently), <strong>resource requirements<\/strong> like how much CPU, memory, and storage each replica needs and maybe <strong>environment variables<\/strong> (configuration settings for your application). The Deployment controller, then takes care of the rest and ensures your app pods reach and maintain that desired state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"key-concepts-in-kubernetes-deployments\">Key Concepts in Kubernetes Deployments?<\/h3>\n\n\n\n<p>What are the key concepts or components of a deployment?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Pods<\/strong>: These are the basic unit of deployment in Kubernetes, comprising one or more containers that share storage and networking.<\/li>\n\n\n\n<li><strong>ReplicaSets<\/strong>: This is a Kubernetes resource that defines a specific set of replica pods and ensures that they are running in the cluster at any given time. It essentially guarantees a desired state for your application by maintaining the specified number of identical pods. Deployments manage ReplicaSets to achieve the desired state.<\/li>\n\n\n\n<li><strong>Labels and Selectors<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Labels<\/strong> are key-value pairs attached to Kubernetes objects like Pods to define identifying attributes. They are like tags for your pods. You can add multiple labels to a pod, allowing for granular categorization.<\/li>\n\n\n\n<li><strong>Selectors<\/strong>: Selectors are used by Deployments to filter and identify pods based on their labels. They are like the search queries targeting pods with specific labels. Selectors use a key-value matching syntax similar to labels.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Strategy<\/strong>: Deployment strategy defines how to replace old Pods with new ones. There are two types of strategies:\n<ul class=\"wp-block-list\">\n<li><strong>Recreate<\/strong> strategy:  This strategy requires that all existing Pods are killed before new ones are created. Until all previous pods are terminated, new pods wont be created. This is the default strategy.<\/li>\n\n\n\n<li><strong>RollingUpdate<\/strong>: This strategy updates Pods incrementally, ensuring that a specified number of Pods are available throughout the deployment process. It gradually replaces old Pods with new ones, maintaining application availability<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"creating-a-kubernetes-deployment\">Creating a Kubernetes Deployment<\/h3>\n\n\n\n<p>Now that you have an understanding of what a Kubernetes deployment is and the core concepts, let&#8217;s check how to create a Kubernetes Deployment.<\/p>\n\n\n\n<p>There are different approaches you can use to create a Kubernetes Deployment.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Declarative approach using YAML Manifests files<\/strong>: The declarative approach involves creating a Deployment via a configuration file in YAML format, here in called, a manifest file.<\/li>\n\n\n\n<li><strong>Imperative Approach<\/strong>: This involves the use <strong>kubectl create deployment<\/strong> command to create your deployment.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"creating-kubernetes-deployment-using-declarative-approach\">Creating Kubernetes Deployment using Declarative Approach<\/h4>\n\n\n\n<p>As already stated, you can use a manifest file to define specific configuration of your Kubernetes deployment.<\/p>\n\n\n\n<p>Here is an example of a manifest YAML file to create a deployment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat my-app.yaml<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: my-app-deployment\nspec:\n  replicas: 2\n  selector:\n    matchLabels:\n      app: my-app\n  template:\n    metadata:\n      labels:\n        app: my-app\n    spec:\n      containers:\n      - name: my-app\n        image: my-app-image:latest\n        port:\n        - containerPort: 80\n<\/code><\/pre>\n\n\n\n<p>Where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>apiVersion: apps\/v1<\/strong>: specifies that the manifest conforms to the deployments API version &#8220;apps\/v1&#8221;.<\/li>\n\n\n\n<li><strong>kind: Deployment<\/strong>: declares that the resource being defined is a &#8220;Deployment&#8221;.<\/li>\n\n\n\n<li><strong>metadata:<\/strong> provides metadata about the deployment, including its name.<\/li>\n\n\n\n<li><strong>name: my-app-deployment:<\/strong> This assigns the name &#8220;my-app-deployment&#8221; to the deployment.<\/li>\n\n\n\n<li><strong>spec:<\/strong> This section defines the desired state of the deployment, specifying how you want your application to run. Here&#8217;s what&#8217;s included:\n<ul class=\"wp-block-list\">\n<li><strong>replicas: 2:<\/strong> This sets the desired number of replicas for your application. In this case, you want two identical pods running for your &#8220;my-app&#8221;.<\/li>\n\n\n\n<li><strong>selector:<\/strong> This section defines how the deployment identifies the pods it manages.\n<ul class=\"wp-block-list\">\n<li><strong>matchLabels:<\/strong> This key-value pair specifies the label selector. The deployment will manage pods that have the label <code class=\"\">app: my-app<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>template:<\/strong> This section defines the template for the pods that will be created by the deployment. Any pods created by this deployment will be based on this template.\n<ul class=\"wp-block-list\">\n<li><strong>metadata:<\/strong> This section defines metadata for the pods, including labels.\n<ul class=\"wp-block-list\">\n<li><strong>labels: app: my-app:<\/strong> This assigns the label <code class=\"\">app: my-app<\/code> to the pods created by the deployment. This label allows the deployment to identify and manage these pods using the selector defined earlier.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>spec:<\/strong> This section defines the container configuration for the pods.\n<ul class=\"wp-block-list\">\n<li><strong>containers:<\/strong> This defines the containers that will be running inside each pod. Here, we have only one container:\n<ul class=\"wp-block-list\">\n<li><strong>name: my-app:<\/strong> This assigns the name &#8220;my-app&#8221; to the container within the pod.<\/li>\n\n\n\n<li><strong>image: my-app-image:latest:<\/strong> This specifies the container image to use. The image name is &#8220;my-app-image&#8221; and the tag is &#8220;latest&#8221;.<\/li>\n\n\n\n<li><strong>port:<\/strong> This section defines the ports the container exposes. Here, you have one port:\n<ul class=\"wp-block-list\">\n<li><strong>containerPort: 80:<\/strong> This exposes port 80 within the container. This is likely where your application listens for traffic.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>In essence, this deployment configuration creates two pods, each running a container based on the image &#8220;my-app-image:latest&#8221; in the default namespace. The pods are labeled with <code class=\"\">app: my-app<\/code>, allowing the deployment to manage them. The containers within the pods expose port 80, where your application listens for incoming requests.<\/p>\n\n\n\n<p>If you want to create the deployment on a different namespace other than the default namespace, you can specify the name of the respective namespace under the metadata section;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: my-app-deployment\n  <strong>namespace: my-namespace<\/strong>\n<\/code><\/pre>\n\n\n\n<p>To deploy the application defined in the YAML manifest file, use the <strong>kubectl apply<\/strong> command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl apply -f my-app.yaml<\/pre>\n\n\n\n<p>This command instructs Kubernetes to create or update resources defined in the <strong>my-app.yaml<\/strong> file.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"creating-kubernetes-deployment-using-imperative-approach\">Creating Kubernetes Deployment using Imperative Approach<\/h4>\n\n\n\n<p>In some scenarios, you might need to use imperative commands to manage Deployments. For example, to create a Deployment imperatively in the default namespace:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl create deployment example-deployment --image=nginx:latest --replicas=3<\/pre>\n\n\n\n<p>If you want to create the deployment in a different namespace, specify the namespace using the <strong>-n NS|&#8211;namespace=NS<\/strong> where NS is the name of the specific namespace.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"viewing-and-managing-deployments\">Viewing and Managing Deployments<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"listing-available-deployments\">Listing Available Deployments:<\/h4>\n\n\n\n<p>To view Deployments in Kubernetes, use:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl get deployments<\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>NAME                 READY   UP-TO-DATE   AVAILABLE   AGE\nexample-deployment   3\/3     3            3           11m\n<\/code><\/pre>\n\n\n\n<p>This command lists all Deployments along with their current status, such as the number of desired, current, and available replicas in the default namespace.<\/p>\n\n\n\n<p>If you want to list Deployments in other namespaces, pass &#8211;<strong>n|&#8211;namespace<\/strong> option specify the namespace you want. For example, to list deployments in the <strong>apps<\/strong> namespace;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get deployments -n apps<\/code><\/pre>\n\n\n\n<p>Demystifying the output of <strong>kubectl get deployments<\/strong> command. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>NAME<\/strong>: This column shows the name of the deployment, in this case, it&#8217;s <code>example-deployment<\/code>.<\/li>\n\n\n\n<li><strong>READY<\/strong>: This column indicates the current state of the deployment&#8217;s pods. The format <code>3\/3<\/code> means:\n<ul class=\"wp-block-list\">\n<li>The deployment has a desired number of 3 replicas (specified in the deployment configuration).<\/li>\n\n\n\n<li>All 3 replicas are currently running and ready.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>UP-TO-DATE<\/strong>: This column shows how many of the deployed pods are up to date with the latest deployment configuration. A value of <code>3<\/code> means all 3 pods are running the latest version of the application as defined in the deployment.<\/li>\n\n\n\n<li><strong>AVAILABLE<\/strong>: This column indicates the number of pods that are available for serving traffic. It should match the number of replicas (<code>3<\/code> in this case), meaning all pods are ready to accept requests.<\/li>\n\n\n\n<li><strong>AGE<\/strong>: This column shows the amount of time since the deployment was created.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"describing-a-deployment\">Describing a Deployment<\/h4>\n\n\n\n<p>To get detailed information about a specific Deployment, use <strong>kubectl describe deployment<\/strong> command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl describe deployment example-deployment<\/pre>\n\n\n\n<p>This command provides information about the Deployment&#8217;s configuration, current status, events, and more.<\/p>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>Name:                   example-deployment\nNamespace:              default\nCreationTimestamp:      Fri, 21 Jun 2024 17:26:05 +0000\nLabels:                 app=example-deployment\nAnnotations:            deployment.kubernetes.io\/revision: 1\nSelector:               app=example-deployment\nReplicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable\nStrategyType:           RollingUpdate\nMinReadySeconds:        0\nRollingUpdateStrategy:  25% max unavailable, 25% max surge\nPod Template:\n  Labels:  app=example-deployment\n  Containers:\n   nginx:\n    Image:         nginx:latest\n    Port:          <none>\n    Host Port:     <none>\n    Environment:   <none>\n    Mounts:        <none>\n  Volumes:         <none>\n  Node-Selectors:  <none>\n  Tolerations:     <none>\nConditions:\n  Type           Status  Reason\n  ----           ------  ------\n  Available      True    MinimumReplicasAvailable\n  Progressing    True    NewReplicaSetAvailable\nOldReplicaSets:  <none>\nNewReplicaSet:   example-deployment-5c5699c8df (3\/3 replicas created)\nEvents:\n  Type    Reason             Age   From                   Message\n  ----    ------             ----  ----                   -------\n  Normal  ScalingReplicaSet  31m   deployment-controller  Scaled up replica set example-deployment-5c5699c8df to 3\n<\/code><\/pre>\n\n\n\n<p>The output is a little bit self-explanatory.<\/p>\n\n\n\n<p>Just under the deployment strategy, we have;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>StrategyType<\/strong>: <code>RollingUpdate<\/code><\/li>\n\n\n\n<li><strong>MinReadySeconds<\/strong>: <code>0<\/code> (minimum seconds for which a newly created pod should be ready without any of its containers crashing, for it to be considered available)<\/li>\n\n\n\n<li><strong>RollingUpdateStrategy<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>25% max unavailable<\/code>: During updates, at most 25% of the desired number of replicas can be unavailable.<\/li>\n\n\n\n<li><code>25% max surge<\/code>: During updates, the deployment can increase its size by up to 25% of the desired number of replicas.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"scaling-deployments\">Scaling Deployments<\/h3>\n\n\n\n<p>You can scale the deployments up or down depending on the usage needs.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"scaling-up-a-deployment\">Scaling Up a Deployment:<\/h4>\n\n\n\n<p>To scale a Deployment to increase the number of replicas (e.g., from 3 to 5):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl scale deployment example-deployment --replicas=5<\/pre>\n\n\n\n<p>This command scales the Deployment <code>example-deployment<\/code> to 5 replicas, distributing the workload across more Pods.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"scaling-down-a-deployment\">Scaling Down a Deployment:<\/h4>\n\n\n\n<p>To scale a Deployment down to decrease the number of replicas (e.g., from 5 to 3):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl scale deployment example-deployment --replicas=3<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"deployment-rollout-and-rollback\">Deployment Rollout and Rollback<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"rolling-updates\">Rolling Updates:<\/h4>\n\n\n\n<p><strong>Rollout<\/strong> refers to the process of updating the pods in a Kubernetes Deployment to a new version of your application. Here\u2019s how it typically works:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Updating the Deployment<\/strong>: When you update the Docker image, configuration, or other parameters of your application in a Deployment manifest (usually done via <code>kubectl apply<\/code> or by editing the YAML directly), Kubernetes initiates a rollout.<\/li>\n\n\n\n<li><strong>Pods Replacement<\/strong>: Kubernetes replaces the existing pods (running the old version) with new pods (running the updated version) gradually. It does this by creating new pods based on the updated Deployment specification while gradually terminating the old pods.<\/li>\n\n\n\n<li><strong>Rolling Update Strategy<\/strong>: By default, Deployments use a RollingUpdate strategy, where Kubernetes ensures that a certain number of pods from the old Deployment are running alongside the new pods during the update. This strategy helps in minimizing downtime and ensures that your application remains available throughout the update process.<\/li>\n\n\n\n<li><strong>Monitoring and Progress<\/strong>: Check the monitoring rollout status section below.<\/li>\n<\/ol>\n\n\n\n<p>A sample command to update the image version of the application:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl set image deployment example-deployment nginx=nginx:1.20<\/pre>\n\n\n\n<p>This command updates the Deployment to use the <code>nginx:1.20<\/code> image.<\/p>\n\n\n\n<p>You can annotate this update to set the cause of the change;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl annotate deployment\/example-deployment kubernetes.io\/change-cause=\"Updated nginx image to version 1.20\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"monitoring-rollout-status\">Monitoring Rollout Status:<\/h4>\n\n\n\n<p>You can monitor the rollout progress using <code>kubectl rollout status deployment &lt;deployment-name&gt;<\/code>;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl rollout status deployment example-deployment<\/pre>\n\n\n\n<p>or by viewing the Deployment details (<code>kubectl describe deployment &lt;deployment-name><\/code>). Kubernetes ensures that the desired number of replicas (pods) are available and healthy before moving on to the next step in the rollout.<\/p>\n\n\n\n<p>Sample rollout status command;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>Waiting for deployment \"example-deployment\" rollout to finish: 2 out of 3 new replicas have been updated...\nWaiting for deployment \"example-deployment\" rollout to finish: 2 out of 3 new replicas have been updated...\nWaiting for deployment \"example-deployment\" rollout to finish: 2 out of 3 new replicas have been updated...\nWaiting for deployment \"example-deployment\" rollout to finish: 1 old replicas are pending termination...\nWaiting for deployment \"example-deployment\" rollout to finish: 1 old replicas are pending termination...\ndeployment \"example-deployment\" successfully rolled out\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"rollback-to-previous-version\">Rollback to Previous Version:<\/h4>\n\n\n\n<p><strong>Rollback<\/strong> provides a safety net in case something goes wrong during a deployment update. It allows you to revert to a previous stable version of your application quickly.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Triggering a Rollback<\/strong>: If issues are detected during the rollout (e.g., application crashes, unexpected errors), you can trigger a rollback manually using <code>kubectl rollout undo deployment &lt;deployment-name&gt; --to-revision=N<\/code>.<\/li>\n\n\n\n<li><strong>Reverting to Previous State<\/strong>: Kubernetes will revert the Deployment to the previous known good state by scaling down the new ReplicaSet (created during the rollout) and scaling up the old ReplicaSet. This effectively switches traffic back to the previous version of your application.<\/li>\n\n\n\n<li><strong>Rollback History<\/strong>: Kubernetes keeps track of Deployment revisions, allowing you to rollback to any previous revision if needed. You can view the revision history using <code>kubectl rollout history deployment &lt;deployment-name&gt;<\/code>.<\/li>\n<\/ol>\n\n\n\n<p>When you update your deployment, you will see that the old replicaset is kept for rollback, just in case.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get replicasets -o wide<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>NAME                            DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES       SELECTOR\nexample-deployment-7648d6474    0         0         0       8m52s   nginx        nginx:1.18   app=example-deployment,pod-template-hash=7648d6474\nexample-deployment-77d66d9f6f   3         3         3       7m29s   nginx        nginx:1.20   app=example-deployment,pod-template-hash=77d66d9f6f\n<\/code><\/pre>\n\n\n\n<p>Sample rollback command;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl rollout undo deployment example-deployment<\/pre>\n\n\n\n<p>This command reverts the Deployment to the previous stable version, minimizing downtime and impact on users.<\/p>\n\n\n\n<p>You can monitor the rollback status using the rollout status command above.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl rollout status deployment example-deployment<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>Waiting for deployment \"example-deployment\" rollout to finish: 1 old replicas are pending termination...\nWaiting for deployment \"example-deployment\" rollout to finish: 1 old replicas are pending termination...\ndeployment \"example-deployment\" successfully rolled out\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get replicasets -o wide<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>NAME                            DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES       SELECTOR\nexample-deployment-7648d6474    3         3         3       13m   nginx        nginx:1.18   app=example-deployment,pod-template-hash=7648d6474\nexample-deployment-77d66d9f6f   0         0         0       12m   nginx        nginx:1.20   app=example-deployment,pod-template-hash=77d66d9f6f\n<\/code><\/pre>\n\n\n\n<p>As you can see, the update replicaset is kept!<\/p>\n\n\n\n<p>Similarly, annotate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl annotate deployment\/example-deployment kubernetes.io\/change-cause=\"Reverted nginx image to version 1.18\"<\/code><\/pre>\n\n\n\n<p>You can check the rollout history;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl rollout history deployment example-deployment<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>REVISION  CHANGE-CAUSE\n4         Updated nginx image to version 1.20\n5         Reverted nginx image to version 1.18\n<\/code><\/pre>\n\n\n\n<p>To rollback to specific revision number;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>kubectl rollout undo deployment &lt;deployment-name&gt; --to-revision=N<\/code><\/code><\/pre>\n\n\n\n<p>For example;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl rollout undo deployment example-deployment --to-revision 4<\/code><\/pre>\n\n\n\n<p>If you want to get details about a specific revision;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl rollout history deployment &lt;deployment> --revision Rev_No<\/code><\/pre>\n\n\n\n<p>For example;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl rollout history deployment example-deployment --revision 5<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>deployment.apps\/example-deployment with revision #5\nPod Template:\n  Labels:\tapp=example-deployment\n\tpod-template-hash=7648d6474\n  Annotations:\tkubernetes.io\/change-cause: Reverted nginx image to version 1.18\n  Containers:\n   nginx:\n    Image:\tnginx:1.18\n    Port:\t&lt;none&gt;\n    Host Port:\t&lt;none&gt;\n    Environment:\t&lt;none&gt;\n    Mounts:\t&lt;none&gt;\n  Volumes:\t&lt;none&gt;\n  Node-Selectors:\t&lt;none&gt;\n  Tolerations:\t&lt;none&gt;\n<\/code><\/pre>\n\n\n\n<p>In essence, deployment rollout and rollback features provides these benefits;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Continuous Delivery<\/strong>: Enables seamless updates of your applications without downtime.<\/li>\n\n\n\n<li><strong>Fault Tolerance<\/strong>: Provides a safety mechanism to quickly revert to a stable state in case of issues.<\/li>\n\n\n\n<li><strong>Version Control<\/strong>: Maintains a history of changes, making it easier to track and manage updates over time.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"deleting-kubernetes-deployment\">Deleting Kubernetes Deployment<\/h3>\n\n\n\n<p>If you have the necessary permissions to delete a deployment, then you can use <code>kubectl<\/code> to remove the deployment. It&#8217;s important to ensure you are deleting the correct deployment to avoid unintended consequences. Always verify your actions, especially in production environments.<\/p>\n\n\n\n<p>As usual, list all deployments to find the one you want to delete.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get deployments &#91;-n NAMESPACE]<\/code><\/pre>\n\n\n\n<p>Once you have identified the deployment you want to delete, use the <strong>kubectl delete deployment<\/strong> command followed by the deployment name. For example, if your deployment is named <strong>my-deployment<\/strong>, you would delete it with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl delete deployment my-deployment<\/code><\/pre>\n\n\n\n<p>If the deployment you want to delete is in a different namespace, specify the namespace using <code>-n<\/code> or <code>--namespace<\/code> flag:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl delete deployment my-deployment -n my-namespace<\/code><\/pre>\n\n\n\n<p>By default, when you delete a deployment, Kubernetes also deletes the pods that are managed by that deployment. If you want to keep the pods running even after deleting the deployment, you can use the <code>--cascade=orphan<\/code> flag:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl delete deployment my-deployment --cascade=orphan<\/code><\/pre>\n\n\n\n<p>Confirm by listing the pods!<\/p>\n\n\n\n<p>To ensure that the deployment has been successfully deleted, you can list the deployments again. If the deployment no longer appears in the list, it has been successfully deleted.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h3>\n\n\n\n<p>Deployments are fundamental to managing containerized applications effectively in Kubernetes. They provide automated management of application lifecycle, including scaling, updates, and fault tolerance. By understanding both declarative and imperative approaches to creating, managing, scaling, you can leverage this flexibility for robust and scalable application deployments.<\/p>\n\n\n\n<p>Further Reading <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/controllers\/deployment\/\" target=\"_blank\" rel=\"noreferrer noopener\">Kubernetes Deployments<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial provides a comprehensive guide on understanding deployments in Kubernetes. Deployments in Kubernetes play a crucial role in managing containerized applications, ensuring scalability, availability,<\/p>\n","protected":false},"author":10,"featured_media":22975,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[1668,1076,121],"tags":[7544,7545,7546],"class_list":["post-22960","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kubernetes","category-containers","category-howtos","tag-kubernetes-create-deloyment","tag-kubernetes-deployment-rollout","tag-undo-kubernetes-deployment-update","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/22960"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=22960"}],"version-history":[{"count":18,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/22960\/revisions"}],"predecessor-version":[{"id":22982,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/22960\/revisions\/22982"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/22975"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=22960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=22960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=22960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}