评论

收藏

[办公软件] 使用Docker swarm构建wordpress集群

电脑办公 电脑办公 发布于:2021-06-25 09:21 | 阅读数:598 | 评论:0

  最近都在忙,久不更新博客了,今天更新下Docker swarm集群和集群容错测试:
1、如下拓扑: DSC0000.png
2、环境说明:
Master/nfs服务器:192.168.63.217
Worker:192.168.63.217
Worker:192.168.63.216
3、创建一个nfs挂载目录,用于存放wordpress代码(63.217):
[root@master ~]#yum install -y nfs-utils
[root@master ~]## cat /etc/exports
/web 192.168.63.0/24(rw,sync,fsid=0)
[root@master ~]#systemctl enable rpcbind.service
[root@master ~]#systemctl enable nfs-server.service
[root@master ~]#systemctl start rpcbind.service
[root@master ~]#systemctl start nfs-server.service
4、挂载/web目录(worker):
[root@node2 ~]#yum install -y nfs-utils
[root@node2 ~]#systemctl enable rpcbind.service
[root@node2 ~]#systemctl start rpcbind.service
[root@webstatus ~]# mount192.168.63.217:/web /web挂载之后上传wordpress代码
5、把我们在Dockerfile构建的镜像推送到本地仓库(这一块可以参考之前的博客,Dockerfile章节)
[root@masterwordpress]# docker p_w_picpath tag lnmp/php:1.0 192.168.63.217:5000/lnmp/php:1.0
[root@masterwordpress]# docker p_w_picpath tag lnmp/nginx:1.0 192.168.63.217:5000/lnmp/nginx:1.0
[root@masterwordpress]# docker p_w_picpath tag lnmp/mysql:1.0 192.168.63.217:5000/lnmp/mysql:1.0
[root@master wordpress]# docker push192.168.63.217:5000/lnmp/php:1.0
[root@master wordpress]# docker push192.168.63.217:5000/lnmp/nginx:1.0
[root@master wordpress]# docker push192.168.63.217:5000/lnmp/mysql:1.0
##为了worker节点下载速度快点,我们建议先docker pull到本地6、构建Docker swarm集群63.217操作:
[root@master~]# 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节点上面操作,加入这个集群:
[root@node2 ~]#docker swarm join  --token SWMTKN-1-37fe5tarzcy50mazbe1e3ewyblkfk7xf9kx9ncanz0wx3q70e1-a7wn9tiepd114if6smuiqlez3 192.168.63.217:2377
7、查看集群的节点状态:
[root@master wordpress]# docker node ls
ID               HOSTNAME  STATUS AVAILABILITY  MANAGER STATUS
e9naz0ctzaaer4bwleruo34x6   master   Ready   Active    Leader
rfcbavxd8yrixximm9e1i6dsn  node1  Ready   Active  
shrzku0k3xx87526lkkkyrxsi  node2  Ready   Active
8、为了使得Docker swarm集群容器互相通讯,我们先创建一个overlay网络:
[root@master docker]# docker network create --driver overlaynginx_network
##注意内核版本要3.18以上,不够的话要升级
9、随机创建调度一个9000端口的Php service运行Docker容器:
[root@masterwordpress]# 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
[root@masterwordpress]# docker service ls
ID          NAME        MODE        REPLICAS      IMAGE
ira3ezabroai    php         replicated      1/1         192.168.63.217:5000/lnmp/php:1.0
10、启动nginx service:
[root@masterwordpress]#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.0
11、再启动mysql service:
[root@masterwordpress]# 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.0
12、我们也可以在复制出一个web service:
[root@master wordpress]# docker servicescale web=2
web scaled to 2
13、看一下我们的容器到底运行在哪个linux主机上呢:
[root@master wordpress]# 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
#####
[root@master wordpress]# 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.0  master        Running       Running 5 minutes ago           
[root@master wordpress]# 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.0  node2         Running       Running 2 minutes ago           
[root@master wordpress]# 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.0  node1         Running       Running 3 minutes ago            
u1vdnr0ujzl7     web.2        192.168.63.217:5000/lnmp/nginx:1.0  node2         Running       Running about a minute ago
14、我们已经确认所有的容器都已经起起来并且正常运行了,访问一下web就可以进行80端口的访问了,192.168.63.217: DSC0001.png ### DSC0002.png ####直接到安装完成:
DSC0003.png
15、为了测试高可用性,我们把node1节点关闭掉,看容器是否转移:我们在node1执行关闭docker:
[root@node1 web]# systemctl stop docker
16、在master节点上查看一下状态:
[root@master web]# docker node ls
ID              HOSTNAME  STATUS  AVAILABILITY MANAGER STATUS
e9naz0ctzaaer4bwleruo34x6  master  Ready   Active     Leader
rfcbavxd8yrixximm9e1i6dsn   node1   Down  Active  
shrzku0k3xx87526lkkkyrxsi   node2   Ready   Active
17、我们可以看到node1节点已经Down,然后我们的Docker 容器也已经转移到了别的worker节点上:
[root@master web]# 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.0  master        Running       Running 18 minutes ago           
[root@master web]# 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.0  node2         Running       Running 15 minutes ago           
[root@master web]# 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.0  master        Running       Running 2 minutes ago          
vhk44hij7gnu     \_web.1      192.168.63.217:5000/lnmp/nginx:1.0  node1         Shutdown      Running 2 minutes ago          
u1vdnr0ujzl7     web.2        192.168.63.217:5000/lnmp/nginx:1.0  node2         Running       Running 14 minutes ago
##我们可以看到在node1节点上之前运行的容器已经shutdown了,然后转移到了master和node2上,再次访问也是不影响的: DSC0004.png
##到处已经完成了,我们可以看到通过Docker swarm构建集群是非常简单的,那么有没有compose可以结合swarm呢?在compose v3版本之后就体现了这个功能的强大。  Docker详情与集群架构部分可以查看http://www.roncoo.com/course/view/3e9d9c48f76f4c698b8349d04b763467
关注下面的标签,发现更多相似文章