# 1. 在节点A上声明自己是 manager docker swarm init --advertise-addr=`hostname -i | awk '{print $1}'` # Swarm initialized: current node (ob8gsaxiz3a4k8rubrv20im2q) is now a manager. # To add a worker to this swarm, run the following command: # docker swarm join --token SWMTKN-1-4luou0jd1580qrjuodc74c7nnavzdoj6pmo8sdfjxy7i0mdld3-45orfnut6qf9ey4kxzs36yjkw 192.168.0.8:2377 # To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
# 3. 在 manager 上查看节点 docker node ls # ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION # ob8gsaxiz3a4k8rubrv20im2q * node1 Ready Active Leader 19.03.11 # jlhp9zpjdhgwygxxl0hrky794 node2 Ready Active 19.03.11 # cohz6uotcm6iogvlxmrh7cdwi node3 Ready Active 19.03.11
# 4. 在 manager 上创建一个 service docker service create --name service1 alpine /bin/ping 127.0.0.1 # u83i2d5k6aiog3elo9fndbic5 # overall progress: 1 out of 1 tasks # 1/1: running [==================================================>] # verify: Service converged
# 5. 在 manager 上查看 service docker service ls # ID NAME MODE REPLICAS IMAGE PORTS # u83i2d5k6aio service1 replicated 1/1 alpine:latest
# 6. 在 manager 上给 service 动态扩容成 2 个节点 docker service scale service1=2
手动搭建一个 wordpress
1 2 3 4 5 6 7 8
# 1. 创建一个 overlay 网络 docker network create -d overlay my-overlay # 2. 创建 mysql 的 service docker service create --name my-mysql --network my-overlay --mount type=volume,source=mysql-data,destination=/var/lib/mysql --env MYSQL_ROOT_PASSWORD=root --env MYSQL_DATABASE=wordpress mysql # 3. 创建 wordpress 的 service docker service create --name my-wordpress --network my-overlay -p 80:80 --env WORDPRESS_DB_PASSWORD=root --env WORDPRESS_DB_HOST=mysql wordpress # 4. 为 wordpress 扩容 docker service scale my-wordpress=3