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.
Now comes the magic moment, as they like to say from Traefik, we will create a file traefik-stack.yml with the following content:
command: --web \
constraints: [node.role == manager]
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