Traefik is a modern HTTP reverse proxy and load balancer that makes deploying microservices easy. Traefik integrates with with multiples infrastructure components (Docker, Kubernetes, ...) and configures itself automatically and dynamically.
This project focuses on the steps needed to setup a local Traefik environment.
Note: This guide asumes that you already have Docker and Docker Compose installed on your system.
-
Add all the domains you need to your
/etc/hosts
file.127.0.0.1 localhost.traefik.com 127.0.0.1 localhost.site.com
-
Create an external Docker network, it will be used to connect traefik to other services.
docker network create docker_default
-
Clone this repository
git clone https://github.com/ealcantara22/traefik.git cd traefik
-
Copy the
.env.sample
file as.env
and fill it with the information you used in steps 1 and 2.cp .env.sample .env
-
Start traefik and verify that
http://localhost.traefik.com/dashboard/
works. The final/
is mandatory.docker-compose up -d
I like to run all my apps locally using HTTPS
for multiple reasons, and the easiest way for me to accomplish that is supporting all the apps and services domain that I need in a single certificate by using a Multi-Domain (SAN) Certificate
.
-
Edit the
openssl.conf
.1.1. Replace the
distinguished name (dn)
section with your informacion. You can read more about these valueshere
.[dn] C=Country ST=State or Province name L=Locality name CN=Common Name O=Organization name OU=Organizational Unit name emailAddress=Email address
1.2. Add all the domain names you need in the
alt_names
section.[alt_names] DNS.1=localhost.traefik.com DNS.2=localhost.site.com DNS.3=my-domain.com . . .
-
Generate the certificate by executing the
generate-ssl.sh
script located in thescripts
directory. You will notice that acert.crt
andcert.key
files were created.cd scripts && ./generate-ssl.sh
Now that you bought or generate a certificate, add it to traefik is really easy.
-
Place your certificate files (generally a
.crt
and a.key
files) inside thecerts
directory. -
Rename the
tlsOptions.toml.sample
file place in thedynamic
directory totlsOptions.toml
.mv tlsOptions.toml.sample tlsOptions.toml
-
Edit the
tlsOptions.toml
file content with your certificate file information using the configuration that best suits your needs. Read morehere
- Create this
docker-composer.yml
file and rundocker-compose up
.version: "3.3" networks: docker_default: external: true services: nginx: image: nginx:latest networks: - docker_default labels: - traefik.enable=true - traefik.docker.network=docker_default - traefik.http.routers.nginx.entryPoints=https - traefik.http.routers.nginx.rule=Host(`localhost.nginx.com`) - traefik.http.routers.nginx.tls=true
- Open your favorite web browser and go to
localhost.nginx.com
.
There's always room for improvements, please submit a issue or a pull request if you have the time.