当前位置:   article > 正文

Docker 使用traefik 进行负载均衡_docker traefik

docker traefik
  1. traefik是一个http反向代理、负载均衡工具.
  2. traefik支持较多的后端协议(http[s]、Websocket、TCP)
  3. traefik支持多种容器框架(docker compose、Swarm、k8s、Mesos), 能识别API后台热更新配置.

1,创建Traefik

安装docker,自行百度
  1. # traefik配置文件 traefik.toml
  2. [global]
  3. checkNewVersion = false
  4. sendAnonymousUsage = false
  5. [log]
  6. level = "WARN"
  7. format = "common"
  8. [api]
  9. dashboard = true
  10. insecure = true
  11. [ping]
  12. [accessLog]
  13. [providers]
  14. [providers.docker]
  15. watch = true
  16. exposedByDefault = false
  17. endpoint = "unix:///var/run/docker.sock"
  18. swarmMode = false
  19. useBindPortIP = false
  20. network = "traefik"
  21. [providers.file]
  22. watch = true
  23. directory = "/etc/traefik/config"
  24. debugLogGeneratedTemplate = true
  25. [entryPoints]
  26. [entryPoints.http]
  27. address = ":80"
  28. [entryPoints.https]
  29. address = ":443"

FALLBACK

  1. #docker-compose.yml
  2. version: '3.7'
  3. services:
  4. traefik:
  5. container_name: traefik
  6. image: traefik:v2.1.3
  7. restart: always
  8. ports:
  9. - 80:80
  10. - 443:443
  11. - 8080:8080
  12. networks:
  13. - traefik
  14. command: traefik --configFile /etc/traefik.toml
  15. volumes:
  16. - /var/run/docker.sock:/var/run/docker.sock:ro
  17. - ./traefik.toml:/etc/traefik.toml:ro
  18. - ./config/:/etc/traefik/config/:ro
  19. healthcheck:
  20. test: ["CMD-SHELL", "wget -q --spider --proxy off localhost:8080/ping || exit 1"]
  21. # 先创建外部网卡
  22. # docker network create traefik
  23. networks:
  24. traefik:
  25. external: true

FALLBACK

1.1 操作顺序

  1. 1,创建 traefik 文件目录
  2. 2,分别创建并保存(配置内容看上方),docker-compose.yml,traefik.toml
  3. 3,启动 docker-compose down && docker-compose up -d
  4. 4, 验证 浏览器输入ip 地址看到 404 即可成功;
  5. 5,访问8080 端口可看到 traefik dashboard 界面(下图);

2,创建Nginx

  1. # docker-compose.yml
  2. version: "3.6"
  3. services:
  4. web_nginx:
  5. image: nginx:1.17.1-alpine
  6. restart: always
  7. expose:
  8. - 80
  9. networks:
  10. - traefik
  11. labels:
  12. - "traefik.enable=true"
  13. - "traefik.docker.network=traefik"
  14. - "traefik.http.routers.web_nginx.entrypoints=http"
  15. - "traefik.http.routers.web_nginx.rule=Host(`web_nginx.halobug.cn`)"
  16. - "traefik.http.services.web_nginx-backend.loadbalancer.server.scheme=http"
  17. - "traefik.http.services.web_nginx-backend.loadbalancer.server.port=80"
  18. logging:
  19. driver: "json-file"
  20. options:
  21. max-size: "1m"
  22. networks:
  23. traefik:
  24. external: true

FALLBACK

2.1 操作顺序

  1. 1,创建 web_nginx 文件目录
  2. 2,分别创建并保存(配置内容看上方),docker-compose.yml
  3. 3,启动命令稍有不同,docker-compose down && docker-compose up --scale web_nginx=10 -d
  4. 4,绑定hosts,127.0.0.1 web_nginx.halobug.cn
  5. 5,浏览器访问,http://web_nginx.halobug.cn
  6. 6,查看 traefik dashboard,http://127.0.0.1:8080/dashboard
  7. 7 ,查看日志 ,docker-compose logs -f

2.1.3,nginx容器启动成功后如下图

2.1.5,访问浏览器刷新多次,后查看日志

2.1.6,看到启动的web服务

2.1.7,log日志显示,已经负载到了每个容器

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/721581
推荐阅读
相关标签
  

闽ICP备14008679号