# Workshop: Traefik Reverse Proxy Server

WARNING

  • Please copy previous workshop (Nginx Reverse Proxy Server) and rename for this Workshop

# Discussion

Basically, NGINX is a good example about how proxy works. However, NGINX could not support Dynamic Configuration (NGINX Plus can do but they are not free). Moreover, Traefik can do, it's free!, and easy to configure it. Moreover, Traefik can do auto TLS (opens new window) which is great and looks like a magic!

Dynamic Configuration: what if we want to add more container(s)/service(s) and want to hide them inside the proxy. Do we need to create new config file or reload config file? Let say YES! Therefore, we needs to restart a proxy. BOOM! Whole system is gone for a few second.

Dynamic Configuration means they can change configuration without restart itself.

# Traefik Configuration

There are 2 types of Traefik Configuration: Dynamic Configuration (opens new window) and Static Configuration (opens new window).

To sum up, Static Configuration we can specify how many ENTRYPOINTS (port) do you want to have, LOG level, TLS certificates, etc. In this example we will configure it through Environment variable.

For Dynamic Configuration we can add labels (opens new window) in each container and Traefik can notice by themselve.

# project structure

+-- backend
|    +-- ...
+-- frontend
|    +-- ...
+-- docker-compose.yml
1
2
3
4
5

./docker-compose.yml














 
 
 
 
 
 
 
 
 
 
 
 









 
 
 
 









 
 
 
 













version: "3"
services: 

    traefik:
        image: traefik:v2.3
    
        ports:
            - "80:80"
            - "8080:8080"
            - "443:443"
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock

        environment:
            - TRAEFIK_PROVIDERS_DOCKER=true


            - TRAEFIK_ENTRYPOINTS_web=true
            - TRAEFIK_ENTRYPOINTS_web_ADDRESS=:80

            - TRAEFIK_ENTRYPOINTS_websecure=true
            - TRAEFIK_ENTRYPOINTS_websecure_ADDRESS=:443


            - TRAEFIK_API_INSECURE=true

       
    frontend:
        build:
            context: ./frontend
        
        volumes:
            - ./frontend/src:/usr/share/nginx/html

        labels:
            - "traefik.http.routers.front.rule=PathPrefix(`/`)"
            - "traefik.http.routers.front.entrypoints=web"
            - "traefik.http.services.front.loadbalancer.server.port=80"

    backend:
        build:
            context: ./backend
            dockerfile: Dockerfile.dev
  
        volumes:
            - ./backend/src:/home/src

        labels:
            - "traefik.http.routers.api.rule=PathPrefix(`/api`)" 
            - "traefik.http.routers.api.entrypoints=web"
            - "traefik.http.services.api.loadbalancer.server.port=8000"        
     
 
    mongo:
        image: mongo:3.6.22-xenial

        environment:
          MONGO_INITDB_ROOT_USERNAME: root
          MONGO_INITDB_ROOT_PASSWORD: 1234

        volumes: 
            - mongo-sad-lab-traefik-proxy:/data/db
volumes: 
  mongo-sad-lab-traefik-proxy:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

# Static Configuration

  • TRAEFIK_PROVIDERS_DOCKER=true
    • providers is Docker.
  • TRAEFIK_ENTRYPOINTS_{NAME}=true
    • creates an entrypoint which name is {NAME}.
  • TRAEFIK_ENTRYPOINTS_{NAME}_ADDRESS=:{PORT NUMBER}
    • specifies port of {NAME} entrypoint.
  • TRAEFIK_API_INSECURE=true

# Dynamic Configuration

  • "traefik.http.routers.{NAME}.rule={RULE (opens new window)}"
  • "traefik.http.routers.{NAME}.entrypoints={ENTRYPOINTS NAME}"
  • "traefik.http.services.{NAME}.loadbalancer.server.port={PORT}"
    • Exposed container port number

# Up

docker-compose up --build -d
1

# Down and finish this workshop

docker-compose down
1