4
Service:Traefik
Jeffery edited this page 2026-06-11 10:03:47 +08:00

簡介

Traefik 是一套現代化的反向代理與負載平衡器,常用於統一管理各服務的對外路由與 TLS。本文記錄在 Ubuntu 上以二進位檔搭配 systemd 部署 Traefik 的步驟,並附上範例設定檔。

下載 Traefik 壓縮檔

從官方下載指定版本的壓縮檔。

wget https://github.com/traefik/traefik/releases/download/v3.7.4/traefik_v3.7.4_linux_amd64.tar.gz

解壓縮

只解出 traefik 執行檔並放到 /usr/local/bin/

tar zxvf traefik_v3.7.4_linux_amd64.tar.gz -C /usr/local/bin/ traefik

建立 Traefik 服務

以 systemd 管理服務,可開機自動啟動、崩潰時自動重啟。先建立服務設定檔。

nano /etc/systemd/system/traefik.service

服務檔內容如下:

[Unit]
Description=Traefik Proxy

[Service]
Environment=HOME=/root
Type=simple
WorkingDirectory=/root
ExecStart=/usr/local/bin/traefik --configFile=traefik.toml
Restart=always
User=root

[Install]
WantedBy=multi-user.target

啟動服務

重新載入 systemd 設定、設為開機自動啟動、啟動服務並檢視狀態。

systemctl daemon-reload && systemctl enable traefik.service && systemctl start traefik.service && systemctl status traefik.service

停止服務

停止、取消開機啟動並移除服務設定檔。

systemctl stop traefik.service && systemctl disable traefik.service && rm /etc/systemd/system/traefik.service

範例設定檔

建立設定檔,路徑須與服務檔 --configFile 指定的位置一致。

nano /root/traefik.toml
################################################################
#
# Configuration sample for Traefik v2.
#
# For Traefik v1: https://github.com/traefik/traefik/blob/v1.7/traefik.sample.toml
#
################################################################

################################################################
# Global configuration
################################################################
[global]
  checkNewVersion = true
  sendAnonymousUsage = true

################################################################
# Entrypoints configuration
################################################################

# Entrypoints definition
#
# Optional
# Default:
[entryPoints]
  [entryPoints.web]
    address = ":80"

  [entryPoints.websecure]
    address = ":443"

################################################################
# Traefik logs configuration
################################################################

# Traefik logs
# Enabled by default and log to stdout
#
# Optional
#
[log]

  # Log level
  #
  # Optional
  # Default: "ERROR"
  #
  # level = "DEBUG"

  # Sets the filepath for the traefik log. If not specified, stdout will be used.
  # Intermediate directories are created if necessary.
  #
  # Optional
  # Default: os.Stdout
  #
  # filePath = "log/traefik.log"

  # Format is either "json" or "common".
  #
  # Optional
  # Default: "common"
  #
  # format = "json"

################################################################
# Access logs configuration
################################################################

# Enable access logs
# By default it will write to stdout and produce logs in the textual
# Common Log Format (CLF), extended with additional fields.
#
# Optional
#
# [accessLog]

  # Sets the file path for the access log. If not specified, stdout will be used.
  # Intermediate directories are created if necessary.
  #
  # Optional
  # Default: os.Stdout
  #
  # filePath = "/path/to/log/log.txt"

  # Format is either "json", "common", or "genericCLF".
  # - "common": Traefik's extended CLF format (default)
  # - "genericCLF": Standard CLF format compatible with standard log analyzers
  # - "json": JSON format for structured logging
  #
  # Optional
  # Default: "common"
  #
  # format = "json"
  # format = "genericCLF"

################################################################
# API and dashboard configuration
################################################################

# Enable API and dashboard
[api]

  # Enable the API in insecure mode
  #
  # Optional
  # Default: false
  #
  # insecure = true

  # Enabled Dashboard
  #
  # Optional
  # Default: true
  #
  # dashboard = false

################################################################
# Ping configuration
################################################################

# Enable ping
[ping]

  # Name of the related entry point
  #
  # Optional
  # Default: "traefik"
  #
  # entryPoint = "traefik"

################################################################
# Docker configuration backend
################################################################

# Enable Docker configuration backend
[providers.docker]

  # Docker server endpoint. Can be a tcp or a unix socket endpoint.
  #
  # Required
  # Default: "unix:///var/run/docker.sock"
  #
  # endpoint = "tcp://10.10.10.10:2375"

  # Default host rule.
  #
  # Optional
  # Default: "Host(`{{ normalize .Name }}`)"
  #
  # defaultRule = "Host(`{{ normalize .Name }}.docker.localhost`)"

  # Expose containers by default in traefik
  #
  # Optional
  # Default: true
  #
  # exposedByDefault = false