Traefik + Docker Swarm

In this article we will show you how to install and configure Traefik in a Docker cluster (Swarm). You may ask what Traefik is. As defined by its creators is a “Router Cloud with reverse proxy and load balancer”. The good thing that Traefik has is that you can integrate with Docker so you can deploy replicas of your services and they are automatically configured as backends in Traefik, so you can scale your services comfortably without having to set up Traefik again.

To install Traefik, the first thing we’ll do is install our cluster with Docker Swarm, as we explained in this article “Docker Swarm in Ubuntu”, we’ll only follow that article until the section “Configuration for Workers” included. From there what we will do is run a Stack, which consists of a file with a set of instructions that displays services and containers in Docker.

Deploy Traefik

Now comes the magic moment, as they like to say from Traefik, we will create a file with the following content:

 

version: "3.3"
services:
  traefik:
    image: traefik
    command: --web \
      --docker \
      --docker.swarmmode \
      --docker.domain=traefik.kb \
      --docker.watch \
      --logLevel=DEBUG
    networks:
      - traefik-net
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /dev/null:/traefik.toml
    deploy:
      placement:
        constraints: [node.role==manager]

  portainer:
    image: portainer/portainer
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "portainerdata:/data"
    networks:
      - traefik-net
    deploy:
      placement:
        constraints: [node.role == manager]
      labels:
        - "traefik.enable=true"
        - "traefik.port=9000"
        - "traefik.docker.network=mystack_traefik-net"
        - "traefik.frontend.rule=Host:portainer.traefik.kb"

  ms1:
    image: melvindave/spring-boot-example
    networks:
      - traefik-net
    depends_on:
      - mongodb
    deploy:
      replicas: 3
      labels:
        - "traefik.enable=true"
        - "traefik.port=8080"
        - "traefik.docker.network=mystack_traefik-net"
        - "traefik.frontend.rule=Host:ms1.traefik.kb"

  mongodb:
    image: mongo
    volumes:
      - "mongodata:/data/db"
    networks:
      - traefik-net
    deploy:
      replicas: 1
    ports:
      - "27017:27017"  

networks:
  traefik-net:

volumes:
   portainerdata:
   mongodata:

We will create 3 DNS records pointing to the IP of our “manager-01” to point to the deployed services: ms1.traefik.kb portainer.traefik.kb dashboard.traefik.kb.

Now we will deploy our Stack executing:

docker stack deploy -c traefik-stack.yml mystack

It has to return us: Creating service mystack_ms1
Creating service mystack_mongodb
Creating service mystack_traefik
Creating service mystack_portainer

We have all already working!

First steps with Traefik

Now that we have deployed Traefik we will enter our browser at http: //dashboard.traefik.kb: 8080 / we will see the Frontend and Backends deployed for each service.

As we can see in the screenshot our front ms1.traefik.kb has 3 backends, this means that when someone visits the web ms1.traefik.kb will load from one of them. Traefik already balances the load and distributes the visits between the backends, if one backend fails, it will send the visits to the other 2. From the “Health” tab we can check that everything works.

Manage replicas and services

As you can see we have also deployed a Portainer service, in this article there are more explanations about what Portainer allows. For this article, we will enter to http://portainer.traefik.kb to adminsitrar backend -replicas-. Suppose we have foreseen that our website “ms1.traefik.kb” has many visits for some reason, we can deploy more replicas from Portainer. When we return to normal visits we can reduce the number of replicas again. We leave you some captures of the process.

As you have seen, it is very easy to deploy services and backends, the advantage of using Traefik over other high-availability services such as HAProxy could be that the setup is done on its own.

You can deploy or delete backends in seconds. With other services, every time you add or delete backends you have to modify the setup.

In this article we have made a simple example with a static page, you can make similar setups with WordPress, PHP, … You can also setup the type of balance, weight of each backend, number of connections, … you can find more information on the Traefik website.

Have you tried it? Leave us your comments! 🙂

The stack of this article has been obtained and modified from the one from Melvinvivas.

To write a comment on this article, fill out the form below. Fields marked with an asterisk (*) are required.