Traefik SSL Certs for your Homelab

Using Traefik to generate Lets Encrypt SSL Certs for your self hosted applications

4/21/20244 min read

What is Traefik v2?

Traefik is a modern and lightweight reverse proxy and load balancer that makes deploying microservices very easy. It is designed to be as simple as possible to operate, but capable of handling large, highly-complex deployments.

It also comes with a powerful set of middleware that enhance its capabilities to include load balancing, API gateway, orchestrator ingress, as well as east-west service communication and more. It is written in Go and is packaged as a single binary file and available as a tiny official docker image.

Traditional reverse-proxies require that you configure each route that will connect paths and subdomains to each microservice. In an environment where you add, remove, kill, upgrade, or scale your services many times a day, the task of keeping the routes up to date becomes tedious. 😟

Traefik listens to your service registry/orchestrator API and instantly generates the routes so your microservices are connected to the outside world β€” without further intervention from your part.

Traefik v2 architecture explanation

Some of Traefik it’s features further explained:

  • Dynamic Routing: Once properly set-up, Traefik will dynamically add new services and containers as they come up to provide traffic routing to them. Let’s say you have Traefik running and you want to add a new app, you just build your container and register a new endpoint and Traefik will automatically detect it and start routing traffic to it. This makes running a docker compose Traefik stack very powerful.

  • Load balancer: If you have multiple instances of a container, then Traefik can provide load balancing between those instances.

  • Let’s Encrypt: When properly configured, Traefik can not only route traffic to a newly discovered service, but also set up free SSL certs from Let’s Encrypt. Afterwards it can then redirect all the http traffic to https through middleware for enhanced security of your application.

  • Web UI: It comes packed with a very useful management dashboard that helps you visualize all the traffic endpoints, services, middleware and docker containers while showing potential warnings and errors as well.

Traefik v2 built-in dashboard

Setting up the instance

On your ubuntu or linux distro create the following folders or their equivalent

β”œβ”€ /treafik
│ └─ docker-compose.yml
└─ /traefik/config
γ€€γ€€β”œβ”€ config.yml
γ€€γ€€β”œβ”€ acme.json
  └─ traefik.yml

The acme.json will need the 600 permission applied to it

sudo chmod 600 ./traefik/config/acme.json

You will then need to populate the traefik.yml file with the following using nano for example

sudo nano traefik.yml

contents of the traefik yml will be as follows

main/Traefik/traefik.yml at main Β· ExpressITTech/main (github.com)

replacing your email with the relevant email address and if you have issues with your certs pulling through uncomment the line around disable propagation

next you will need to populate your docker compose however we will install some tools first to put your login into a base64 format

sudo apt-get update sudo apt install apache2-utils

to then get your username and password setup we will need to create that login in base 64 to do that swap out your username and password for one you want to use

echo $(htpasswd -nb "<USER>" "<PASSWORD>") | sed -e s/\\$/\\$\\$/g

this will generate your userid and password string we will need for your docker compose file.

Finally on cloud flare we will go and create an API
It will need to have edit rights to the Zone DNS and the Account that you are using .

now edit the docker compose yaml

main/Traefik/docker-compose.yaml at main Β· ExpressITTech/main (github.com)

replacing all elements in the <> dont include the <> when completed for example

<traefik-dashboard.yourdomain.co.uk> would become - <traefik-dashboard.expressittechtips.co.uk> for me

Create the Proxy network

To create the proxy network run the following

sudo docker network create proxy

Setting up a local dns entry

You can either do this using something like pi-hole and pointing your new FQDN to your IP of your traefik host or you can edit your host file on your local device

Editing the Hosts File on Windows 10 or Windows 11

The Windows hosts file, like other files that can be found in the Windows folder, is protected. You need administrative privileges to move, edit, or delete the file.

You can use any notepad style editor for this job

Hit the Start button, type "notepad" into the search, and then right-click it and hit "Run as Administrator."

Click on "File" in the top left corner, hit "Open," and then navigate to the Windows hosts file's location:

C:\Windows\System32\drivers\etc

Make sure to change the select to "All Files (.)" rather than "Text Documents (*.txt)." The hosts file doesn't have a file extension, so it won't show up if you're only displaying text documents. After you change that setting, locate the "hosts" fileand click open.

You can then edit the hosts file however you'd like.

Editing Host File in Ubuntu

Using something like nano

sudo nano /etc/hosts

Editing host file in MacOS

In macOS, accessing the hosts file is very similar to Ubuntu. Begin in terminal and use your favorite editor β€” it could even be a GUI-based text editor if you wish, but it is easier to use a command-line editor from the Terminal.

The hosts file on macOS is located in /etc/ just like on Linux.

The file will look a lot like the hosts file found in Windows and Linux.

Spinning up Traefik

You will now be ready to spin up your traefik instance

sudo docker compose up -d

You should now be able to access your traefik dashboard on the following

https://traefik-dashboard.<yourdomainname.co.uk>

remembering to have a CNAME at your domain registrar for this to take effect