实验环境
准备三台虚拟机
172.25.11.12 rhel7.3 manager vm2
172.25.11.13 rhel7.3 node vm3
172.25.11.14 rhel7.3 node vm4
三台主机上:
yum install -y docker-engine-17.03.1.ce-1.el7.centos.x86_64.rpm docker-engine-selinux-17.03.1.ce-1.el7.centos.noarch.rpm
systemctl start docker
yum install -y bash-completion.noarch # 若Tab键补不齐的话下载此软件
vm2上:
[root@vm2 ~]# docker swarm init
Swarm initialized: current node (xdjl4l6y6lw3r9nww9zhijf7l) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-47uuc1popatt8a08ncyh3kermwul5fvf3quq84fhrii3nt7wtp-3w1ssi01g7modi6ls7dxhuj86 \
172.25.11.12:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
node节点上都做:
[root@vm3 ~]# docker swarm join \
> --token SWMTKN-1-47uuc1popatt8a08ncyh3kermwul5fvf3quq84fhrii3nt7wtp-3w1ssi01g7modi6ls7dxhuj86 \
> 172.25.11.12:2377
This node joined a swarm as a worker.
将主机证书复制到每个主机上,以便使用主机上的仓库
仓库搭建:容器仓库搭建
容器仓库搭建上传拉取认证
在manager上:
docker node ls
docker pull westos.org/nginx # 拉取的前提主机仓库中必须有
docker service create --name nginx --publish 80:80 --replicas 3 westos.org/nginx
docker pull westos.org/visualizer
[root@vm2 ~]# docker service create \
> --name=viz \
> --publish=8080:8080/tcp \
> --constraint=node.role==manager \
> --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
> westos.org/visualizer
浏览器:
每个主机都做
docker cp index.html nginx.3.0ht3bzq63yjckumz2tu85bfzj:/usr/share/nginx/html # 设置首页文件
验证负载均衡:
docker service update --image westos.org/game2048 --update-parallelism 10 --update-delay 10s nginx # 大批量更新 10s一次,一次10个
浏览器查看:页面变化