Published on

使用 Docker Compose 快速安装 Traefik

Authors
  • avatar
    Name
    Neo
    Twitter

关注公众号,第一时间获取更新

关注前沿科技,分享独立开发故事。欢迎一起寻找利基市场,构建小而美的生意

HackerNeo公众号

Traefik 是一个开源的边缘路由器,具有和 Nginx 类似的反向代理功能。相较 Nginx,Traefik 自身就具备服务发现能力,可以自动将你的服务向外暴露,不需要再手动去配置端口和 SSL 证书等。

下面,我们将使用 docker compose 来安装 Traefik,几分钟即可搞定。

Table of Contents

1. 创建文件夹和文件

mkdir ~/traefik
cd ~/traefik
mkdir -p data/configurations
touch docker-compose.yml
touch data/traefik.yml
touch data/acme.json
touch data/configurations/dynamic.yml
chmod 600 data/acme.json

2. 配置 docker-compose.yml

文件路径为 ~/traefik/docker-compose.yml

version: '3.7'

services:
  traefik:
    image: traefik:v2.4
    container_name: traefik
    restart: always
    security_opt:
      - no-new-privileges:true
    ports:
      - 80:80
      - 443:443
    volumes:
      # 如果宿主机是标准 linux 系统,可以设置一下localtime
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./data/traefik.yml:/traefik.yml:ro # 映射静态配置文件
      - ./data/acme.json:/acme.json # 映射证书文件,SSL 证书申请成功后,就会存在这个文件中
      - ./data/configurations:/configurations # 映射动态配置文件
    networks:
      - traefik
    labels:
      # 下面这些标签,可以帮助 traefik 正确处理该服务
      - 'traefik.enable=true'
      - 'traefik.docker.network=traefik' # 指定 docker network
      # 指定服务入口为 websecure,websecure 会在静态配置文件traefik.yml中定义
      - 'traefik.http.routers.traefik-secure.entrypoints=websecure'
      # 定义访问域名,需要做 DNS 解析
      - 'traefik.http.routers.traefik-secure.rule=Host(`traefik.yourdomain.com`)'
      - 'traefik.http.routers.traefik-secure.middlewares=user-auth@file'
      - 'traefik.http.routers.traefik-secure.service=api@internal'

networks:
  traefik:
    external: true

创建 docker-compose.yml 内需要的 network

docker network create traefik

3. 修改静态配置文件

文件路径为 ~/traefik/data/traefik.yml

api:
  dashboard: true

entryPoints:
  web:
    address: :80
    http:
      redirections:
        entryPoint:
          to: websecure

  websecure:
    address: :443
    http:
      middlewares:
        - secureHeaders@file
        - nofloc@file
      tls:
        certResolver: buypass

pilot:
  dashboard: false

providers:
  docker:
    endpoint: 'unix:///var/run/docker.sock'
    exposedByDefault: false
  file:
    filename: /configurations/dynamic.yml # 动态配置文件位置

certificatesResolvers:
  letsencrypt:
    acme:
      email: admin@yourdomain # 更改为你的域名
      storage: acme.json
      keyType: EC384
      httpChallenge:
        entryPoint: web

  buypass:
    acme:
      email: admin@yourdomain # 更改为你的域名
      storage: acme.json
      caServer: https://api.buypass.com/acme/directory
      keyType: EC256
      httpChallenge:
        entryPoint: web

4. 修改动态配置文件

文件路径为 ~/traefik/data/configurations/dynamic.yml

# Dynamic configuration
http:
  middlewares:
    nofloc:
      headers:
        customResponseHeaders:
          Permissions-Policy: 'interest-cohort=()'
    secureHeaders:
      headers:
        sslRedirect: true
        forceSTSHeader: true
        stsIncludeSubdomains: true
        stsPreload: true
        stsSeconds: 31536000

    # UserName : admin
    # Password : qwer1234
    user-auth:
      basicAuth:
        # users 选项是认证用户的列表
        # 使用 echo $(htpasswd -nb user password) | sed -e s/\\$/\\$\\$/g
        # 来创建 user:password 键值对
        users:
          - 'admin:$apr1$tm53ra6x$FntXd6jcvxYM/YH0P2hcc1'

tls:
  options:
    default:
      cipherSuites:
        - TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
        - TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
        - TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
        - TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
        - TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305
        - TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305
      minVersion: VersionTLS12

5. 解析域名

docker-compose.yml 中,我们指定了使用 traefik.yourdomain.com 作为访问域名。因此,我们需要将 traefik.yourdomain.com 解析至 traefik 服务入口 IP 地址。

若使用单个云服务器部署 traefik,就需要将域名解析到服务器 IP 地址上

6. 启动服务

cd ~/traefik  # 确保位于 docker-compose.yml 所在目录
docker-compose up -d

7. 访问 Traefik Dashboard

在浏览器中访问 traefik.yourdomain.com,会发现自动加上了 SSL。

输入静态配置文件 dynamic.yml 中的用户名和密码,即可进入 Traefik Dashboard。

Traefik Dashboard 用户名和密码
Traefik Dashboard