How to perform rolling update on Kubernetes Deployment?

KubernetesKubernetesBeginner
Practice Now

Introduction

Kubernetes is a powerful container orchestration platform that simplifies the deployment and management of applications. One of the key features of Kubernetes is the ability to perform rolling updates on Deployments, allowing you to update your application with minimal downtime. In this tutorial, you'll learn how to configure and execute rolling updates on Kubernetes Deployments, ensuring a smooth and efficient update process for your applications.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedDeploymentGroup(["`Advanced Deployment`"]) kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") kubernetes/BasicCommandsGroup -.-> kubernetes/edit("`Edit`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/AdvancedDeploymentGroup -.-> kubernetes/rollout("`Rollout`") kubernetes/AdvancedDeploymentGroup -.-> kubernetes/scale("`Scale`") subgraph Lab Skills kubernetes/create -.-> lab-414653{{"`How to perform rolling update on Kubernetes Deployment?`"}} kubernetes/edit -.-> lab-414653{{"`How to perform rolling update on Kubernetes Deployment?`"}} kubernetes/apply -.-> lab-414653{{"`How to perform rolling update on Kubernetes Deployment?`"}} kubernetes/rollout -.-> lab-414653{{"`How to perform rolling update on Kubernetes Deployment?`"}} kubernetes/scale -.-> lab-414653{{"`How to perform rolling update on Kubernetes Deployment?`"}} end

Understanding Kubernetes Deployments

Kubernetes Deployments are a crucial component in the Kubernetes ecosystem, providing a declarative way to manage the lifecycle of your applications. A Deployment is responsible for creating and managing a set of replicated Pod instances, ensuring high availability and scalability of your application.

What is a Kubernetes Deployment?

A Kubernetes Deployment is a resource that defines the desired state of your application, including the number of replicas, the container image to be used, and other configuration details. The Deployment controller then ensures that the actual state of the application matches the desired state, creating and managing the necessary Pods.

Deployment Components

A Kubernetes Deployment consists of the following key components:

  • ReplicaSet: The ReplicaSet is responsible for maintaining the desired number of Pod replicas. It ensures that the correct number of Pods are running and automatically replaces any Pods that fail or are deleted.
  • Pod: Pods are the basic unit of deployment in Kubernetes, encapsulating one or more containers that make up your application.
  • Container Image: The container image defines the application code and its dependencies, which are deployed as Pods.

Deployment Use Cases

Kubernetes Deployments are commonly used in the following scenarios:

  • Scaling: Deployments allow you to easily scale your application up or down by adjusting the number of replicas.
  • Rolling Updates: Deployments support rolling updates, allowing you to update your application's container image without downtime.
  • Self-Healing: Deployments automatically manage the lifecycle of Pods, replacing any failed or deleted Pods to maintain the desired state.
graph TD A[Kubernetes Cluster] --> B[Deployment] B --> C[ReplicaSet] C --> D[Pod] D --> E[Container]

By understanding the key concepts and components of Kubernetes Deployments, you'll be better equipped to manage and deploy your applications in a Kubernetes environment.

Configuring Rolling Updates

One of the powerful features of Kubernetes Deployments is the ability to perform rolling updates, which allow you to update your application's container image without downtime. This is achieved through the use of the Deployment's update strategy.

Update Strategies

Kubernetes Deployments support two main update strategies:

  1. RollingUpdate: This is the default update strategy, where new Pods are gradually rolled out and old Pods are gradually terminated. This ensures that your application remains available during the update process.
  2. Recreate: This strategy first terminates all existing Pods and then creates new Pods with the updated container image. This approach results in downtime during the update process.

To configure the update strategy, you can set the strategy.type field in your Deployment manifest. For example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  ## other Deployment configuration

In the example above, the Deployment is configured to use the RollingUpdate strategy, with a maximum of 1 Pod being unavailable and a maximum of 1 additional Pod being created during the update process.

Controlling Rolling Updates

You can further customize the rolling update behavior using the following parameters:

  • maxUnavailable: The maximum number of Pods that can be unavailable during the update process.
  • maxSurge: The maximum number of Pods that can be created above the desired number of Pods.

These parameters allow you to fine-tune the update process to meet your application's availability and resource requirements.

By understanding how to configure rolling updates for your Kubernetes Deployments, you can ensure a seamless and reliable update process for your applications.

Hands-on: Updating a Deployment

In this section, we'll walk through the steps to update a Kubernetes Deployment with a new container image.

Prerequisites

  • A Kubernetes cluster with kubectl configured and accessible.
  • A Deployment already created and running in your cluster.

Updating a Deployment

  1. Identify the Deployment: First, let's identify the Deployment we want to update. You can list all Deployments in your cluster using the following command:

    kubectl get deployments

    This will display a list of all Deployments in your cluster, including the one you want to update.

  2. Update the Container Image: To update the container image used by the Deployment, you can use the kubectl set image command. For example, if your Deployment is named my-app and the container image is my-image:v1, you can update it to my-image:v2 with the following command:

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

    This command will trigger a rolling update, gradually replacing the old Pods with new Pods using the updated container image.

  3. Monitor the Update Process: You can monitor the progress of the rolling update using the following commands:

    ## Watch the Deployment status
    kubectl get deployment my-app -w
    
    ## View the events related to the Deployment
    kubectl describe deployment my-app

    These commands will allow you to see the new Pods being created, the old Pods being terminated, and the overall status of the rolling update.

  4. Verify the Update: Once the rolling update is complete, you can verify that the new container image is being used by the Pods in the Deployment:

    ## Get the Pods in the Deployment
    kubectl get pods -l app=my-app
    
    ## Inspect the container image of the Pods
    kubectl describe pod <pod-name> | grep Image

    This will confirm that the Pods are now running the updated container image.

By following these steps, you can easily perform a rolling update on a Kubernetes Deployment and ensure a smooth application update process.

Summary

In this Kubernetes tutorial, you've learned how to perform rolling updates on Deployments. By understanding the configuration options and the step-by-step process, you can now safely and efficiently update your Kubernetes-based applications without disrupting your users. Mastering rolling updates is a crucial skill for Kubernetes administrators and developers, as it enables you to deliver new features, fix bugs, and maintain your applications with minimal downtime.

Other Kubernetes Tutorials you may like