Rolling Updates in Kubernetes

Saurabh AdhauSaurabh Adhau
3 min read

Introduction

In Kubernetes, a rolling update is a deployment strategy used to update applications running as pods in a controlled and gradual manner. This approach ensures minimal disruption to the application's availability by gradually replacing old instances with new ones.

How Rolling Updates Work

  1. Deployment Definition

    Rolling updates are typically managed through Kubernetes Deployments, which provide declarative updates to applications. Here's an example of a simple Deployment definition:

     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: my-app
     spec:
       replicas: 3
       selector:
         matchLabels:
           app: my-app
       template:
         metadata:
           labels:
             app: my-app
         spec:
           containers:
           - name: my-app-container
             image: my-app:v1
             ports:
             - containerPort: 80
    

    This Deployment specifies three replicas (replicas: 3) of a container (my-app-container) running the my-app:v1 image.

  2. Updating the Deployment

    To perform a rolling update, you update the Deployment's spec.template.spec.containers[0].image field to point to a new version of your application's image (my-app:v2, for example). Kubernetes automatically manages the update process:

    • Pod Creation: Kubernetes creates new pods with the updated image (my-app:v2).

    • Pod Termination: Kubernetes gradually terminates old pods running the previous version (my-app:v1).

    • Replica Control: It ensures that the specified number of replicas (replicas: 3) is maintained throughout the update process.

  3. Update Strategy

    Kubernetes manages the rolling update process using a configurable strategy defined in the Deployment's spec.strategy field. By default, it uses a rolling update strategy (RollingUpdate) which ensures that a specified number of pods (or a percentage) are updated at a time.

     spec:
       strategy:
         type: RollingUpdate
         rollingUpdate:
           maxUnavailable: 1
           maxSurge: 1
    
    • maxUnavailable: Specifies the maximum number or percentage of pods that can be unavailable during the update process. Setting maxUnavailable: 1 ensures that only one pod is taken down at a time.

    • maxSurge: Specifies the maximum number or percentage of new pods that can be created above the desired number of pods. Setting maxSurge: 1 allows one additional pod beyond the desired count to be created temporarily during the update.

Benefits of Rolling Updates

  • Zero Downtime: Ensures continuous availability of the application by gradually updating pods.

  • Controlled Process: Kubernetes manages the update process, ensuring consistency and reliability.

  • Rollback Capability: Provides the ability to rollback to a previous version quickly if issues arise during the update.

Considerations

  • Health Checks: Define readiness and liveness probes in your Deployment to ensure Kubernetes only considers pods ready for traffic.

  • Resource Requirements: Ensure nodes have sufficient resources to handle both old and new pods during the update.

Conclusion

Rolling updates in Kubernetes are a fundamental feature for maintaining application availability while updating containerized applications. By leveraging Kubernetes Deployments and update strategies, administrators can ensure smooth and controlled updates, minimizing disruption and providing a reliable mechanism for managing application lifecycle in dynamic environments.

0
Subscribe to my newsletter

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

Written by

Saurabh Adhau
Saurabh Adhau

As a DevOps Engineer, I thrive in the cloud and command a vast arsenal of tools and technologies: โ˜๏ธ AWS and Azure Cloud: Where the sky is the limit, I ensure applications soar. ๐Ÿ”จ DevOps Toolbelt: Git, GitHub, GitLab โ€“ I master them all for smooth development workflows. ๐Ÿงฑ Infrastructure as Code: Terraform and Ansible sculpt infrastructure like a masterpiece. ๐Ÿณ Containerization: With Docker, I package applications for effortless deployment. ๐Ÿš€ Orchestration: Kubernetes conducts my application symphonies. ๐ŸŒ Web Servers: Nginx and Apache, my trusted gatekeepers of the web.