使用 docker-compose 启动 traefik
 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  | 
version: '2'
services:
    traefik:
        image: traefik:v2.7
        container_name: traefik
        # 启用Web UI并监听docker
        command: --api.insecure=true --providers.docker
        networks:
            - minikube        
        ports:
            # HTTP 端口
            - "80:80"
            # Web UI 端口
            - "8080:8080"        
        labels:
            - "traefik.enable=false"
        volumes:
            # 这样Traefik可以监听Docker事件
            # - /var/run/docker.sock:/var/run/docker.sock
            # 启动配置文件
            - C:\Users\hxf16\traefik\traefik.toml:/etc/traefik/traefik.toml
            # 自定义routers、services
            - ~/traefik/conf:/etc/traefik/conf
        restart: always
networks:
  minikube:
    external: true
  | 
 
查看 minikube 使用的 docker 网络名称
使用命令
1
2
  | 
minikube profile
docker network ls
  | 
 
默认使用的网络名称为 minikube
注意需要在 traefik 目录下创建 conf 目录以及 traefik.toml 文件
 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
37
38
39
  | 
# 全局配置
[global]
  checkNewVersion = false
  sendAnonymousUsage = false
# 入口
[entryPoints]
  # http
  [entryPoints.web]
    address = ":80"
  # https "websecure"这个名字是自己定义的
  [entryPoints.websecure]
    address = ":443"
  #  traefik web ui
  [entryPoints.traefik]
    address = ":8080"
# 免费Let's Encrypt 证书(注意:必须域名解析到的主机中才有效,traefik会把秘钥写入acme.json中)
[certificatesResolvers.bxtlschallenge.acme]
  # 自己的邮箱地址
  email = "123456@qq.com"
  # 秘钥写入地址,根据自己文件映射来配置
  storage = "/letsencrypt/acme.json"
  [certificatesResolvers.bxtlschallenge.acme.httpChallenge]
    entryPoint = "web"
# 自定义Router、Services(貌似必须定义在外部文件里面,坑了好久)
[providers.file]
  directory = "/etc/traefik/conf"
# traefik 需要开启的功能
[api]
  insecure = true
  dashboard = true
[ping]
[providers.docker]
  endpoint = "unix:///var/run/docker.sock"
  exposedByDefault = true
# 传输配置
[serversTransport]
  # 如果后端需要代理https自定义证书,可以使用这种方式就可以跳过验证,不然会报x509证书错误
  insecureSkipVerify = true
  | 
 
在 conf 目录下放入 minikube.toml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  | 
[http.services]
  [http.services.minikube.loadBalancer]
    [[http.services.minikube.loadBalancer.servers]]
      url = "http://192.168.49.2:80/"
[http.routers]
  [http.routers.example]
    entryPoints = ["web"]
    rule = "Host(`a.example.com`)"
    service = "minikube"
  | 
 
启动 traefik
命令如下:
1
  | 
docker-compose -f docker-compose-traefik.yaml up -d
  | 
 
查看 dashboard,查看服务是否正常

正常后,局域网添加 hosts 文件内容
1
  | 
192.168.3.55 a.example.com
  | 
 
然后浏览器访问
 http://a.example.com/
 