鸣谢:本文作为日志记录,其中参考了https://blog.csdn.net/candcplusplus/article/details/53836703?utm_source=blogxgwz3 在内的若干博文,再次感谢!
介绍
Swarm 作为Docker 三剑客之一,于2014年提出,自 v1.12.3 以来, docker swarm 成为 docker 引擎内置的服务。它和谷歌的 K8s 产生了直接竞争。很难说哪个好,对于熟悉 docker 容器的人来说,docker swarm 应该友好一些。
安装
准备材料:
1、硬件:centos 7 2台(可以是虚拟机)
2、docker 版本: 172.16.0.45 是 18.06.0-ce
172.16.0.43 是 1.13.1
3、yum 源 :阿里源
部署集群:
1、在master上执行docker pull swarm 拉取swarm镜像
2、初始化swarm : docker swarm init --advertise-addr 172.16.0.45
[root@k8smaster /]# docker swarm init --advertise-addr 172.16.0.45
Swarm initialized: current node (npb3iydf14kkts0b5ie66k2vn) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-4j7abdlcx8seeia13e609hy536gu0vazt3gz1tv7nao0072wwl-3igsdtz5niaoxvt6ysmskt031 172.16.0.45:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
注: docker swarm join --token SWMTKN-1-4j7abdlcx8seeia13e609hy536gu0vazt3gz1tv7nao0072wwl-3igsdtz5niaoxvt6ysmskt031 172.16.0.45:2377 为加入该集群的服务需要执行的tokenId
3、在node 上执行docker pull swarm 拉取swarm镜像
[root@pb_hdp_03 home]# docker pull swarm
Using default tag: latest
Trying to pull repository docker.io/library/swarm ...
sha256:406022f04a3d0c5ce4dbdb60422f24052c20ab7e6d41ebe5723aa649c3833975: Pulling from docker.io/library/swarm
d85c18077b82: Pull complete
1e6bb16f8cb1: Pull complete
85bac13497d7: Pull complete
Digest: sha256:406022f04a3d0c5ce4dbdb60422f24052c20ab7e6d41ebe5723aa649c3833975
Status: Downloaded newer image for docker.io/swarm:latest
4、在node 上执行 docker swarm join --token SWMTKN-1-4j7abdlcx8seeia13e609hy536gu0vazt3gz1tv7nao0072wwl-3igsdtz5niaoxvt6ysmskt031 172.16.0.45:2377 即可加入45上创建的集群
[root@pb_hdp_03 home]# docker swarm join --token SWMTKN-1-4j7abdlcx8seeia13e609hy536gu0vazt3gz1tv7nao0072wwl-3igsdtz5niaoxvt6ysmskt031 172.16.0.45:2377
This node joined a swarm as a worker.
5、如果返回This node joined a swarm as a worker. 标识成功加入集群
6、如果返回Error response from daemon: rpc error: code = 14 desc = grpc: the connection is unavailable 考虑防火墙问题
7、采用firewall-cmd --state 查询防火墙(如果返回not running 标识没有运行)
如果要采用防火墙那么需要通过开发下拉端口
firewall-cmd --add-port=2376/tcp --permanent
firewall-cmd --add-port=2377/tcp --permanent
firewall-cmd --add-port=7946/tcp --permanent
firewall-cmd --add-port=7946/udp --permanent
firewall-cmd --add-port=4789/udp --permanent
切记需要重启docker 才能生效。
8、在master 上执行docker node ls 来查看集群中的节点
[root@k8smaster /]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
npb3iydf14kkts0b5ie66k2vn * k8smaster Ready Active Leader 18.06.0-ce
qn9ewaj1kxn9g6wclylsow1vi pb_hdp_03 Ready Active 1.13.1
输出信息第二行 id 后面的*
表示当前连接到了该节点。HOSTNAME
栏输出节点的 hostname。MANAGER
用于指示 swarm
中的管理节点,该栏值为 Leader
表示为管理节点,空值表示为工作节点
9、在master 上执行docker node ps 查看当前节点容器信息(类似于docker ps)
[root@k8smaster /]# docker node ps
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
tf6rgcdtg9lz helloworld.1 busybox:1.25.1-musl k8smaster Running Running about an hour ago
sx52pbyed2jg helloworld.4 busybox:1.25.1-musl k8smaster Running Running about an hour ago
测试集群:
1、在master上通过“docker service create --replicas 1 --name helloworld busybox:1.25.1-musl /bin/sh -c "while true; do echo hello world; sleep 3; done"”来创建一个服务,每隔三秒输出一个 “hello world”
[root@k8smaster /]# docker service create --replicas 1 --name helloworld busybox:1.25.1-musl /bin/sh -c "while true; do echo hello world; sleep 3; done"
edkbe2lic2ie9vl7cxbihfn0n
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
解说:
docker service create 命令创建服务。
--name 标志将服务命名为helloworld。
--replicas 标志指定了期望状态为 1 个运行示例。
参数 busybox:1.25.1-musl /bin/sh -c "while true; do echo hello world; sleep 3; done 将服务定义为使用镜像busybox:1.25.1-musl 创建容器,并在里面执行 /bin/sh -c "while true; do echo hello world; sleep 3; done"。
2、在master上通过docker service ls 查询服务列表
[root@k8smaster /]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
edkbe2lic2ie helloworld replicated 1/1 busybox:1.25.1-musl
3、通过“docker service inspect --pretty helloworld” 查询容器的详细信息
[root@k8smaster /]# docker service inspect --pretty helloworld
ID: edkbe2lic2ie9vl7cxbihfn0n
Name: helloworld
Service Mode: Replicated
Replicas: 5
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: busybox:1.25.1-musl@sha256:46634e32e559271b8e18b7f1b21d981da4cd63ed8d36fbdc35c0b56464238a0c
Args: /bin/sh -c while true; do echo hello world; sleep 3; done
Init: false
Resources:
Endpoint Mode: vip
4、查看那个节点运行在该服务上 docker service ps helloworld
[root@k8smaster /]# docker service ps helloworld
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
tf6rgcdtg9lz helloworld.1 busybox:1.25.1-musl k8smaster Running Running 2 hours ago
pnv59taeonqy helloworld.2 busybox:1.25.1-musl pb_hdp_03 Running Running about an hour ago
qccg5si9wtxm helloworld.3 busybox:1.25.1-musl pb_hdp_03 Running Running about an hour ago
sx52pbyed2jg helloworld.4 busybox:1.25.1-musl k8smaster Running Running about an hour ago
r0riw4d2hvrt helloworld.5 busybox:1.25.1-musl pb_hdp_03 Running Running about an hour ago
5、删除运行的服务 docker service rm helloworld
[root@k8smaster /]# docker service rm helloworld
helloworld
6、docker service ls 查看运行在集群上的服务
下线节点:
1、通过docker node ls 查看当前活动的节点
[root@k8smaster /]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
npb3iydf14kkts0b5ie66k2vn * k8smaster Ready Active Leader 18.06.0-ce
qn9ewaj1kxn9g6wclylsow1vi pb_hdp_03 Ready Active 1.13.1
2、通过docker node update --availability drain pb_hdp_03 下线名称为pb_hdp_03 的节点
[root@k8smaster /]# docker node update --availability drain pb_hdp_03
pb_hdp_03
[root@k8smaster /]# docker service ps helloworld
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
wmjzu4nrra59 helloworld.1 busybox:1.25.1-musl k8smaster Ready Ready 5 seconds ago
90zczn851j81 \_ helloworld.1 busybox:1.25.1-musl pb_hdp_03 Shutdown Running 6 seconds ago
rxzzchtmlnb5 helloworld.2 busybox:1.25.1-musl k8smaster Ready Ready 5 seconds ago
v5qlp8cma8tj \_ helloworld.2 busybox:1.25.1-musl pb_hdp_03 Shutdown Running 6 seconds ago
1nrd1op72awm helloworld.3 busybox:1.25.1-musl k8smaster Running Running 2 minutes ago
vntrn9vwumxq helloworld.4 busybox:1.25.1-musl k8smaster Ready Ready 4 seconds ago
pbxar3qcyvgf \_ helloworld.4 busybox:1.25.1-musl pb_hdp_03 Shutdown Running 6 seconds ago
j0thl91hkm53 helloworld.5 busybox:1.25.1-musl k8smaster Running Running 2 minutes ago
可以看到该节点的状态为Ready
,但可用性为Drain
3、通过
docker node update --availability active pb_hdp_03 再次将pb_hdp_03启用
[root@k8smaster /]# docker node update --availability active pb_hdp_03
pb_hdp_03
[root@k8smaster /]# docker service ps helloworld
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
wmjzu4nrra59 helloworld.1 busybox:1.25.1-musl k8smaster Running Running 2 minutes ago
90zczn851j81 \_ helloworld.1 busybox:1.25.1-musl pb_hdp_03 Shutdown Shutdown 2 minutes ago
rxzzchtmlnb5 helloworld.2 busybox:1.25.1-musl k8smaster Running Running 2 minutes ago
v5qlp8cma8tj \_ helloworld.2 busybox:1.25.1-musl pb_hdp_03 Shutdown Shutdown 2 minutes ago
1nrd1op72awm helloworld.3 busybox:1.25.1-musl k8smaster Running Running 5 minutes ago
vntrn9vwumxq helloworld.4 busybox:1.25.1-musl k8smaster Running Running 2 minutes ago
pbxar3qcyvgf \_ helloworld.4 busybox:1.25.1-musl pb_hdp_03 Shutdown Shutdown 2 minutes ago
j0thl91hkm53 helloworld.5 busybox:1.25.1-musl k8smaster Running Running 5 minutes ago
说明现在该节点又可以重新接收任务了。
注:
一个可用性为Active
的节点在以下情况下可以接收到新任务:
- 当一个服务在伸缩规模时
- 滚动更新时
- 当你把其他某个节点的可用性设为
Drain
时 - 当某个任务在另外某个
Active
节点上启动失败时