One thing I have always wanted to accomplish is full Continuous Deployment, with zero downtime, using Blue-Green Deployments, but I didn’t want all the extra overhead of setting up Swarm or Kubernetes.
Here’s how I recently accomplished this goal.
First, create a traefik.yml file for static configuration:
entryPoints: http: address: ":80" https: address: ":443" providers: file: directory: /etc/traefik/dynamic watch: true api: dashboard: true insecure: true
Then create a directory,
dynamic for the dynamic configurations. There is nothing in it yet.
Next create a docker-compose.yml file to spin up Traefik and hook up some volumes:
services: traefik: image: traefik:v2.4 ports: - 80:80 - 443:443 - 8080:8080 volumes: - ./traefik.yml:/etc/traefik/traefik.yml - ./dynamic:/etc/traefik/dynamic:ro
Start it up,
docker-compose up -d, and vist http://localhost:8080.
If everything worked, you should see the Traefik dashboard!
Now we need our
green services. Create two simple html files,
Then, create two simple Dockerfiles,
FROM nginx COPY blue.html /usr/share/nginx/html/index.html
FROM nginx COPY green.html /usr/share/nginx/html/index.html
Build and label the images:
docker build -t blue -f Dockerfile.blue . docker build -t green -f Dockerfile.green .
Let’s test them out:
docker run -p 8081:80 --rm blue docker run -p 8082:80 --rm green
blue: image: blue green: image: green
Then start them,
docker-compose up -d. If you run
docker ps or
you should now see 3 containers running.
We need to tell Traefik about these services, so in the
dynamic directory, we are going to add 4 files,
http: services: blue: loadBalancer: servers: - url: 'http://blue/'
http: services: green: loadBalancer: servers: - url: 'http://green/'
http: routers: blue-docker-localhost: rule: Host(`blue.docker.localhost`) service: blue@file
http: routers: green-docker-localhost: rule: Host(`green.docker.localhost`) service: blue@file
Almost there! Just one final step. Add the following file
http: routers: docker-localhost: rule: Host(`docker.localhost`) service: blue@file
See that last line? That’s where you decide which service,
green, maps to
When you want to change it, just change this one value. Traefik watches for changes in this directory, and updates
In a follow-up post, I will build a simple app that shows you which service is selected, and lets you change it.