一、创建单节点的Docker Swarm集群
docker swarm init
二、在Swarm集群中创建一个网络
docker network create --driver=overlay traefik --attachable
三、在Swarm集群中部署traefik负载均衡器服务
docker service create \
--name traefik \
--constraint=node.role==manager \
--publish 80:80 --publish 8080:8080 \
--mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \
--network traefik \
traefik \
--docker \
--docker.swarmmode \
--docker.domain=traefik \
--docker.watch \
--web
四、在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
五、在浏览器中通过域名访问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
六、Swarm集群中服务扩容后,再查看Traefik UI
docker service scale httpd=2
docker service scale nginx=3
http://192.168.3.168:8080
附图:
参考链接:
docker swarm模式使用traefik部署服务
http://blog.51cto.com/6764097/2066670
Docker Swarm (mode) cluster
http://docs.traefik.cn/user-guide/swarm-mode