Docker Swarm集群
集群以公司服务器集群架构为例;
因为集群需要至少一台服务器为管理节点(manager),至少一台服务器为工作节点(worker)。所以至少购买2台服务器。可以部署一主多从或多主多从。
docker Swarm创建集群节点,执行以下命令创建manager节点。
docker swarm init --advertise-addr 112.125.24.210Swarm initialized: current node (spjk8co05lnq3vsouv6fqri6u) is now a manager.To add a worker to this swarm, run the following command:docker swarm join --token SWMTKN-1-1ubgk142rpmalzsm96wg3w7b21k3u9lwxj87gfn9u8k7smo49j-a6cm1q7vv4sfxlepsaecy8t3c 112.125.24.210:2377To 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集群端口。
docker swarm join --token SWMTKN-1-1ubgk142rpmalzsm96wg3w7b21k3u9lwxj87gfn9u8k7smo49j-a6cm1q7vv4sfxlepsaecy8t3c 112.125.24.210:2377This node joined a swarm as a worker.
查看加入集群manager或worker节点的token。
docker swarm join-token managerTo add a manager to this swarm, run the following command:docker swarm join --token SWMTKN-1-1ubgk142rpmalzsm96wg3w7b21k3u9lwxj87gfn9u8k7smo49j-a1hp3lawivv9er6tohoxzo906 112.125.24.210:2377docker swarm join-token workerTo add a worker to this swarm, run the following command:docker swarm join --token SWMTKN-1-1ubgk142rpmalzsm96wg3w7b21k3u9lwxj87gfn9u8k7smo49j-a6cm1q7vv4sfxlepsaecy8t3c 112.125.24.210:2377
查看集群的状态
docker node lsID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSIONspjk8co05lnq3vsouv6fqri6u * iZ2zeb53vb2leq0m0wz0zsZ Ready Active Leader 20.10.1tsmnxrw830undvwb1et8ky66w izbp157dewpls799nnspklz Ready Active 20.10.5
到此,swarm集群已经创建完毕,下面我们来部署服务。
上节提到添加容器镜像仓库,我们可以把自定义的镜像上传到私有仓库中,然后在下载下来
docker pull registry.cn-hangzhou.aliyuncs.com/penk_registry/centos74-php72-nginx:appapp: Pulling from penk_registry/centos74-php72-nginx840caab23da4: Already existsc54b80530976: Pull complete49433ff9c6bf: Pull completebef63c3f3631: Pull complete60234ef842d8: Pull complete3c755d1063fa: Pull complete4372a4b32a53: Pull complete4a5a56e1f4a8: Pull completee1a3e4f1764b: Pull completedf6a4263c5cf: Pull complete3ba88e03c490: Pull complete9ff4528721c7: Pull completec0450946d577: Pull complete020276fb403c: Pull completeec0dad955e62: Pull complete32b63ef3a126: Pull completeDigest: sha256:511021143282b06b9d13f8414bc89fa060338883f5ff7e0b5ca15e636dbbda78Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/penk_registry/centos74-php72-nginx:appregistry.cn-hangzhou.aliyuncs.com/penk_registry/centos74-php72-nginx:app
创建服务命令,已app项目为例,集群的每台worker服务器上mount映射的目录必须存在。
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:appimage registry.cn-hangzhou.aliyuncs.com/penk_registry/centos74-php72-nginx:app could not be accessed on a registry to recordits digest. Each node will access registry.cn-hangzhou.aliyuncs.com/penk_registry/centos74-php72-nginx:app independently,possibly leading to different nodes running differentversions of the image.riyg73yx14g038vkd2v2mdbnsoverall progress: 1 out of 2 tasks1/2: invalid mount config for type "bind": bind source path does not exist: /co…2/2: running [==================================================>]^COperation continuing in background.Use `docker service ps riyg73yx14g038vkd2v2mdbns` to check progress.
服务扩容、缩容命令
docker service scale app=10
若镜像中需要添加或者修改东西,需要重新更新镜像,这里更新镜像是平滑,无感知的更新。
docker service update --image registry.cn-hangzhou.aliyuncs.com/penk_registry/centos74-php54-nginx:app app
