How to scale your web platform with Traefik and Docker Swarm

Gleb Kletskov

--

Traefik working schema

A few months ago I faced up with a quite difficult problem. I have a website and a domain, but I want to scale up my platform with subdomains and other pet projects. For example: my website is kletskovg.tech and I want to add some app to this domain. So it should be like someapp.kletskovg.tech. How can I do it? I choose a few technologies which can help me with it. It is Traefik, Docker and Docker Swarm. Also you will need at least 2 virtual machines on your cloud provider (I prefer Digital Ocean).

What is Traefik?

Traefik is a leading modern reverse proxy and load balancer that makes deploying microservices easy. Traefik integrates with your existing infrastructure components and configures itself automatically and dynamically.

Traefik is designed to be as simple as possible to operate, but capable of handling large, highly-complex deployments across a wide range of environments and protocols in public, private, and hybrid clouds. It also comes with a powerful set of middlewares that enhance its capabilities to include load balancing, API gateway, orchestrator ingress, as well as east-west service communication and more.

Step 1: Create a Swarm 🐳

First of all, you will need to have at least 2 VM’s on your cloud provider because one of machines will be a manager and the second one will be a worker.

docker swarm init --advertise-addr <IP-OF-YOUR-MANAGER-MACHINE>

This command creates a swarm on the manager machine.

The output of this command will be like the following:

docker swarm init command

Pay attention to token. You have to copy it to connect from worker node to manager to create a swarm.

After this you can go to your worker node and run this command:

docker swarm join --token <TOKEN-FROM-INIT-COMMAND> <IP-OF-MASTER>:2377

You can run docker node ls on the master node and see output like this

docker node ls command

Step 2: Create external Docker network 🕸

Services in your swarm have to communicate with each other even if they are located on the different nodes. Docker networks solving this problem. You can read more about docker networking here.

Firstly first — create network with this command:

docker network create --scope swarm --driver overlay <NAME-OF-NETWORK>

With this command you will create network which will can work with different nodes ( — scope swarm) and will be distributed ( — driver overlay).

Congratulations! You have prepared your swarm environment! And you we are going to deploy Traefik and our services to swarm nodes!

Step 3: Docker Compose config and Traefik 🐿

Note: In this article I`am using Traefik v2.0 and Docker-compose v3.3

We can config our swarm with docker-compose.yml file. In this file we will declarate our services and other stuff.

First version of our compose file can contain some code like that:

Look at the traefik service config: We expose 80 ports and in command section pass some CLI arguments.

Also, you have to add all services to network which you have created before (In my example this network called web).

Step 4: Docker Stack deploy ⚙️

At this moment we already can check that our setup is ready and we can deploy our first service.

To update/deploy our swarm we can run following command:

docker stack deploy --compose-file docker-compose.yml <NAME-OF-STACK>

This command will parse our compose file and create/update all services.

After deploy our swarm we can go to yourdomain.com:80 and it should render traefik dashboard:

Traefik Dashboard

Step 5: Deploy your services and creating DNS records 📦

If you want to place all your services to different subdomains like todoapp.domain.com and api.domain.com, you have to create A level DNS records in your cloud provider or domain provider like that (Digital Ocean example):

To add your services to your swarm and connect them to Traefik proxy, you should add this code for each service to compose file

To update your swarm just run:

docker stack deploy --compose-file docker-compose.yml <NAME-OF-STACK>

You can check status of services with this command:

docker service ls

It will show you status of all services in the swarm and show how many containers of each service are booted.

Congrats! Your swarm is up and running. You can easily scale your services and deploy new ones! Good luck at building your perfect infrastructure!

Written by Kletskov Gleb, frontend software engineer in Mail.ru Group.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Gleb Kletskov
Gleb Kletskov

Written by Gleb Kletskov

Front-end 🕸 /DevOps 📦 /Raves ⚡️

No responses yet

Write a response