# 创建集群,主机有多块网卡必须使用127.0.0.1本地回环地址创建
# 创建节点默认为leader节点(管理manager的节点),可以使用其他节点加入集群为manager节点或work节点
# 可以有多个manager和work节点,但只有一个leader节点
[root@cui ~]#docker swarm init --advertise-addr 127.0.0.1
Swarm initialized: current node (6ualq8a4l4txwj9zojinjz2tc) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-37b9oos4pxouhrhavocf6gbrvvv03uvicigxfjf7u4d8lrt1bi-2scnrxs6uyeqvadc0j5trm9hb 127.0.0.1:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
# 查看集群信息
[root@cui ~]#docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
6ualq8a4l4txwj9zojinjz2tc * cui Ready Active Leader 24.0.7
# 新建服务
# dockeer service管理swarm集群中的服务
[root@cui ~]#docker service create --replicas 3 -p 8081:80 --name nginx nginx:1.21
z6c2tdtxm0hxt4gbrxebpib5b
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
# 查看服务
[root@cui ~]#docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
z6c2tdtxm0hx nginx replicated 3/3 nginx:1.21 *:8081->80/tcp
# 查看某个服务的详细信息
[root@cui ~]#docker service ps nginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
fe3uyqbmi18k nginx.1 nginx:1.21 cui Running Running 7 minutes ago
x1hb7s92git9 nginx.2 nginx:1.21 cui Running Running 7 minutes ago
zkdsua2ktr0m nginx.3 nginx:1.21 cui Running Running 7 minutes ago
# 查看某个服务的日志
[root@cui ~]#docker logs nginx.1.fe3uyqbmi18kab2mcy5tcy9yz
# 服务伸缩
[root@cui ~]#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
# 删除服务
docker service rm nginx
Docker swarm集群创建及基础命令
最新推荐文章于 2024-09-02 14:26:55 发布