Reverse Proxy and Load Balancer
3 Weeks - 15 Days - Complete Guide
Traefik adalah reverse proxy dan load balancer yang modern dan powerful
| Hari | Topik | Praktik |
|---|---|---|
| 1 | Pengenalan Traefik | Apa itu Traefik? |
| 2 | Instalasi | Install Traefik di Docker |
| 3 | Konfigurasi Dasar | Static dan Dynamic config |
| 4 | Docker Provider | Auto-discovery services |
| 5 | Basic Routing | Path dan Host routing |
docker network create traefik
docker run -d \
--name traefik \
--network traefik \
-p 80:80 -p 443:443 -p 8080:8080 \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
traefik:v3.0 \
--api.insecure=true \
--providers.docker
services:
traefik:
image: traefik:v3.0
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./traefik.yml:/traefik.yml:ro
networks:
- traefik
networks:
traefik:
name: traefik
api:
dashboard: true
insecure: true
entryPoints:
web:
address: ":80"
websecure:
address: ":443"
providers:
docker:
endpoint: "unix:///var/run/docker.sock"
exposedByDefault: false
log:
level: INFO
docker run -v ./traefik.yml:/traefik.yml:ro \
traefik:v3.0 --configFile=/traefik.yml
services:
myapp:
image: nginx
labels:
- "traefik.enable=true"
- "traefik.http.routers.myapp.rule=Host(`example.com`)"
- "traefik.http.routers.myapp.entrypoints=web"
- "traefik.http.services.myapp.loadbalancer.server.port=80"
traefik.enable=true
traefik.http.routers.myrouter.rule=Host(`domain.com`)
traefik.http.routers.myrouter.rule=PathPrefix(`/api`)
traefik.http.services.myservice.loadbalancer.server.port=3000
labels:
- "traefik.http.routers.app1.rule=Host(`app1.example.com`)"
- "traefik.http.routers.app2.rule=Host(`app2.example.com`)"
labels:
- "traefik.http.routers.api.rule=PathPrefix(`/api`)"
- "traefik.http.routers.web.rule=Path(`/admin`)"
labels:
- "traefik.http.routers.app.rule=Host(`app.example.com`) && PathPrefix(`/api`)"
| Hari | Topik | Praktik |
|---|---|---|
| 1 | Lets Encrypt | Automatic SSL |
| 2 | SSL Options | TLS configuration |
| 3 | Basic Auth | Password protection |
| 4 | Rate Limiting | Prevent abuse |
| 5 | Redirects | HTTP to HTTPS |
certificatesResolvers:
letsencrypt:
acme:
email: [email protected]
storage: /letsencrypt/acme.json
httpChallenge:
entryPoint: web
labels:
- "traefik.http.routers.myapp.rule=Host(`example.com`)"
- "traefik.http.routers.myapp.tls=true"
- "traefik.http.routers.myapp.tls.certresolver=letsencrypt"
labels:
- "traefik.http.routers.myapp.tls=true"
- "traefik.http.routers.myapp.tls.options.mytls.minVersion=VersionTLS12"
tls:
options:
default:
minVersion: VersionTLS12
sniStrict: true
labels:
- "traefik.http.routers.myapp.tls=true"
- "traefik.http.routers.myapp.tls.domains[0].main=example.com"
htpasswd -nb admin password | cut -d: -f2
http:
middlewares:
basic-auth:
basicAuth:
users:
- "admin:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"
labels:
- "traefik.http.routers.myapp.middlewares=basic-auth"
http:
middlewares:
rate-limit:
rateLimit:
average: 100
burst: 50
period: 1s
http:
middlewares:
rate-limit-per-ip:
rateLimit:
average: 10
burst: 20
period: 1s
sourceCriterion:
ipStrategy:
depth: 1
labels:
- "traefik.http.routers.http-catchall.rule=Host(`example.com`)"
- "traefik.http.routers.http-catchall.entrypoints=web"
- "traefik.http.routers.http-catchall.middlewares=redirect-to-https"
http:
middlewares:
redirect-to-https:
redirectScheme:
scheme: https
permanent: true
http:
middlewares:
redirect-old-to-new:
redirectRegex:
regex: "^/old/(.*)$"
replacement: "/new/$1"
permanent: true
| Hari | Topik | Praktik |
|---|---|---|
| 1 | Load Balancing | Multiple backends |
| 2 | Service Health | Health checks |
| 3 | File Provider | YAML config |
| 4 | Dashboard | Monitoring |
| 5 | Full Setup | Real example |
services:
app:
deploy:
replicas: 3
labels:
- "traefik.http.services.app.loadbalancer.server.port=8080"
labels:
- "traefik.http.services.app.loadbalancer.sticky.cookie=true"
- "traefik.http.services.app.loadbalancer.sticky.cookie.name=session"
labels:
- "traefik.http.services.app.loadbalancer.healthcheck.path=/health"
- "traefik.http.services.app.loadbalancer.healthcheck.interval=30s"
- "traefik.http.services.app.loadbalancer.healthcheck.timeout=5s"
- "traefik.http.services.app.loadbalancer.healthcheck.port=8080"
labels:
- "traefik.http.services.app.loadbalancer.healthcheck.hostname=myapp.local"
- "traefik.http.services.app.loadbalancer.healthcheck.scheme=http"
http:
routers:
my-router:
rule: "Host(`example.com`)"
service: my-service
tls: {}
services:
my-service:
loadBalancer:
servers:
- url: "http://localhost:8081"
- url: "http://localhost:8082"
providers:
docker: {}
file:
filename: /config/routes.yml
watch: true
api:
dashboard: true
insecure: true
labels:
- "traefik.http.routers.dashboard.rule=Host(`traefik.example.com`)"
- "traefik.http.routers.dashboard.service=api@internal"
metrics:
prometheus:
addEntryPointsLabels: true
addServicesLabels: true
services:
traefik:
image: traefik:v3.0
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./traefik.yml:/traefik.yml:ro
- ./letsencrypt:/letsencrypt
app1:
image: nginx
labels:
- "traefik.enable=true"
- "traefik.http.routers.app1.rule=Host(`app1.local`)"
- "traefik.http.routers.app1.entrypoints=websecure"
- "traefik.http.routers.app1.tls.certresolver=letsencrypt"
app2:
image: whoami
labels:
- "traefik.enable=true"
- "traefik.http.routers.app2.rule=PathPrefix(`/whoami`)"