1. 主旨介绍
使用mac和fusion vm两台主机搭建一个小的docker集群, 采用docker swarm方式搭建. Swarm基本概念:
- manager:用来管理swarm服务
- node: docker enginer节点, 通过manager可以在node上操作, manager本身也是node
- worker: 非管理的node, 一般是实际部署和运行container的docker节点
目的是是探索使用swarm完成动态伸缩的可行性.
2. 操作步骤
-
swarm init(vm中操作,因为docker for mac有网络问题 manager)
docker swarm init --advertise-addr 192.168.132.148 Swarm initialized: current node (b1uem2hg4y2ojnauvre3x2rdw) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-1snrbxsenp4v1npcusox9l3n9jzsa4n8jbkvlhjc19e3z89c8a-7vfrbr1o1e1sr2g2r6f97xhne 192.168.132.148:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
初始化swarm之后可以使用docker node ls查看信息:
docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION b1uem2hg4y2ojnauvre3x2rdw * linuxkit-025000000001 Ready Active Leader 18.06.1-ce 18.06.1-ce
-
join node(worker)
docker swarm join --token SWMTKN-1-3h1rzm2cdu6ms935f6kysy4rhzs54mqofnhr1thlwu0fiewqfq-9q6fwc7eqk70zwb1kwi2vdklj 192.168.132.148:2377 This node joined a swarm as a worker.
-
deploy service 部署一个alpine容器, 运行ping命令视为内部服务
docker service create --replicas 1 --name helloword alpine ping docker.com vyw5nezmiv37j3fk5kgu4231x overall progress: 1 out of 1 tasks 1/1: running [==================================================>]
查看结果如下, 可以发现一个alpine被部署到worker节点(vm是manager机器名是unbuntu所以下面的linuxkit-xxxx是worker, 即mac server)
docker service ps helloword ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS ht9npghzzux9 helloword.1 alpine:latest linuxkit-025000000001 Running Running 4 minutes ago
-
scale service 横向扩展服务为2个实例
docker service scale helloword=2 helloword scaled to 2 overall progress: 1 out of 2 tasks 1/2: running [==================================================>] overall progress: 2 out of 2 tasks 1/2: running [==================================================>] 2/2: running [==================================================>] verify: Service converged
查看扩展的结果如下, 可以发现两个实例被部署到worker和manager, 因为worker不够用, 所以使用了manager, 也可以设置manager只是管理, 不运行task
docker service ps helloword ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS ht9npghzzux9 helloword.1 alpine:latest linuxkit-025000000001 Running Running 5 minutes ago v0i6ghtx8eed helloword.2 alpine:latest ubuntu Running Running 26 seconds ago
3. 总结
使用swarm集群可以优雅的进行docker部署和扩展, 结合docker-compose操作会更加方便, 如果增加了产线的流量监控+服务发现, 就可以实现产线环境的动态伸缩部署, 很棒!!
4. 问题和解答
-
unavailable desc = all SubConns are in TransitentFailure
这个问题以为manager是mac, docker for mac 因为网络的问题不能支持work去链接到mac docker swarm manager
-
Error response from daemon: manager stopped: failed to listen on remote API address: listen tcp 10.0.1.34:2377: bind: cannot assign requested address
这是因为指定了 --listen-addr和--advertise-addr ip一样但是端口默认也是一直的, 去除listen-addr或者指定不同的端口即可
-
结束swarm
在manager服务器运行: docker swarm leave --force, 这样manager就变成inactive了, 可以重新操作执行init或者其他
5. 参考资料
- Getting started with swarm mode
- Add nodes to the swarm
- Deploy a service to the swarm
- Scale the service in the swarm
来源:oschina
链接:https://my.oschina.net/u/2623130/blog/2872189