Rolling Deployment Explained: For Zero Downtimes

  • 2024/7/17
  • Rolling Deployment Explained: For Zero Downtimes はコメントを受け付けていません

Introduction

Rolling deployment is a popular deployment strategy that updates instances of an application incrementally, ensuring minimal disruption and downtime. This approach replaces old instances with new ones one by one, allowing the application to remain available throughout the update process. Rolling deployment is particularly useful for large-scale applications that require continuous service availability and minimal downtime during updates.

Key Benefits of Rolling Deployment

  1. Zero Downtime: By updating instances incrementally, rolling deployment ensures that the application remains available throughout the update process.
  2. Reduced Risk: Gradual updates reduce the risk of widespread failures. Issues can be detected and resolved in smaller subsets before impacting the entire system.
  3. Seamless User Experience: Users experience a consistent and uninterrupted service as updates are applied gradually across instances.
  4. Resource Efficiency: Resources are used efficiently, as old instances are replaced with new ones without the need for additional infrastructure.

Implementing Rolling Deployment

Let’s demonstrate a practical implementation of rolling deployment using a basic Flask application that listens on a specified port and responds with a simple message. We’ll include two versions of the application to demonstrate the rolling deployment process and automate the incremental update of instances and include rollback mechanisms in case of health check failures or errors.

Step 1: Create Simple applications 

Version 1 (Old Version)

Create a file named app_v1.py:

from flask import Flask

app = Flask(__name__)

@app.route(‘/’)
def hello():
    return “Hello, World! This is the old version.”

if __name__ == ‘__main__’:
    app.run(host=’0.0.0.0′, port=80)

 

Version 2 (New Version)

Create a file named app_v2.py:

from flask import Flask

app = Flask(__name__)

@app.route(‘/’)
def hello():
    return “Hello, World! This is the new version.”

if __name__ == ‘__main__’:
    app.run(host=’0.0.0.0′, port=80)

 

Step2: Docker Configuration

Next, we’ll create Dockerfiles for both versions of the application.

  • Dockerfile for Version 1

Create a file named Dockerfile_v1:

FROM python:3.8-slim

WORKDIR /app

COPY app_v1.py .

RUN pip install Flask

CMD [“python”, “app_v1.py”]


  • Dockerfile for Version 2

Create a file named Dockerfile_v2:

FROM python:3.8-slim

WORKDIR /app

COPY app_v2.py .

RUN pip install Flask

CMD [“python”, “app_v2.py”]

 

Step3: Building Docker Images

Build the Docker images for both versions:

docker build -f Dockerfile_v1 -t myapp:old .
docker build -f Dockerfile_v2 -t myapp:new .

 

Step4: Deploying the Application

Deploy the old version of the application across multiple instances:

docker run -d –name app1 -p 8081:80 myapp:old
docker run -d –name app2 -p 8082:80 myapp:old
docker run -d –name app3 -p 8083:80 myapp:old

 

Step5: Automating the Rolling Deployment

Use the following script to automate the rolling deployment process, including health checks and rollback mechanisms:

#!/bin/bash

instances=(“app1” “app2” “app3”)
new_image=”myapp:new”
old_image=”myapp:old”

for instance in “${instances[@]}”; do
    echo “Updating $instance…”
    docker stop $instance
    docker rm $instance
    docker run -d –name $instance -p 8081:80 $new_image

    # Health check
    sleep 10
    if docker logs $instance | grep -q “[ERROR]”; then
        echo “Error detected in $instance, rolling back…”
        docker stop $instance
        docker rm $instance
        docker run -d –name $instance -p 8081:80 $old_image
        exit 1
    fi

    echo “$instance updated successfully.”
done

echo “All instances updated successfully.”

 

Step6: Testing the Deployment

You can test the deployment by accessing the application in your web browser:

  • Old version: http://localhost:8081
  • New version (after deployment): http://localhost:8082

Step7: Rollback (if necessary) If any issues are detected, the script will automatically rollback the updated instance to the old version.

This setup demonstrates how to use rolling deployment to update a simple Flask application incrementally, ensuring minimal disruption and zero downtime. 

Conclusion

Rolling deployment is a robust strategy for ensuring seamless application updates with zero downtime. By incrementally updating instances and continuously monitoring for issues, developers can maintain a stable and responsive application. The automated script provided ensures that instances are updated and rolled back if any issues are detected, further reducing the risk of deployment failures.

Whether you’re managing a large-scale distributed system or a simple web application, rolling deployment provides the reliability and flexibility needed for modern software development. By understanding and implementing rolling deployment, you can enhance your deployment processes, reduce risk, and ensure a smooth user experience.

関連記事

カテゴリー:

ブログ

情シス求人

  1. チームメンバーで作字やってみた#1

ページ上部へ戻る