Using Traefik with Docker Swarm For Deploying Web Applications
This tutorial will show you how to get started with deploying web applications on a docker swarm cluster with Traefik. Traefik allows us to deploy multiple web applications that each "want" to be accessed on port 80/443, on the same host. It's latest support for docker swarm takes this further by managing web applications across multiple hosts, allowing us to scale out so that we can manage any number of applications on a single cluster, rather than managing individual hosts. It basically makes deploying and managing web application containers a doddle.
If you haven't already, create a swarm cluster with 1 manager node and at least 1 worker node. It will take you less than 5 minutes and sounds harder than it is.
Once we have our swarm, we need to create an overlay network for traefik to use.
docker network create --driver=overlay traefik-net
After running the command, you should get something like
0fv9x8scsntdvngawcocohue6 as output. You can ignore this.
Now we need to deploy Traefik as a service in our cluster. For Traefik to work with swarm mode, it needs to run on the manager node. To achieve this we need to use a constraint. Execute the command below to start the traefik service.
docker service create \ --name traefik \ --constraint=node.role==manager \ --publish 80:80 \ --publish 8080:8080 \ --mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \ --network traefik-net \ traefik:v1.1.0 \ --docker \ --docker.swarmmode \ --docker.domain=traefik \ --docker.watch \ --web
Now we can start deploying our web applications. For this example, I am deploying the
emilevauge/whoami image as a web application that should respond when the cluster is hit by a user navigating to
my-site.programster.org. For this to work in your environment, you would need to update your DNS server or hosts file so that
my-site.programster.org will point the user to any of the nodes in your swarm cluster.
docker service create \ --name whoami1 \ --label traefik.port=80 \ --network traefik-net \ --label traefik.frontend.rule=Host:my-site.programster.org\ emilevauge/whoami
You probably want to change
my-site.programster.org to your own domain names, but you can keep repeating this last step with all of your web applications until all of them are on the same cluster.
After having deployed traefik and a service or two, it would be a good idea to have a look at Traefik's web interface by using the IP or hostname of any of your swarm nodes, and using port 8080 as shown below:
You should see something like this:
Explaining the web UI is beyond the scope of this tutorial, but I recommend you have a look around and click on the link to the documentation in to top-right corner, for further information.
We have only scratched the service of using Traefik and docker swarm, but in future we will cover other features such as:
- Let's encrypt integration for automatic SSL certificates.
- Load balancing and redundancy.