Docker Swarm集群

集群以公司服务器集群架构为例;

因为集群需要至少一台服务器为管理节点(manager),至少一台服务器为工作节点(worker)。所以至少购买2台服务器。可以部署一主多从或多主多从。

docker Swarm创建集群节点,执行以下命令创建manager节点。

  1. docker swarm init --advertise-addr 112.125.24.210
  2. Swarm initialized: current node (spjk8co05lnq3vsouv6fqri6u) is now a manager.
  3. To add a worker to this swarm, run the following command:
  4. docker swarm join --token SWMTKN-1-1ubgk142rpmalzsm96wg3w7b21k3u9lwxj87gfn9u8k7smo49j-a6cm1q7vv4sfxlepsaecy8t3c 112.125.24.210:2377
  5. To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

当然还有多种命令,可使用docker swarm init —help查看。

加入集群命令,可以先ping manager主机的ip地址,查看是否可以ping通,manager节点需要先开放2377默认的swarm集群端口。

  1. docker swarm join --token SWMTKN-1-1ubgk142rpmalzsm96wg3w7b21k3u9lwxj87gfn9u8k7smo49j-a6cm1q7vv4sfxlepsaecy8t3c 112.125.24.210:2377
  2. This node joined a swarm as a worker.

查看加入集群manager或worker节点的token。

  1. docker swarm join-token manager
  2. To add a manager to this swarm, run the following command:
  3. docker swarm join --token SWMTKN-1-1ubgk142rpmalzsm96wg3w7b21k3u9lwxj87gfn9u8k7smo49j-a1hp3lawivv9er6tohoxzo906 112.125.24.210:2377
  4. docker swarm join-token worker
  5. To add a worker to this swarm, run the following command:
  6. docker swarm join --token SWMTKN-1-1ubgk142rpmalzsm96wg3w7b21k3u9lwxj87gfn9u8k7smo49j-a6cm1q7vv4sfxlepsaecy8t3c 112.125.24.210:2377

查看集群的状态

  1. docker node ls
  2. ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
  3. spjk8co05lnq3vsouv6fqri6u * iZ2zeb53vb2leq0m0wz0zsZ Ready Active Leader 20.10.1
  4. tsmnxrw830undvwb1et8ky66w izbp157dewpls799nnspklz Ready Active 20.10.5

到此,swarm集群已经创建完毕,下面我们来部署服务。

上节提到添加容器镜像仓库,我们可以把自定义的镜像上传到私有仓库中,然后在下载下来

  1. docker pull registry.cn-hangzhou.aliyuncs.com/penk_registry/centos74-php72-nginx:app
  2. app: Pulling from penk_registry/centos74-php72-nginx
  3. 840caab23da4: Already exists
  4. c54b80530976: Pull complete
  5. 49433ff9c6bf: Pull complete
  6. bef63c3f3631: Pull complete
  7. 60234ef842d8: Pull complete
  8. 3c755d1063fa: Pull complete
  9. 4372a4b32a53: Pull complete
  10. 4a5a56e1f4a8: Pull complete
  11. e1a3e4f1764b: Pull complete
  12. df6a4263c5cf: Pull complete
  13. 3ba88e03c490: Pull complete
  14. 9ff4528721c7: Pull complete
  15. c0450946d577: Pull complete
  16. 020276fb403c: Pull complete
  17. ec0dad955e62: Pull complete
  18. 32b63ef3a126: Pull complete
  19. Digest: sha256:511021143282b06b9d13f8414bc89fa060338883f5ff7e0b5ca15e636dbbda78
  20. Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/penk_registry/centos74-php72-nginx:app
  21. registry.cn-hangzhou.aliyuncs.com/penk_registry/centos74-php72-nginx:app

创建服务命令,已app项目为例,集群的每台worker服务器上mount映射的目录必须存在。

  1. docker service create --replicas 2 --publish 8005:80 --name app --mount type=bind,source=/code/zkgj/kf/zkgj-app/,target=/usr/share/nginx/html/ registry.cn-hangzhou.aliyuncs.com/penk_registry/centos74-php72-nginx:app
  2. image registry.cn-hangzhou.aliyuncs.com/penk_registry/centos74-php72-nginx:app could not be accessed on a registry to record
  3. its digest. Each node will access registry.cn-hangzhou.aliyuncs.com/penk_registry/centos74-php72-nginx:app independently,
  4. possibly leading to different nodes running different
  5. versions of the image.
  6. riyg73yx14g038vkd2v2mdbns
  7. overall progress: 1 out of 2 tasks
  8. 1/2: invalid mount config for type "bind": bind source path does not exist: /co
  9. 2/2: running [==================================================>]
  10. ^COperation continuing in background.
  11. Use `docker service ps riyg73yx14g038vkd2v2mdbns` to check progress.

服务扩容、缩容命令

  1. docker service scale app=10

若镜像中需要添加或者修改东西,需要重新更新镜像,这里更新镜像是平滑,无感知的更新。

  1. docker service update --image registry.cn-hangzhou.aliyuncs.com/penk_registry/centos74-php54-nginx:app app