traefik是一个http反向代理、负载均衡工具.
traefik支持较多的后端协议(http[s]、Websocket、TCP)
traefik支持多种容器框架(docker compose、Swarm、k8s、Mesos), 能识别API后台热更新配置.
1,创建Traefik
安装docker,自行百度
1# traefik配置文件 traefik.toml
2
3[global]
4 checkNewVersion = false
5 sendAnonymousUsage = false
6
7[log]
8 level = "WARN"
9 format = "common"
10
11[api]
12 dashboard = true
13 insecure = true
14
15[ping]
16
17[accessLog]
18
19[providers]
20 [providers.docker]
21 watch = true
22 exposedByDefault = false
23 endpoint = "unix:///var/run/docker.sock"
24 swarmMode = false
25 useBindPortIP = false
26 network = "traefik"
27 [providers.file]
28 watch = true
29 directory = "/etc/traefik/config"
30 debugLogGeneratedTemplate = true
31
32[entryPoints]
33 [entryPoints.http]
34 address = ":80"
35 [entryPoints.https]
36 address = ":443"
1#docker-compose.yml
2
3version: '3.7'
4
5services:
6
7 traefik:
8 container_name: traefik
9 image: traefik:v2.1.3
10 restart: always
11 ports:
12 - 80:80
13 - 443:443
14 - 8080:8080
15 networks:
16 - traefik
17 command: traefik --configFile /etc/traefik.toml
18 volumes:
19 - /var/run/docker.sock:/var/run/docker.sock:ro
20 - ./traefik.toml:/etc/traefik.toml:ro
21 - ./config/:/etc/traefik/config/:ro
22 healthcheck:
23 test: ["CMD-SHELL", "wget -q --spider --proxy off localhost:8080/ping || exit 1"]
24
25# 先创建外部网卡
26# docker network create traefik
27networks:
28 traefik:
29 external: true
30
1.1 操作顺序
1,创建 traefik 文件目录
2,分别创建并保存(配置内容看上方),docker-compose.yml,traefik.toml
3,启动 docker-compose down && docker-compose up -d
4, 验证 浏览器输入ip 地址看到 404 即可成功;
5,访问8080 端口可看到 traefik dashboard 界面(下图);
2,创建Nginx
1# docker-compose.yml
2version: "3.6"
3
4 services:
5
6 web_nginx:
7 image: nginx:1.17.1-alpine
8 restart: always
9 expose:
10 - 80
11 networks:
12 - traefik
13 labels:
14 - "traefik.enable=true"
15 - "traefik.docker.network=traefik"
16 - "traefik.http.routers.web_nginx.entrypoints=http"
17 - "traefik.http.routers.web_nginx.rule=Host(`web_nginx.halobug.cn`)"
18 - "traefik.http.services.web_nginx-backend.loadbalancer.server.scheme=http"
19 - "traefik.http.services.web_nginx-backend.loadbalancer.server.port=80"
20 logging:
21 driver: "json-file"
22 options:
23 max-size: "1m"
24 networks:
25 traefik:
26 external: true
2.1 操作顺序
1,创建 web_nginx 文件目录
2,分别创建并保存(配置内容看上方),docker-compose.yml
3,启动命令稍有不同,docker-compose down && docker-compose up --scale web_nginx=10 -d
4,绑定hosts,127.0.0.1 web_nginx.halobug.cn
5,浏览器访问,http://web_nginx.halobug.cn
6,查看 traefik dashboard,http://127.0.0.1:8080/dashboard
7 ,查看日志 ,docker-compose logs -f
2.1.3,nginx容器启动成功后如下图
2.1.5,访问浏览器刷新多次,后查看日志
2.1.6,看到启动的web服务
2.1.7,log日志显示,已经负载到了每个容器