(五) docker-swarm 集群管理工具

swarm deamon 只是一个调度器加路由器,swarm自己不运行容器,它只是接受docker客户端发送过来的请求,调度适合的节点来运行容器。

DOCKER管理工具-SWARM部署记录

部署步骤

manager初始化

1
docker swarm init --advertise-addr 47.99.236.50

加入节点

1
2
3
4
5
# 作为worker加入到swarm
docker swarm join --token SWMTKN-1-3xt1c2h1aek21zlh15nq1fg29o2i8yhxjsw8j59f9ngddpvihf-d0tb91hx2krdysuwfepv8dny9 47.99.236.50:2377

# 作为manager加入到swarm
docker swarm join-token manager

查看节点

1
docker node ls

节点升降级

1
2
将节点升级为manager:docker node promote 节点名
将节点降级为worker: docker node demote 节点名

端口开放

1
2
3
2377 集群管理端口
7946 tcp/udp端口 节点间通讯
4789 用于覆盖网络流量

操作记录

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
[root@manager ~]# docker swarm init --advertise-addr 47.99.236.50
Swarm initialized: current node (038olxese1szrj1f0am0mi0ab) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join --token SWMTKN-1-3xt1c2h1aek21zlh15nq1fg29o2i8yhxjsw8j59f9ngddpvihf-d0tb91hx2krdysuwfepv8dny9 47.99.236.50:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

[root@manager ~]# docker swarm join-token worker
To add a worker to this swarm, run the following command:

docker swarm join --token SWMTKN-1-3xt1c2h1aek21zlh15nq1fg29o2i8yhxjsw8j59f9ngddpvihf-d0tb91hx2krdysuwfepv8dny9 47.99.236.50:2377

[root@manager ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
038olxese1szrj1f0am0mi0ab * manager Ready Active Leader 19.03.5


[root@worker1 ~]# docker swarm join --token SWMTKN-1-3xt1c2h1aek21zlh15nq1fg29o2i8yhxjsw8j59f9ngddpvihf-d0tb91hx2krdysuwfepv8dny9 47.99.236.50:2377
This node joined a swarm as a worker.

[root@manager ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
038olxese1szrj1f0am0mi0ab * manager Ready Active Leader 19.03.5
k34cve7sd9au2zu5a4phwvokv worker1 Ready Active

服务管理

创建服务

1
2
3
docker service create --replicas 5  -p 8080:80 --name nginx  nginx

--replicas 服务数量

更新服务

1
docker service update --image nginx:alpine nginx

删除服务

1
docker service rm nginx

服务实例

1
2
docker service scale nginx=0
docker service scale nginx=5

查看服务容器状态

1
docker service ps nginx

查看服务的详细信息

1
docker service inspect nginx

docker swarm 默认就实现了轮询的负载均衡

1
2
3
# 进入swarm节点机器配置
docker config create redis.conf redis.conf
配置文件名称 文件地址

网络共享问题

1
docker network create --driver=overlay myOverly

docker-compose.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
version: "3.6"
services:
redis-master1:
image: redis:3.2.2
ports:
- "6397:6397"
- "16397:16397"
stdin_open: true
deploy:
mode: replicated
replicas: 2
networks:
- cluster
tty: true
configs:
- source: redis.conf
target: /root/redis.conf
privileged: true
volumes: ["/root/config:/data"]
configs:
redis.conf:
external: true
networks:
cluster:

1
2
# 进入swarm节点机器配置
docker stack deploy -c docker-compose.yaml redis-cluster

滚动更新

1
2
3
4
docker service update --update-parallelism 2  --image demo:2.0 --update-delay 10s test

--update-parallelism 参数用来指定最大同步更新任务数。
--update-delay 两次更新的间隔,单位默认为秒

可以测试更新镜像版本的时候客户端并不会断开连接,服务平滑升级,即服务不停机更新,客户端无感知。