Blue-Green Deployments with Containers and Traefik

Minimising downtime when deploying a new version of your application is not an easy feat. In this post we will explore the blue-green deployment strategy.

Minimizing downtime when deploying a new version of your application is not an easy feat. In this post we'll explore blue-green deployments that was described in the excellent book Continuous Delivery by Jez Humble and David Farley.

Blue-Green Deployments

Using a blue-green deployment strategy can help minimize downtime and reduce the risks associated with traditional deployments. The blue-green deployment strategy works by creating two identical environments: one blue environment and one green environment. You can then switch between them with minimal interruption to the end-user.

Containers

Containers have revolutionized the way that applications are developed and deployed. Since they are lightweight and portable, it provides an ideal foundation for blue-green deployments by encapsulating an application and its dependencies. That provides us with consistency and eliminates compatibility issues across different environments.

Container orchestration platforms such as Kubernetes and Docker Swarm make it easy to manage and scale your container applications. These platforms offer features such as automated deployment, scaling, and continuous updates, making them ideal for implementing blue-green deployments.

Traefik

Traefik is a modern reverse proxy and load balancer that plays a key role in simplifying the deployment process. It also seamlessly integrates with container orchestration platforms to dynamically route incoming traffic to blue or green environments.

Traefik acts as a gateway that intercepts incoming requests and delivers them based on predefined rules. During a blue-green deployment, Traefik can be configured to forward traffic to the blue environment while the green environment remains idle. Once the green environment is fully tested and ready, Traefik can redirect traffic to the green environment for a smooth transition without impacting end users.

Configuration

Let's take a look at the process of setting up a blue-green deployment using containers and Traefik.

  1. Start by containerizing your application using Docker or another containerization tool. This encapsulates the application along with its dependencies.
  2. Deploy containers using container orchestration platforms such as Kubernetes or Docker Swarm.
  3. Configure Traefik as a reverse proxy and load balancer. Define the blue and green environments as separate services, each with its own container.
Logical component configuration

Routing

Control traffic flow with Traefik's routing rules. First, direct all traffic to the blue environment. Once you've verified that your green environment is ready, update your routing rules to direct your traffic to the green environment.

Databases

In a blue-green deployment, data and databases need special attention. By using Entity Framework, we can simplify the database migrations by using a code-first migration strategy. Entity Framework supports up and down migrations which is useful when switching between the blue and green environments - for an upgrade or a rollback. Backwards compatible schemas and idempotent scripts will ensure smooth migrations when switching between the blue and green environment.

There are various strategies to keep the data synchronized between the blue and green environment such as streaming data using Kafka, peer-to-peer transactional replication, ETL, SQL Data Sync for Azure, etc. The synchronization method will depend on technology that's available in the environment and the availability requirements of the data.

Example

The following example is available in the Straypaper Git Repository.

In the example above Traefik receives all the incoming requests. It routes every incoming request to the correct container based on the host header or SNI. For example, if the host header matches blue.docker.localhost, the request will be sent to the blue container. If the request matches green.docker.localhost the request will be sent to the green container. When you browse to docker.localhost, the request will be sent to whichever service is specified in the main-router.yml file.

http:
  routers:
    main-router:
      rule: Host(`docker.localhost`)
      service: blue-service@docker

You can update the service in this file to green-service@docker; it is watched by Traefik for changes; to swap to the green environment in real time.

All incoming requests on TCP/1433 is redirected to the MS SQL Server container. In the above example, there is an external MS SQL Server, that is not a container in Docker Swarm, which is considered to be the active database whereas the database in Docker Swarm is considered the idle database.

An ETL component is shown which can be replaced by any other synchronization mechanism to synchronize the data between the active and idle database. An ETL mechanism is used in this example as the data needs to be extracted from the source database and transformed to match the destination schema before it can be loaded into the destination database.

Conclusion

In this post we explored blue-green deployments using containers and Traefik. We also briefly looked at synchronizing database schemas and data.