ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Docker七 | 搭建Swarm集群

2026/9/18 12:20:33 拓冰建站 浏览量
Docker七 | 搭建Swarm集群

目录

创建Swarm集群

创建管理节点 

增加工作节点

 查看集群

部署服务

新建服务 

 查看服务

服务伸缩

增加服务 

 减少服务

删除服务


创建Swarm集群

创建管理节点 

在192.168.117.131下执行docker swarm init命令的节点自动成为管理节点

[root@localhost ~]# docker swarm init --advertise-addr 192.168.117.131
Swarm initialized: current node (mn0xmlobseurosjh9ylsex0uq) is now a manager.To add a worker to this swarm, run the following command:docker swarm join --token SWMTKN-1-3x9heaku0p7o99e56rwng3opbvtgbrtfnpavdewbrpgu8joojn-cjv34fhoz2aaqeewe6ga0ceof 192.168.117.131:2377To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

增加工作节点

在192.168.117.130和192.168.117.132下执行如下命令使之成为工作节点加入到集群中

docker swarm join --token SWMTKN-1-3x9heaku0p7o99e56rwng3opbvtgbrtfnpavdewbrpgu8joojn-cjv34fhoz2aaqeewe6ga0ceof 192.168.117.131:2377

成功会有如下反应

 查看集群

在管理节点执行如下命令

[root@localhost ~]# docker node ls
ID                            HOSTNAME                STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
mn0xmlobseurosjh9ylsex0uq *   localhost.localdomain   Ready     Active         Leader           24.0.7
pifvf9mr1ggvez9o92067gv60     localhost.localdomain   Ready     Active                          24.0.6
xdedgykahub32jyh09yeupbim     localhost.localdomain   Ready     Active                          24.0.6

部署服务

新建服务 

 在创建的Swarm集群中运行nginx服务

[root@localhost ~]# docker service create --replicas 3 -p 80:80 --name nginx nginx:1.13.7-alpine
hzb8ckdfjjrq7lmrgplhclblm
overall progress: 3 out of 3 tasks 
1/3: running   
2/3: running   
3/3: running   
verify: Service converged 

发现三种ip节点都可以访问nginx

 查看服务

 查看当前Swarm集群运行的服务

[root@localhost ~]# docker service ls
ID             NAME      MODE         REPLICAS   IMAGE                 PORTS
hzb8ckdfjjrq   nginx     replicated   3/3        nginx:1.13.7-alpine   *:80->80/tcp

查看某个服务的详情

[root@localhost ~]# docker service ps nginx 
ID             NAME      IMAGE                 NODE                    DESIRED STATE   CURRENT STATE            ERROR     PORTS
s18jve694hof   nginx.1   nginx:1.13.7-alpine   localhost.localdomain   Running         Running 15 minutes ago             
kukdiwkwzbk0   nginx.2   nginx:1.13.7-alpine   localhost.localdomain   Running         Running 15 minutes ago             
dutte36gcuqq   nginx.3   nginx:1.13.7-alpine   localhost.localdomain   Running         Running 15 minutes ago  

查看某个服务的日志

[root@localhost ~]# docker service logs nginx

服务伸缩

增加服务 

[root@localhost ~]# docker service scale nginx=5
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@localhost ~]# 

 减少服务

[root@localhost ~]# docker service scale nginx=2
nginx scaled to 2
overall progress: 2 out of 2 tasks 
1/2: running   
2/2: running   
verify: Service converged 
[root@localhost ~]# 

删除服务

[root@localhost ~]# docker service rm nginx
nginx