准备四台1c1g服务器/虚拟机(建议同一内网)
安装好docker、docker-compose
swarm官方文档:https://docs.docker.com/engine/swarm/how-swarm-mode-works/nodes/
布置节点:2主2从
初始化一个swarm集群
docker swarm -init
现在docker swarm初始化完成,这台服务器是当前swarm集群的leader、同时也是管理节点(manager);
如果想添加工作节点到当前集群,在目标服务器使用之前生成的命令即可;
回到管理节点查看节点列表
docker node ls
生成新的token,添加新的管理节点
# 生成管理节点token(manager)
docker swarm join-token manager
# 生成工作节点token(worker)
docker swarm join-token worker
我这里配了1主3从,现在通过更改角色来实现2主2从
# docker node update --help
Usage: docker node update [OPTIONS] NODE
Update a node
Options:
--availability string Availability of the node ("active"|"pause"|"drain")
--label-add list Add or update a node label (key=value)
--label-rm list Remove a node label if exists
--role string Role of the node ("worker"|"manager")
删除节点
# 在目标机器上输入命令(如果是管理节点离开,需要命令后面加 '--force')
docker swarm leave
回到管理节点,查看节点状态有一个显示离开;
使用docker node rm 命令移除节点
# docker node --help
Usage: docker node COMMAND
Manage Swarm nodes
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
Run 'docker node COMMAND --help' for more information on a command.
补充:如果你想自定义一个swarm网络
docker network create -d overlay --attachable swarm-overlay
切记一定要带上--attachable参数
否则会出现奇怪的报错
ERROR: for redis_redis_1 Cannot start service redis: Could not attach to network swarm-overlay: rpc error: code = PermissionDenied desc = network swarm-overlay not manually attachable
ERROR: for redis Cannot start service redis: Could not attach to network swarm-overlay: rpc error: code = PermissionDenied desc = network swarm-overlay not manually attachable
ERROR: Encountered errors while bringing up the project.