7.4 deploy WordPress through service in swarm cluster

Time:2021-10-22

Create an overlay driven network

docker network create -d overlay wordpress
[[email protected] ~]$ docker network ls
NETWORK ID NAME DRIVER SCOPE
q3rp3i79eud7 wordpress overlay swarm

Create MySQL container

docker service create --name mysql --env MYSQL_ROOT_PASSWORD=root --env MYSQL_DATABASE=wordpress --network wordpress --mount type=volume,source=mysql-data,destination=/var/lib/mysql mysql

Create WordPress

docker service create --name wordpress -p 80:80 --env WORDPRESS_DB_PASSWORD=root --env WORDPRESS_DB_HOST=mysql --network wordpress wordpress

View node distribution

#Service list
[[email protected] ~]$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
vxz0bfmul9th mysql replicated 1/1 mysql:5.7.22
cpe3wqrsoqxi wordpress replicated 1/1 wordpress:latest *:80->80/tcp

#Node distribution of WordPress service
[[email protected] ~]$ docker service ps wordpress
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
tnh4f0ldyidl wordpress.1 wordpress:latest swarm-manager Running Running 4 minutes ago

#Node distribution of MySQL service
[[email protected] ~]$ docker service ps mysql
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
jns8bllzbz94 mysql.1 mysql:5.7.22 swarm-work1 Running Running 33 minutes ago

Although WordPress is only distributed on the swarm Manager (192.168.205.10), we find that the effect is the same when we use the following links. Isn’t it amazing… See 7.6 for specific reasons

http://192.168.205.10/
http://192.168.205.11/
http://192.168.205.12/

In swarm mode, there is no need to install etcd. Swarm will automatically help us maintain the distributed overlay network