使用Docker swarm构建wordpress集群
最近都在忙,久不更新博客了,今天更新下Docker swarm集群和集群容错测试:1、如下拓扑:
2、环境说明:Master/nfs服务器:192.168.63.217
Worker:192.168.63.217
Worker:192.168.63.2163、创建一个nfs挂载目录,用于存放wordpress代码(63.217):#yum install -y nfs-utils
## cat /etc/exports
/web 192.168.63.0/24(rw,sync,fsid=0)
#systemctl enable rpcbind.service
#systemctl enable nfs-server.service
#systemctl start rpcbind.service
#systemctl start nfs-server.service4、挂载/web目录(worker):#yum install -y nfs-utils
#systemctl enable rpcbind.service
#systemctl start rpcbind.service
# mount192.168.63.217:/web /web挂载之后上传wordpress代码5、把我们在Dockerfile构建的镜像推送到本地仓库(这一块可以参考之前的博客,Dockerfile章节)# docker p_w_picpath tag lnmp/php:1.0 192.168.63.217:5000/lnmp/php:1.0
# docker p_w_picpath tag lnmp/nginx:1.0 192.168.63.217:5000/lnmp/nginx:1.0
# docker p_w_picpath tag lnmp/mysql:1.0 192.168.63.217:5000/lnmp/mysql:1.0
# docker push192.168.63.217:5000/lnmp/php:1.0
# docker push192.168.63.217:5000/lnmp/nginx:1.0
# docker push192.168.63.217:5000/lnmp/mysql:1.0##为了worker节点下载速度快点,我们建议先docker pull到本地6、构建Docker swarm集群63.217操作:# docker swarm init --advertise-addr 192.168.63.217
To add a worker to this swarm, run the following command:
docker swarm join \
--tokenSWMTKN-1-37fe5tarzcy50mazbe1e3ewyblkfk7xf9kx9ncanz0wx3q70e1-a7wn9tiepd114if6smuiqlez3\
192.168.63.217:2377##在worker节点上面操作,加入这个集群:
#docker swarm join--token SWMTKN-1-37fe5tarzcy50mazbe1e3ewyblkfk7xf9kx9ncanz0wx3q70e1-a7wn9tiepd114if6smuiqlez3 192.168.63.217:23777、查看集群的节点状态:# docker node ls
ID HOSTNAMESTATUS AVAILABILITYMANAGER STATUS
e9naz0ctzaaer4bwleruo34x6 master Ready Active Leader
rfcbavxd8yrixximm9e1i6dsn node1 Ready Active
shrzku0k3xx87526lkkkyrxsi node2 Ready Active8、为了使得Docker swarm集群容器互相通讯,我们先创建一个overlay网络:# docker network create --driver overlaynginx_network##注意内核版本要3.18以上,不够的话要升级
9、随机创建调度一个9000端口的Php service运行Docker容器:# docker service create --mount type=bind,source=/web/,target=/web/--network nginx_network --name php -p 9000:9000192.168.63.217:5000/lnmp/php:1.0
# docker service ls
ID NAME MODE REPLICAS IMAGE
ira3ezabroai php replicated 1/1 192.168.63.217:5000/lnmp/php:1.010、启动nginx service:#docker service create --mount type=bind,source=/web/,target=/web/ --network nginx_network --name web -p 80:80 192.168.63.217:5000/lnmp/nginx:1.011、再启动mysql service:# docker service create --mounttype=bind,source=/data/,target=/var/lib/mysql/ --network nginx_network --namemysql -p 3306:3306 192.168.63.217:5000/lnmp/mysql:1.012、我们也可以在复制出一个web service:# docker servicescale web=2
web scaled to 213、看一下我们的容器到底运行在哪个linux主机上呢:# docker service ls
ID NAME MODE REPLICAS IMAGE
ira3ezabroai php replicated 1/1 192.168.63.217:5000/lnmp/php:1.0
kcxqzxwe0dzb mysql replicated 1/1 192.168.63.217:5000/lnmp/mysql:1.0
ufn1n5phtsqn web replicated 2/2 192.168.63.217:5000/lnmp/nginx:1.0###### docker service ps php
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ptxokpvq1b7s php.1 192.168.63.217:5000/lnmp/php:1.0master Running Running 5 minutes ago
# docker service ps mysql
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
zowbxqnr9toi mysql.1 192.168.63.217:5000/lnmp/mysql:1.0node2 Running Running 2 minutes ago
# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
vhk44hij7gnu web.1 192.168.63.217:5000/lnmp/nginx:1.0node1 Running Running 3 minutes ago
u1vdnr0ujzl7 web.2 192.168.63.217:5000/lnmp/nginx:1.0node2 Running Running about a minute ago14、我们已经确认所有的容器都已经起起来并且正常运行了,访问一下web就可以进行80端口的访问了,192.168.63.217:#######直接到安装完成:
15、为了测试高可用性,我们把node1节点关闭掉,看容器是否转移:我们在node1执行关闭docker:# systemctl stop docker16、在master节点上查看一下状态:# docker node ls
ID HOSTNAMESTATUSAVAILABILITY MANAGER STATUS
e9naz0ctzaaer4bwleruo34x6master Ready Active Leader
rfcbavxd8yrixximm9e1i6dsn node1 Down Active
shrzku0k3xx87526lkkkyrxsi node2 Ready Active
17、我们可以看到node1节点已经Down,然后我们的Docker 容器也已经转移到了别的worker节点上:# docker service ps php
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
ptxokpvq1b7s php.1 192.168.63.217:5000/lnmp/php:1.0master Running Running 18 minutes ago
# docker service ps mysql
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
zowbxqnr9toi mysql.1 192.168.63.217:5000/lnmp/mysql:1.0node2 Running Running 15 minutes ago
# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
jqcuqzjdgi9y web.1 192.168.63.217:5000/lnmp/nginx:1.0master Running Running 2 minutes ago
vhk44hij7gnu \_web.1 192.168.63.217:5000/lnmp/nginx:1.0node1 Shutdown Running 2 minutes ago
u1vdnr0ujzl7 web.2 192.168.63.217:5000/lnmp/nginx:1.0node2 Running Running 14 minutes ago##我们可以看到在node1节点上之前运行的容器已经shutdown了,然后转移到了master和node2上,再次访问也是不影响的:
##到处已经完成了,我们可以看到通过Docker swarm构建集群是非常简单的,那么有没有compose可以结合swarm呢?在compose v3版本之后就体现了这个功能的强大。Docker详情与集群架构部分可以查看http://www.roncoo.com/course/view/3e9d9c48f76f4c698b8349d04b763467
页:
[1]