docker环境自行安装
常用命令:
#管理swarm
# docker swarm
Commands:
init Initialize a swarm
join Join a swarm as a node and/or manager
join-token Manage join tokens
leave Leave the swarm
unlock Unlock swarm
unlock-key Manage the unlock key
update Update the swarm
#管理 node 节点
#docker node
Commands:
demote Demote one or more nodes from manager in the swarm
inspect Display detailed information on one or more nodes
ls List nodes in the swarm
promote Promote one or more nodes to manager in the swarm
ps List tasks running on one or more nodes, defaults to current node
rm Remove one or more nodes from the swarm
update Update a node
#管理service
#docker service
Commands:
create Create a new service
inspect Display detailed information on one or more services
logs Fetch the logs of a service or task
ls List services
ps List the tasks of one or more services
rm Remove one or more services
scale Scale one or multiple replicated services
update Update a service
#部署
#docker stack
Commands:
deploy Deploy a new stack or update an existing stack
ls List stacks
ps List the tasks in the stack
rm Remove one or more stacks
services List the services in the stack
1.初始化master节点 docker swarm init 记住加入的token,然后再 节点上面加入集群,忘记token可以以下命令获取
docker swarm join-token manager
docker swarm join-token worker
2.测试实验
docker pull linuxserver/nginx
docker service create --name webapp-nginx --replicas 3 --publish 80:80 linuxserver/nginx
#收缩实例个数为1个
docker service scale webapp-nginx=1
#将master节点排除,不运行任务
docker node update --availability drain hostname
#滚动升级images
docker service update --image nginx webapp-nginx
#服务更新和回滚策略
docker service create \
--name webapp-nginx \
--replicas 3 \
--update-delay 10s \
--update-parallelism 2 \
--update-failure-action continue \
--rollback-parallelism 2 \
--rollback-monitor 20s \
--rollback-max-failure-ratio 0.2 \
nginx:1.12.1
# --update-parallelism 2 : 每次允许两个服务一起更新
# --update-parallelism continue : 更新失败后的动作是继续
# --rollback-parallelism 2 : 回滚时允许两个一起
# --update-monitor 20s :回滚监控时间20s
# --update-max-failure-ratio 0.2 : 回滚失败率20%
#手动设置一下
docker service update --rollback-max-failure-ratio 0.2 webapp-nginx
#手动回滚一下
##默认只能回滚到上一次操作的状态,并不能连续回滚到指定操作
docker service update --rollback webapp-nginx