112、如何使用stack (Swarm19)
定义好了 stack YAML 文件,就可以通过 docker stack deploy 命令部署应用,命令最后面的wpstack 是自动创建各项资源的前缀,方便区分不同的环境
root@host03:~# docker stack deploy -c wp.yaml wpstack # 部署stack
Creating network wpstack_default
Creating secret wpstack_db_password
Creating secret wpstack_db_root_password
Creating service wpstack_db
Creating service wpstack_wordpress
root@host03:~# docker stack ls # 查看stack list
NAME SERVICES ORCHESTRATOR
wpstack 2 Swarm
root@host03:~# docker stack services wpstack # 查看某stack包含的service list
ID NAME MODE REPLICAS IMAGE PORTS
60gocweop7sy wpstack_wordpress replicated 1/1 wordpress:latest *:80->80/tcp
wpi8ieo8k89z wpstack_db replicated 0/1 mysql:5.7
root@host03:~# docker service ps wpstack_db # 查看 **_db service
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
lmpt206stwnl wpstack_db.1 mysql:5.7 host02 Running Running less than a second ago
root@host03:~# docker service ps wpstack_wordpress # 查看 **_wordpress service
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
q4zbhhs2nl7d wpstack_wordpress.1 wordpress:latest host02 Running Running 33 seconds ago
访问 http://10.12.31.211 验证部署结果
root@host03:~# sed -i 's/80:80/81:80/' wp.yaml # 修改wordpress对外端口号
root@host03:~# docker stack deploy -c wp.yaml wpstack # 更新stack 配置
Updating service wpstack_db (id: wpi8ieo8k89z46ahxuwbnu357)
Updating service wpstack_wordpress (id: 60gocweop7sytwa5eab7zffkf)
root@host03:~# docker stack ls
NAME SERVICES ORCHESTRATOR
wpstack 2 Swarm
root@host03:~# docker stack services wpstack
ID NAME MODE REPLICAS IMAGE PORTS
60gocweop7sy wpstack_wordpress replicated 1/1 wordpress:latest *:81->80/tcp
wpi8ieo8k89z wpstack_db replicated 1/1 mysql:5.7
root@host03:~# docker service ps wpstack_db
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
lmpt206stwnl wpstack_db.1 mysql:5.7 host02 Running Running 3 minutes ago
root@host03:~# docker service ps wpstack_wordpress # 可以看到新建并启动了一个副本
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
nqpzyhi353qt wpstack_wordpress.1 wordpress:latest host01 Running Running 45 seconds ago
q4zbhhs2nl7d \_ wpstack_wordpress.1 wordpress:latest host02 Shutdown Shutdown 47 seconds ago
访问 http://10.12.31.211 验证部署结果
root@host03:~# docker stack rm wpstack # 删除stack
Removing service wpstack_db
Removing service wpstack_wordpress
Removing secret wpstack_db_password
Removing secret wpstack_db_root_password
Removing network wpstack_default
如果想更新stack的某些属性,直接修改 YAML文件即可,然后再次执行上面的命令。