swarm 可以简单理解为简单版k8s,采用管理节点+工作节点的方式
初始化一个swarm集群
[root@localhost ~]# docker swarm init --advertise-addr 192.168.106.131
Swarm initialized: current node (uuak4q55jejkbj654yhry6vvh) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-0itq4kr7g74p1lk90syomkp7duqg040eli9stpuxscno1q8ben-a88didzcmnbfzi5pi020bjzia 192.168.106.131:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
生成增加manager节点的命令
[root@localhost ~]# docker swarm join-token manager
To add a manager to this swarm, run the following command:
docker swarm join --token SWMTKN-1-0itq4kr7g74p1lk90syomkp7duqg040eli9stpuxscno1q8ben-3l2s23nmszm5u8cvdi9kql9eo 192.168.106.131:2377
[root@localhost ~]#
生成增加work节点的命令
[root@localhost ~]# docker swarm join-token worker
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-0itq4kr7g74p1lk90syomkp7duqg040eli9stpuxscno1q8ben-a88didzcmnbfzi5pi020bjzia 192.168.106.131:2377
[root@localhost ~]#
然后在需要加入集群的机器执行上述对应命令即可
查看当前swarm集群节点状态
[root@localhost ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
uuak4q55jejkbj654yhry6vvh * localhost.localdomain Ready Active Leader 20.10.7
[root@localhost ~]#
双主双从集群时:节点宕机时,如果只存在一个manager节点,则整个集群不可用
需大多数节点存活,整个集群才可用,节点数 >= 3;
节点离开集群
如果离开的是manager节点,会有警告,可使用 -f 强制离开
docker swarm leave
Swarm 部署应用,动态扩缩容
docker启动应用方式
服务:集群提供一个服务;可以是一个容器,也可以是一组相同的容器
集群中节点的分布:可能在不同机器上,也可能在一个机器上的不同容器
docker run -> 启动一个容器,不具有扩缩容
docker service create -> 启动一个服务,可以是一个容器,也可以是多个容器,具有扩缩容特性;即集群的概念
docker service
[root@swarm01 ~]# docker service create --help
Usage: docker service create [OPTIONS] IMAGE [COMMAND] [ARG...]
Create a new service
eg: docker service create --name my-nginx -p 18080:80 nginx
在当前节点启动容器,不一定会分布到当前节点,可能是集群中的任何节点
[root@swarm01 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
pl07nc4j4jpp my-nginx replicated 1/1 nginx:latest *:18080->80/tcp
[root@swarm01 ~]# docker service ps my-nginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
navzuz169o9g my-nginx.1 nginx:latest swarm03 Running Running 2 minutes ago
docker service update [OPTIONS] SERVICE
扩容为3个副本(原有那个不变,新增两个)
[root@swarm01 ~]# docker service update --replicas 3 my-nginx
my-nginx
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged
查看扩容后的状态
[root@swarm01 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
pl07nc4j4jpp my-nginx replicated 3/3 nginx:latest *:18080->80/tcp
[root@swarm01 ~]# docker service ps my-nginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
navzuz169o9g my-nginx.1 nginx:latest swarm03 Running Running about an hour ago
ganoswo1p7tq my-nginx.2 nginx:latest swarm04 Running Running about a minute ago
mt0gjzwqa7o5 my-nginx.3 nginx:latest swarm02 Running Running about a minute ago
可以看到,扩容之后,新增的两个节点分布在02 04 上面,此时通过集群内所有节点都可以访问 my-nginx 服务。因为我们访问的是集群,而集群内部的路由转发这些都是由docker-swarm 去完成的;
看到这里,对于普通的 docker run ,可能会存在 端口冲突的问题,但是这里,采用的是以服务方式启动,单个容器是不会暴露服务端口的,端口是由docker swarm 集群去暴露的,也就不存在端口冲突的问题
此时的容器是这样的
[root@swarm01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
943072cfc92d nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp my-nginx.6.8qu4v48614zb9h00koi45jb9b
58ce7df6d2d7 nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp my-nginx.5.w73pj7s2ad9bu8n9qdft9ofj1
扩容和缩容是一个相对的概念,docker service update --replicas 增加/减少都可以
docker service --help
docker service scale --help
Usage: docker service scale SERVICE=REPLICAS [SERVICE=REPLICAS...]
Scale one or multiple replicated services
Options:
-d, --detach Exit immediately instead of waiting for the service to converge
eg:设置副本数为 5
[root@swarm01 ~]# docker service scale my-nginx=5
my-nginx scaled to 5
overall progress: 5 out of 5 tasks
1/5: running [==================================================>]
2/5: running [==================================================>]
3/5: running [==================================================>]
4/5: running [==================================================>]
5/5: running [==================================================>]
verify: Service converged
集群内任意节点都可以访问服务,服务副本可以动态扩缩容,非常方便,达到高可用
[root@swarm01 ~]# docker service rm my-nginx
my-nginx
移除后整个集群内服务下的所有容器及整个服务全部移除
[root@swarm01 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
[root@swarm01 ~]#
[root@swarm01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@swarm01 ~]#
Docker Swarm 基本使用
搭建集群;启停服务;动态扩缩容