- Why docker swarm and not kubernetes ?
- Let’s dive into it !
In the light of recent events that occured at OVH, I had to fully remake my VPS architecture, and decided to drop down my old nginx configuration so I could use traefik which features really are awesome !
I already used traefik in his 1.17 version with docker swarm, so it was time to discover what they prepared for version 2.4 !
Well I only have a single server, and for single node servers I do think that docker swarm is more suitable. Kubernetes is one hell of a complicated thing (even though it’s quite awesome), and the simplicity of docker swarm is really good for personal use even though it may be limited for professional use.
Furthermore, most of swarm problems are network problems, which won’t be real problems for a small one node personal infrastructure.
Ok so first we’ll start our swarm and create the overlay network for traefik so it will be able to talk to other docker services
We will then take the docker-compose file from traefik doc and apply it as minimal configuration (https://doc.traefik.io/traefik/user-guides/docker-compose/basic-example/)
Then we can just deploy it in our swarm and check on http://localhost:8080 if everything went fine during the installation. This dashboard is enabled because of the two first command flags, as explained in the doc. I only added the network part and the letsencrypt volume for further use.
OK this looks fine already, let’s add some name resolution on the dashboard, it will be easier than going to port 8080 and it will help us learn how labels work.
Ok those labels explain themselves pretty much:
- Host(`traefik.example.com`) will be the Host HTTP header traefik will match to know if it needs to redirect incoming traffic to this service. There’s plenty of other rules possible, such as ones using regexp, do not hesitate to check traefik documentation.
- The api@internal part is custom for dashboard, but it is important to notice that it’s actualling refering to the dashboard service on port 8080
- The entrypoint is the name of the port defined on command tags. Here web=80 (http)
Now when we go to traefik.example.com, we can see our beautiful dashboard, and so we don’t have to expose port 8080 anymore !
But let’s not forget about security ! This dashboard is quite sensitive, so let’s add a security layer above it.
We have middlewares in traefik v2 that will do the job !
The first line represent all the middlewares we will be using for this service, separated by a comma, and the second line is the definition of the middleware.
This is a simple basicAuth here (don’t take this hash for your personal website !), but there are a lot of other predefined middleware that you could use.
Notice that we doubled each of our dollar signs in the basic auth definition. This is important for traefik ! It acts as a backslash character.
With all that we are ready for our first service !
Let’s say you would like, i don’t know, to run your blog on blog.example.com hostname.
I guess you already understood everything ! It’s super easy since we already have done quite the same with the traefik dashboard. Don’t forget to add the network proxy, the same as traefik, to your swarm service, so traefik can find it.
Wow, looks like we’re going fast ! But now comes the sounds-like-tricky part, as it often is a pain in the ass to configure https.
But not really with traefik, as everything is pretty much already included !
You can see a bunch of new commands and labels, but they have quite verbose name !
New commands basically define a new entrypoint called web-secure assigned to port 443 (common port for https://) here, redirect all web entrypoint traffic to it, and now we have our dashboard entrypoint to this new endpoint with this
I won’t show the example for the blog service, but it’s all the same.
OK so now we have https, which is great, but still our browser find it insecure to browse our website, not cool.
The problem is that traefik is serving its default certificate, which is of course not recognized by a CA.
But i don’t want to pay for certificates, so let’s configure let’s encrypt to get free certificates that will auto-renew. Can we do that in traefik ? Of course we can !
I will do it with ovh because my dns zone is at their place, but the configuration must be similar for other DNS providers.
You mean I only have a bunch of environment variables and to define my certificate resolver and the work is done ?
Well, yes, specifying ovh as a provider will have traefik do the job alone ! It will connect to ovh with this api key, and will create the DNS records so it can check the website does belong to you as all our bases should have.
Notice the new label, it specifies with certificate resolver you want for your service. Thereforce you could have multiple dns providers, and still make it work !
Certificates will be stored in the
acme.json file in letsencrypt volume.
I hope this post will make you mane to use traefik as it is really easy to understand, easy to configure and has a lot of super cool features already built into it !