What is Traefik
Traefik is a reverse proxy written in Go language. The feature is that the settings can be changed dynamically, and the application server can be registered under the reverse proxy even if the routing registration that was done with the conventional reverse proxy is not done in the configuration file in advance.
Environment I tried
- Ubuntu 18.04
- Traefik 2.1
Installation
There are two ways, one is to use a single binary and the other is to use a container.
When using a single binary
Download the binary from GitHub and place it somewhere in your path.
When using a container
traefik
You can use it by pulling the image.
root@4test:~# docker pull traefik
How to use
After starting Traefik using a container, I would like to try the process of automatically registering the server started in the container with Traefik as a backend server.
Launch Traefik
root@4test:~# docker run --name traefik -d -p 80:80 -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock traefik --api --docker
The following is a description of each option passed to traefik.
--api
--Enable dashboard to check status (accessible on port 8080)--docker
--Allow Docker container to be registered in the backend
Routing to the backend server
Next, register the server running in the container as Traefik’s backend server. Let’s containous/whoami
use an image here .
root@4test:~# docker run --name whoami -d -l "traefik.frontend.rule=PathPrefixStrip:/whoami" containous/whoami
If you give a traefik.
label that starts when you start the container, Traefik will recognize that the container with that label is being monitored. You can also use labels to configure routing, add HTTP headers, and more.
traefik.frontend.rule
Is a label that allows you to define rules for forwarding requests received by Traefik to the backend server.
PathPrefixStrip:/whoami
This is a definition of routing rules, which means that anything that /whoami
starts with a request /
changes the path to and forwards it to the backend server. The interesting thing about Traefik is that such routing settings were traditionally done on the reverse proxy side, but Traefik can be set on the backend server side. Therefore, when you want to register a new backend server, you do not need to bother to change the reverse proxy settings and restart.
Try making a request to see if the backend server is properly registered with Traefik.
$ curl http://127.0.0.1/whoami
Hostname: 79affa961863
IP: 127.0.0.1
IP: 172.17.0.3
GET / HTTP/1.1
Host: localhost
User-Agent: curl/7.29.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.17.0.1
X-Forwarded-Host: localhost
X-Forwarded-Port: 80
X-Forwarded-Prefix: /whoami
X-Forwarded-Proto: http
X-Forwarded-Server: 23d2da1e2c95
X-Real-Ip: 172.17.0.1
The routing settings can be set not only by the label of the container, but also by static settings using the TOML file and setting changes using the REST API.