1、部署环境
服务器名称 | IP地址 | 备注 |
node01 | 10.32.161.124 | Manager |
node02 | 10.32.161.125 | Work |
node03 | 10.32.161.126 | Work |
2、Docker CE安装
1 | 参考:https: //www .cnblogs.com /a120608yby/p/9883175 .html |
3、更改所有节点配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # 修改配置并重启docker # vi /etc/docker/daemon.json { "live-restore" : false } # systemctl restart docker # 防火墙配置 firewall-cmd --add-port={2377 /tcp ,7946 /tcp ,7946 /udp ,4789 /udp } firewall-cmd --runtime-to-permanent # 安装命令补全 dnf install bash -completion -y |
4、在Manager节点上初始化
1 2 3 4 5 6 7 8 | # docker swarm init Swarm initialized: current node (bfa2c8gfcxgmb60uq4e0j9zgp) is now a manager. To add a worker to this swarm, run the following command : docker swarm join --token SWMTKN-1-5yz0ag8l4mqj1gw20kl6fn4j38gj9rs4oefi6pgyuz40wsjotp-1tpgymi77j5idqanazqbpsmoj 10.32.161.124:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions. |
5、将Work节点加入集群
1 2 3 | # docker swarm join \ --token SWMTKN-1-5yz0ag8l4mqj1gw20kl6fn4j38gj9rs4oefi6pgyuz40wsjotp-1tpgymi77j5idqanazqbpsmoj 10.32.161.124:2377 This node joined a swarm as a worker. |
6、在Manager节点上验证集群节点情况
1 2 3 4 5 | # docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION bfa2c8gfcxgmb60uq4e0j9zgp * node01 Ready Active Leader 24.0.7 c6zanp6akzdpzefiyudynajcc node02 Ready Active 24.0.7 yuw9tg4ufwmtslanebshyg7zb node03 Ready Active 24.0.7 |
7、在Manager节点上创建镜像和服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | # 创建服务 # docker service create --name swarm_cluster --replicas=2 -p 80:80 nginx:stable-alpine ue6t4lnrn9s5udcowue6v3oe5 overall progress: 2 out of 2 tasks 1 /2 : running [==================================================>] 2 /2 : running [==================================================>] verify: Service converged # 查看服务 # docker service ls ID NAME MODE REPLICAS IMAGE PORTS ue6t4lnrn9s5 swarm_cluster replicated 2 /2 nginx:stable-alpine *:80->80 /tcp # docker service inspect swarm_cluster --pretty ID: ue6t4lnrn9s5udcowue6v3oe5 Name: swarm_cluster Service Mode: Replicated Replicas: 2 Placement: UpdateConfig: Parallelism: 1 On failure: pause Monitoring Period: 5s Max failure ratio: 0 Update order: stop-first RollbackConfig: Parallelism: 1 On failure: pause Monitoring Period: 5s Max failure ratio: 0 Rollback order: stop-first ContainerSpec: Image: nginx:stable-alpine@sha256:089520833b93077841d3cdc7ab1f7b817de73c7e10070b71b85fa97da7623dbe Init: false Resources: Endpoint Mode: vip Ports: PublishedPort = 80 Protocol = tcp TargetPort = 80 PublishMode = ingress # docker service ps swarm_cluster ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS p8ik8k4eijfk swarm_cluster.1 nginx:stable-alpine node02 Running Running 2 minutes ago l6qosoijkpzw swarm_cluster.2 nginx:stable-alpine node03 Running Running 2 minutes ago # 访问服务 # for i in {124..126}; do curl -I 10.32.161.$i; done HTTP /1 .1 200 OK Server: nginx /1 .24.0 Date: Thu, 11 Jan 2024 02:47:49 GMT Content-Type: text /html Content-Length: 615 Last-Modified: Tue, 11 Apr 2023 17:21:57 GMT Connection: keep-alive ETag: "64359735-267" Accept-Ranges: bytes HTTP /1 .1 200 OK Server: nginx /1 .24.0 Date: Thu, 11 Jan 2024 02:47:49 GMT Content-Type: text /html Content-Length: 615 Last-Modified: Tue, 11 Apr 2023 17:21:57 GMT Connection: keep-alive ETag: "64359735-267" Accept-Ranges: bytes HTTP /1 .1 200 OK Server: nginx /1 .24.0 Date: Thu, 11 Jan 2024 02:47:49 GMT Content-Type: text /html Content-Length: 615 Last-Modified: Tue, 11 Apr 2023 17:21:57 GMT Connection: keep-alive ETag: "64359735-267" Accept-Ranges: bytes # 服务伸缩 # docker service scale swarm_cluster=3 swarm_cluster scaled to 3 overall progress: 3 out of 3 tasks 1 /3 : running [==================================================>] 2 /3 : running [==================================================>] 3 /3 : running [==================================================>] verify: Service converged # docker service ps swarm_cluster ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS p8ik8k4eijfk swarm_cluster.1 nginx:stable-alpine node02 Running Running 5 minutes ago l6qosoijkpzw swarm_cluster.2 nginx:stable-alpine node03 Running Running 5 minutes ago u8yxsvmn8up7 swarm_cluster.3 nginx:stable-alpine node01 Running Running 9 seconds ago |
8、Portainer CE 安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # 下载部署文件 curl -L https: //downloads .portainer.io /ce2-19/portainer-agent-stack .yml -o portainer-agent-stack.yml # 部署portainer # docker stack deploy -c portainer-agent-stack.yml portainer Creating network portainer_agent_network Creating service portainer_agent Creating service portainer_portainer # docker stack ps portainer ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS ppo2yz1g5jjx portainer_agent.bfa2c8gfcxgmb60uq4e0j9zgp portainer /agent :2.19.4 node01 Running Running 2 minutes ago 1puaw3gom6ts portainer_agent.c6zanp6akzdpzefiyudynajcc portainer /agent :2.19.4 node02 Running Running 2 minutes ago e5f04449exnr portainer_agent.yuw9tg4ufwmtslanebshyg7zb portainer /agent :2.19.4 node03 Running Running 2 minutes ago 2i6ae0pwrhbv portainer_portainer.1 portainer /portainer-ce :2.19.4 node01 Running Running 2 minutes ago |
Portainer访问
1 2 | 访问地址:http: //Manager 地址:9000/ 默认账号:admin |
参考:
1 2 | https: //docs .docker.com /engine/swarm/swarm-tutorial/create-swarm/ https: //docs .portainer.io /start/install-ce/server/swarm/linux |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述