Motivation
I previously used Nginx as a reverse proxy to access websites on my private server. At that time, Nginx was installed directly on the computer without containerization, which meant that when I wanted to migrate these services to other computers later, I had to reconfigure Nginx and reconfigure SSL certificates, etc.
So I decided to containerize the reverse proxy service as well, to facilitate future management and migration. I saw many examples of using Traefik in Docker online, so I decided to migrate from Nginx to Traefik.
Prerequisites
- A computer running Linux (as a private server)
- A private domain name (for detailed operations, please refer to the previous post “The Process of Building a Personal Website (1): Purchasing a Personal Domain Name and Configuring Dynamic Domain Name Resolution”)
- Docker and docker-compose installed, and a basic understanding of Docker concepts and usage. If you haven’t installed Docker and docker-compose, or if you don’t understand the basic concepts and usage of Docker and containerization, you can refer to “Containers (1): Introduction to Container-Related Knowledge - Containerization, Docker, Docker-Compose, Kubernetes / K8s, etc.” for a more detailed introduction.
- A basic understanding of Nginx concepts and usage. If you haven’t used or don’t understand Nginx, you can refer to “Accessing Personal Websites from the Public Network - Nginx Reverse Proxy Configuration” for a more detailed introduction.
Traefik Overview
Traefik is a modern reverse proxy and load balancer that can automatically discover and configure backend services. Traefik supports various backend services such as Docker, Kubernetes, Consul, etc., and can automatically obtain SSL certificates. Traefik’s configuration is very flexible and can be done through labels, files, or APIs.
Installing and Configuring Traefik
Overall Architecture
Traefik can be configured using labels, APIs, or files. Since I previously used file-based configuration with Nginx, where each service had its own configuration file, I found this approach very clear and manageable. Therefore, I will also use file-based configuration with Traefik.
The Traefik container itself is independent and managed using a docker-compose.yml
file like other Docker containers. We will create a new network in Docker called traefik-net
, and all containers, including Traefik, will use this network. Within this network, we will use Traefik for reverse proxying.
In this article, we will take the deployment of a Whoami service as an example to introduce the use of Traefik. Whoami is a simple HTTP server that returns the request’s IP address, request headers, and other information, making it ideal for testing reverse proxies.
Project Directory Structure
traefik
Project
Based on the above architecture design, we create a new traefik
directory under the directory where all Docker projects are stored, to hold the relevant files for the Traefik project. The directory structure is as follows:
|
|
where:
docker-compose.yml
: The Docker Compose configuration file for Traefik..env
: The environment variable file used to store environment variables for the Traefik project.traefik.yml
: The main configuration file for Traefik.acme.json
: The file used to store SSL certificates.dynamic
: The directory for storing dynamic configuration files, with each service having its own configuration file.
whoami
Project
Since we are taking a simple Whoami service as an example, we need to create a Whoami project in addition to the Traefik project. The Whoami project is very simple and also placed in the directory where all Docker projects are stored, containing only a docker-compose.yml
file.
|
|
Traefik Installation, Configuration, and Testing Process
1. Create traefik-net
Network in Docker
First, we create a public network in Docker called traefik-net
(you can use a different name, but it must be consistent in subsequent configurations):
|
|
2. Create the traefik
Project Directory
Create a traefik
directory under the directory where all Docker projects are stored, and create the above files and subdirectories in that directory.
docker-compose.yml
docker-compose.yml
如下:
|
|
where traefik-net
is the network we created earlier. The Traefik configuration file traefik.yml
, dynamic configuration directory dynamic/
, and the file for storing SSL certificates acme.json
are mounted into the container.
.env
In .env
, we store the CloudFlare API key and email address used for automatically obtaining SSL certificates. You need to replace the following content with your own Cloudflare API key and email address. In theory, providing just the key should be enough, but I haven’t tested it. If you are using a DNS provider other than Cloudflare (for example, Alibaba Cloud), you need to replace the relevant API key and email address with those of your DNS provider. For details, please refer to the Traefik documentation.
|
|
traefik.yml
traefik.yml
is the main configuration file for Traefik. Below is a basic configuration example:
|
|
Here we specify two entry points: web
(HTTP) and websecure
(HTTPS), and set the directory for dynamic configuration files to /etc/traefik/dynamic
. We also enable the Traefik API dashboard and configure the ACME certificate resolver for Cloudflare DNS Challenge. If you are using a different DNS provider, you can replace provider: cloudflare
with the name of your DNS provider and configure the corresponding API key and email address according to its documentation.
acme.json
acme.json
is the file used to store SSL certificates. You need to create this file first (an empty file is sufficient) and set its permissions to 600
to ensure that Traefik can write certificate information:
|
|
dynamic
Directory
In the dynamic
directory, we can create a configuration file for each service. Below is a very simple configuration file for the Whoami service, and this article will use the deployment of a Whoami service as an example to introduce the use of Traefik.
|
|
3. Create the whoami
Project Directory
Create a whoami
directory under the directory where all Docker projects are stored, and create a docker-compose.yml
file in that directory. The content is also very simple:
|
|
This docker-compose.yml
file defines a Whoami service that uses the official Whoami image provided by Traefik and connects it to the previously created traefik-net
network.
4. Start the Whoami and Traefik Services
If you have previously deployed an Nginx service on this computer, you need to stop the Nginx service first to avoid port conflicts with Traefik:
1
sudo systemctl stop nginx
To prevent Nginx from starting again after the computer restarts, you can disable the Nginx service:
1
sudo systemctl disable nginx
Enter the
whoami
directory and use the following command to start the Whoami service:1
docker-compose up -d
Enter the
traefik
directory and use the following command to start the Traefik service:1
docker-compose up -d
Check if the Traefik and Whoami services are running properly:
1
docker ps
You should see that the containers for the Traefik and Whoami services are running.
5. Test Traefik Reverse Proxy
First, make sure you have resolved your domain name (e.g., whoami.example.com
) to your server’s IP address with your DNS provider.
Then, you can test Traefik’s reverse proxy functionality by accessing https://whoami.example.com
. You need to replace whoami.example.com
with your own domain name and ensure that DNS resolution is correctly configured.
If everything is working properly, you should be able to access the Whoami service, and you should see a page containing request information, similar to the content below:
|
|
If you cannot access https://whoami.example.com
, it means there is a problem with the configuration above. You can uncomment the log.level: "DEBUG"
line in traefik.yml
, so Traefik will output more detailed log information to help you troubleshoot the issue.
If you encounter SSL certificate errors when accessing the site, it may be because Traefik has not yet obtained the SSL certificate. You can wait for a while, and Traefik will automatically obtain the SSL certificate and configure HTTPS.