1、部署环境
| 服务器名称 | IP地址 | 备注 |
| node01 | 10.32.161.124 | Manager |
| node02 | 10.32.161.125 | Work |
| node03 | 10.32.161.126 | Work |
2、Docker CE安装
参考:https://www.cnblogs.com/a120608yby/p/9883175.html
3、更改所有节点配置
# 修改配置并重启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节点上初始化
# 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节点加入集群
# docker swarm join \ --token SWMTKN-1-5yz0ag8l4mqj1gw20kl6fn4j38gj9rs4oefi6pgyuz40wsjotp-1tpgymi77j5idqanazqbpsmoj 10.32.161.124:2377 This node joined a swarm as a worker.
6、在Manager节点上验证集群节点情况
# 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节点上创建镜像和服务
# 创建服务
# 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 安装
# 下载部署文件 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访问
访问地址:http://Manager地址:9000/ 默认账号:admin







参考:
https://docs.docker.com/engine/swarm/swarm-tutorial/create-swarm/ https://docs.portainer.io/start/install-ce/server/swarm/linux
浙公网安备 33010602011771号