1、建立单节点的Docker Swarm集群node
docker swarm initnginx
2、在Swarm集群中建立一个网络web
docker network create --driver=overlay traefik --attachabledocker
3、在Swarm集群中部署traefik负载均衡器服务浏览器
docker service create \网络
--name traefik \负载均衡
--constraint=node.role==manager \frontend
--publish 80:80 --publish 8080:8080 \dom
--mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \ide
--network traefik \
traefik \
--docker \
--docker.swarmmode \
--docker.domain=traefik \
--docker.watch \
--web
4、在Swarm集群中分别部署2个容器服务(httpd和nginx)
#2.1 httpd.abc.com
docker service create \
--name httpd \
--network traefik \
--label "traefik.backend=httpd" \
--label "traefik.enable=true" \
--label "traefik.protocol=http" \
--label "traefik.port=80" \
--label "traefik.frontend.rule=Host:httpd.abc.com" \
httpd
#2.2 nginx.abc.com
docker service create \
--name nginx \
--network traefik \
--label "traefik.backend=nginx" \
--label "traefik.enable=true" \
--label "traefik.protocol=http" \
--label "traefik.port=80" \
--label "traefik.frontend.rule=Host:nginx.abc.com" \
nginx
5、在浏览器中经过域名访问2个网站、查看Traefik UI
#修改Win7客户端电脑的hosts文件C:\Windows\System32\drivers\etc\hosts
#增长2条主机记录:
httpd.abc.com 192.168.3.168
nginx.abc.com 192.168.3.168
http://192.168.3.168:8080
http://httpd.abc.com
http://nginx.abc.com
6、Swarm集群中服务扩容后,再查看Traefik UI
docker service scale httpd=2
docker service scale nginx=3
http://192.168.3.168:8080
附图:
参考连接:
docker swarm模式使用traefik部署服务
https://blog.51cto.com/6764097/2066670
Docker Swarm (mode) cluster
http://docs.traefik.cn/user-guide/swarm-mode