Skip to content

Traefik 反向代理服务部署

准备工作

创建配置文件

proxy-stack.yml
 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
version: "3.9"

services:
  traefik:
    image: traefik:latest
    networks:
      - proxy
    ports:
      - 80:80
      - 8080:8080
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    command:
      - --accesslog=true
      - --api.insecure=true
      - --metrics.prometheus=true
      - --entrypoints.web.address=:80
      - --providers.docker.network=proxy
      - --providers.docker.swarmMode=true
      - --providers.docker.exposedbydefault=false
      - --providers.docker.endpoint=unix:///var/run/docker.sock
    deploy:
      mode: replicated
      replicas: 1
      placement:
        constraints: [node.role == manager]
      labels:
        - traefik.enable=true
        - traefik.http.routers.traefik-auth.entrypoints=web
        - traefik.http.routers.traefik-auth.rule=HostRegexp(`{host:^traefik.*}`)
        - traefik.http.services.traefik-auth.loadbalancer.server.port=8080
        - traefik.http.routers.traefik-auth.middlewares=auth
        - traefik.http.middlewares.auth.basicauth.users=${BASIC_AUTH}
networks:
  proxy:
    external: true

部署运行

生成密码 : echo $(htpasswd -nB admin)

在yml直接填写 : echo $(htpasswd -nB admin) | sed -e s/\\$/\\$\\$/g

Bash
1
2
export BASIC_AUTH=$(htpasswd -nB admin)
docker stack deploy -c proxy-stack.yml proxy