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 image tag lnmp/php:1.0 192.168.63.217:5000/lnmp/php:1.0
[root@masterwordpress]# docker image tag lnmp/nginx:1.0 192.168.63.217:5000/lnmp/nginx:1.0
[root@masterwordpress]# docker image 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