๐Ÿ”ง DevOps

Traefik

Traefik

A cloud-native reverse proxy. Automatic configuration, Let's Encrypt integration.

Detailed Explanation

Traefik (pronounced "traffic") is a cloud-native reverse proxy and load balancer developed in 2015 by Containous (now Traefik Labs). Its key feature is that it automatically detects various infrastructure environments such as Docker, Kubernetes, and Consul and dynamically updates its configuration.

Traefik's greatest advantage is automatic service discovery. Routing is configured automatically once you define only Docker labels or Kubernetes Ingress/IngressRoute annotations. Unlike Nginx, you do not need to manage configuration files directly and reload.

Let's Encrypt integration is built in, so HTTPS certificate issuance and renewal are handled automatically. It supports HTTP Challenge, DNS Challenge, and TLS-ALPN Challenge, and can also issue wildcard certificates. Files, Kubernetes Secret, Consul, and others are used as the certificate store.

In practice, it is widely used as an API Gateway in microservice environments and as a Kubernetes Ingress Controller. Through middleware chains, authentication, rate limiting, redirects, header manipulation, and more are configured declaratively. Integration with Prometheus metrics and Jaeger tracing is also simple.

Code Example

# docker-compose.yml - Traefik ๊ธฐ๋ณธ ์„ค์ •
version: '3.8'

services:
  traefik:
    image: traefik:v3.0
    command:
      - --api.dashboard=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80
      - --entrypoints.websecure.address=:443
      # Let's Encrypt ์ž๋™ ์ธ์ฆ์„œ
      - --certificatesresolvers.letsencrypt.acme.email=admin@example.com
      - --certificatesresolvers.letsencrypt.acme.storage=/letsencrypt/acme.json
      - --certificatesresolvers.letsencrypt.acme.httpchallenge.entrypoint=web
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./letsencrypt:/letsencrypt
    labels:
      # ๋Œ€์‹œ๋ณด๋“œ ํ™œ์„ฑํ™”
      - traefik.enable=true
      - traefik.http.routers.dashboard.rule=Host(`traefik.example.com`)
      - traefik.http.routers.dashboard.service=api@internal
      - traefik.http.routers.dashboard.middlewares=auth
      - traefik.http.middlewares.auth.basicauth.users=admin:$$apr1$$...

  # ๋ฐฑ์—”๋“œ ์„œ๋น„์Šค ์˜ˆ์‹œ
  api:
    image: myapp/api:latest
    labels:
      - traefik.enable=true
      - traefik.http.routers.api.rule=Host(`api.example.com`)
      - traefik.http.routers.api.entrypoints=websecure
      - traefik.http.routers.api.tls.certresolver=letsencrypt
      # ๋ฏธ๋“ค์›จ์–ด ์ฒด์ธ
      - traefik.http.routers.api.middlewares=ratelimit,compress
      # ๋ ˆ์ดํŠธ ๋ฆฌ๋ฐ‹ ๋ฏธ๋“ค์›จ์–ด
      - traefik.http.middlewares.ratelimit.ratelimit.average=100
      - traefik.http.middlewares.ratelimit.ratelimit.burst=50
      # ์••์ถ• ๋ฏธ๋“ค์›จ์–ด
      - traefik.http.middlewares.compress.compress=true

---
# Kubernetes IngressRoute (Traefik CRD)
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
  name: api-route
spec:
  entryPoints:
    - websecure
  routes:
    - match: Host(`api.example.com`) && PathPrefix(`/v1`)
      kind: Rule
      services:
        - name: api-service
          port: 8080
      middlewares:
        - name: api-ratelimit
        - name: api-auth
  tls:
    certResolver: letsencrypt
---
# ๋ฏธ๋“ค์›จ์–ด ์ •์˜
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
  name: api-ratelimit
spec:
  rateLimit:
    average: 100
    burst: 50
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
  name: api-auth
spec:
  forwardAuth:
    address: http://auth-service:8080/verify
    authResponseHeaders:
      - X-User-Id
      - X-User-Role

How It's Said in Practice

Senior: "Every time I deploy a new service, modifying the Nginx configuration and reloading is a hassle. If we switch to Traefik, just adding labels makes it automatic."

Junior: "Are Let's Encrypt certificates also managed automatically? I'm renewing them manually right now."

Senior: "Yes, once you set up a certresolver, everything from issuance to renewal is automatic. You don't even need to set up things like certificate expiration alerts."

Interviewer: "What is the difference between Nginx and Traefik?"

Candidate: "Nginx is based on static configuration and requires a reload when changes are made, whereas Traefik automatically detects Docker/Kubernetes and enables dynamic configuration. Let's Encrypt integration is built in, and authentication and rate limiting can be configured declaratively through middleware chains, making it better suited to microservice environments."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "The IngressRoute has no rateLimit middleware. If it's a public API, rate limiting is a must."

Developer: "I just need to add a Middleware CRD and link it in the middlewares array, right? What would be appropriate values for average and burst?"

Reviewer: "About 2x the current traffic should do, and adjust it by looking at the Prometheus metrics."

Cautions

Learn More