Docker swarm with traefik basic config


Let’s setup a basic 2 hosts load-balanced whoami with docker swarm and Traefik.

The setup

  • 2 instances on Google Cloud Patform, or watever you’ll have to test with (Easy to prototype for almost free).
  • Traefik for reverse proxy and load balancer.
  • Whoami as a POC (proof of concept).
  • Sub-domain with DNS that will aim the master swarm node. In my case, it will be test.sirap.ovh.

Google Cloud Platform

In compute engine tab, I created 2 instances (micro), with Ubuntu 18-04 LTS minimal image loaded. I need to :

  • Install docker
  • Initialize swarm
  • create backend overlay network
wget -O - https://get.docker.com/ | sudo bash
sudo usermod -aG docker <username>
exit
docker swarm init
docker network create --driver=overlay backend

Make sure you has joined the node with something like :

docker swarm join --token SWMTKN-1-4uci0t...ve4q0-bnp...q8e 10.142.xx.xx:2377

docker-compose.yml

I’ve done simple, without ACME, no https, just plain http.

Here the docker-compose.yml, I had problem without the - "--docker.network=backend", as it couldn’t get the network right, I had lot’s of 504 gateway timeout.

version: "3" networks: backend: driver: overlay external: true services: traefik: image: traefik ports: - "80:80" # The HTTP port - "443:443" - "8080:8080" volumes: - /var/run/docker.sock:/var/run/docker.sock command: - "--api" - "--entrypoints=Name:http Address::80" - "--defaultentrypoints=http" - "--docker" - "--docker.swarmMode" - "--docker.domain=mydomain.ca" - "--docker.watch" - "--docker.network=backend" networks: - backend deploy: replicas: 1 placement: constraints: - node.role == manager whoami: image: emilevauge/whoami deploy: replicas: 2 labels: - "traefik.port=80" - "traefik.frontend.rule=Host:test.sirap.ovh" networks: - backend ports: - 8081:80

Now, let’s deploy the stack.

docker stack deploy -c docker-compose.yml test

Let’s check the result, seem’s good, it load balance from one host to another.

:~$ http "test.sirap.ovh" | grep Hostname
Hostname: d5b3ba65bb5c
:~$ http "test.sirap.ovh" | grep Hostname
Hostname: c9ca97496d84
:~$ http "test.sirap.ovh" | grep Hostname
Hostname: d5b3ba65bb5c