1+X 云计算运维与开发(中级)案例实战——部署swarm即使用


前言

学而不思则罔,思而不学则殆。

IP 主机名 节点
192.168.200.10/24 master swarm 集群 master 节点
192.168.200.20/24 node swarm 集群 node 节点

思路

本次部署swarm,我们将安装portainer,然后熟悉一些命令

首先是安装portainer:
1.修改主机名,配置主机映射,设置时间同步
2.开启Docker API
3.初始化swarm集群
4.将node加入到集群
5.安装portainer

其次是熟悉命令:
docker service
docker node
docker swarm


实操

1. 安装potainer

1.1 修改主机名,配置主机映射,设置时间同步

修改主机名,配置主机映射

master节点:

[root@master ~]# hostnamectl set-hostname master
[root@master ~]# bash
bash
[root@master ~]#
[root@master ~]# cat >> /etc/hosts << eof
> 192.168.200.130 master
> 192.168.200.132 node
> eof
[root@master ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.130 master
192.168.200.132 node
[root@master ~]#

node节点:

[root@master ~]# hostnamectl set-hostname node
[root@master ~]# bash
bash
[root@node ~]#

[root@node ~]# cat >> /etc/hosts << eof

> 192.168.200.130 master
> 192.168.200.132 node
> eof
[root@node ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.130 master
192.168.200.132 node
[root@node ~]#

设置时间同步
master节点:

[root@master ~]# yum -y install chrony
[root@master ~]# sed -i 's/^server/#&/' /etc/chrony.conf
[root@master ~]# cat >> /etc/chrony.conf << eof
> server master iburst
> local stratum 10
> allow all
> eof
[root@master ~]# systemctl restart chronyd
[root@master ~]# timedatectl set-ntp true
[root@master ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* master                       10   6    37    70    -10us[  -25us] +/-   37us
[root@master ~]#

node节点:

[root@node ~]# yum -y install chrony
[root@node ~]# ^C
[root@node ~]# sed -i 's/^server/#&/' /etc/chrony.conf
[root@node ~]# echo server master iburst >> /etc/chrony.conf
[root@node ~]# systemctl restart chronyd
[root@node ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* master                       11   6    17    15    -12us[ -794us] +/- 6015us
[root@node ~]#

两台机子执行命令—— #chronyc source 之后显示 ^* 即为成功

1.2 开启Docker API

对两台机子进行相同操作
这里以master为例:

[root@master ~]# vi /lib/systemd/system/docker.service
#将第14行改为以下内容
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2573 -H unix:///var/run/docker.sock
#保存退出后
[root@master ~]# systemctl docker restart
[root@master ~]# systemctl restart docker

1.3 初始化swarm集群,将node加入到集群

master节点:

[root@master ~]# docker swarm init --advertise-addr 192.168.200.130
Swarm initialized: current node (r92bkng7m42nx0gzcpkbuistx) is now a manager.

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

    docker swarm join --token SWMTKN-1-2e6p74jc37gtr5wxyq0huuuhdsr4xvtk55zxxw1gs4qaigdyl4-arw8p959mkhwvph5erhmdn819 192.168.200.130:2377

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

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

    docker swarm join --token SWMTKN-1-2e6p74jc37gtr5wxyq0huuuhdsr4xvtk55zxxw1gs4qaigdyl4-arw8p959mkhwvph5erhmdn819 192.168.200.130:2377

dockers swarm init 表示初始化swarm集群,然后它会回显一串token值,我们需要留意,如果之后有操作导致找不到这个token值,
则我们可以使用docker swarm join-token worker来获取,并且这个命令在上一条初始化命令中是有提示的

node节点:

[root@node ~]# docker swarm join --token SWMTKN-1-2e6p74jc37gtr5wxyq0huuuhdsr4xvtk55zxxw1gs4qaigdyl4-arw8p959mkhwvph5erhmdn819 192.168.200.130:2377
This node joined a swarm as a worker.
[root@node ~]#

在master节点验证集群:

[root@master ~]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
r92bkng7m42nx0gzcpkbuistx *   master              Ready               Active              Leader              18.09.6
mpkgede7dt90j751oidmx14h1     node                Ready               Active                                  18.09.6
[root@master ~]#

1.4 安装portainer

Portainer 是 Docker 的图形化管理工具,提供状态显示面板、应用模板快速部署、容器镜像网络数据卷的基本操作(包括上传下载镜像,创建容器等操作)、事件日志显示、容器控制台操作、Swarm 集群和服务等集中管理和操作、登录用户管理和控制等功能。功能十分全面,基本能满足中小型企业对容器管理的全部需求。

在master节点:
上传镜像:

[root@master images]# docker load -i portainer_portainer_latest.tar
dd4969f97241: Loading layer [==================================================>]    278kB/278kB
8b156c0136c9: Loading layer [==================================================>]  80.32MB/80.32MB
Loaded image: portainer/portainer:latest
[root@master images]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
portainer/portainer   latest              4cda95efb0e4        2 years ago         80.6MB
[root@master images]#

启动portainer容器:

[root@master ~]# docker volume create portainer_data  ##创建一个卷
[root@master ~]# docker service create --name potainer --replicas 1 -p 9000:9000 --constraint 'node.role == manager' 
--mount type=bind,src=//var/run/docker.sock,dst=/var/run/docker.sock 
--mount type=volume,src=portainer_data,dst=/data portainer/portainer 
-H unix:///var/run/docker.sock

rglyv1u0p3bfcm82dnptcs8ow
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged

关于 docker.sock:我们需要了解什么是套接字(socket);然后要了解docker的套接字是怎么运作的

  1. docker.sock可以参考这位大佬的博客:https://www.cnblogs.com/fundebug/p/6723464.html
  2. 套接字可以参考这位大佬的文章:https://www.cnblogs.com/h5l0/p/15114948.html

事实上,我们也可以用以下命令安装portainer:

docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock portainer/portainer

打开图形界面,在浏览器上输入master节点的 IP地址:9000
在这里插入图片描述

2. 熟悉命令

回到master节点

2.1 运行 service

部署 service 的命令形式与运行容器的 docker run 很相似,如:

[root@master ~]# docker service create --name web --replicas 2 nginx
qnc9h4dlw15aimbymqnkkigcs
overall progress: 2 out of 2 tasks
1/2: running   [==================================================>]
2/2: running   [==================================================>]
verify: Service converged
[root@master ~]#

–name 为 service 命名,nginx 为镜像的名字,–replicas 参数指定此服务在工作节点上运行的任务数。

通过 docker service ls 命令,可以查看当前 Swarm 中的 service。

[root@master ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                        PORTS
rglyv1u0p3bf        potainer            replicated          1/1                 portainer/portainer:latest   *:9000->9000/tcp
qnc9h4dlw15a        web                 replicated          2/2                 nginx:latest
[root@master ~]#

REPLICAS显示当前副本信息,2/2的意思是web这个service期望的容器副本数量为2,目前已经启动的副本数量为 2,也即当前 service 已经部署完成。

命令 docker service ps 可以查看 service 每个副本的状态

[root@master ~]# docker service ps web
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
2fix4zbk3b82        web.1               nginx:latest        node                Running             Running 35 seconds ago
jbq6hvipt8lp        web.2               nginx:latest        master              Running             Running 37 seconds ago
[root@master ~]#

2.2 service 伸缩

之前部署了只有两个副本的 service,不过对于 Web 服务,通常会运行多个实例。这样可以负载均衡,同时也能提供高可用。

Swarm 要实现这个目标非常简单,执行命令“docker service scale 服务名=副本数量”就可以增加 service 的副本数.

在 master 上执行如下命令

[root@master ~]# docker service scale web=5
web 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
[root@master ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                        PORTS
rglyv1u0p3bf        potainer            replicated          1/1                 portainer/portainer:latest   *:9000->9000/tcp
qnc9h4dlw15a        web                 replicated          5/5                 nginx:latest
[root@master ~]# docker service ps web
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
2fix4zbk3b82        web.1               nginx:latest        node                Running             Running 2 minutes ago
jbq6hvipt8lp        web.2               nginx:latest        master              Running             Running 2 minutes ago
mbnia6ahv879        web.3               nginx:latest        node                Running             Running 18 seconds ago
wuhbr8obf3qm        web.4               nginx:latest        master              Running             Running 18 seconds ago
nji2k5d1kvbp        web.5               nginx:latest        node                Running             Running 18 seconds ago

5 个副本已经分布在 Swarm 的各个节点上

2.3 调度节点

默认配置下 master 也是 worker node,所以 master 上也运行了副本。如果不希望在 master上运行 ervice,可以执行如下命令。

[root@master ~]# docker node update --availability drain master
master
[root@master ~]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
r92bkng7m42nx0gzcpkbuistx *   master              Ready               Drain               Leader              18.09.6
mpkgede7dt90j751oidmx14h1     node                Ready               Active                                  18.09.6

Drain表示master已经不负责运行service,之前master运行的那2个副本会如何处理呢?
使用 docker service ps 查看一下。

[root@master ~]# docker service ps web
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE             ERROR               PORTS
2fix4zbk3b82        web.1               nginx:latest        node                Running             Running 5 minutes ago
j44hf93h5xwx        web.2               nginx:latest        node                Running             Running 45 seconds ago
jbq6hvipt8lp         _ web.2           nginx:latest        master              Shutdown            Shutdown 46 seconds ago
mbnia6ahv879        web.3               nginx:latest        node                Running             Running 2 minutes ago
42wsnt0tw4ng        web.4               nginx:latest        node                Running             Running 45 seconds ago
wuhbr8obf3qm         _ web.4           nginx:latest        master              Shutdown            Shutdown 46 seconds ago
nji2k5d1kvbp        web.5               nginx:latest        node                Running             Running 2 minutes ago
[root@master ~]#

master 上的副本 web.1 和 web.3 已经被 Shutdown 了,为了达到 5 个副本数的目标,在node 上添加了新的副本 web.1 和 web.3。

2.4 访问 service

要将 service 暴露到外部,方法其实很简单,执行下面的命令即可。

[root@master ~]# docker service update --publish-add 8080:80 web
web
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

浏览器访问 IP地址:8080
在这里插入图片描述


总结

你发如雪 凄美了离别
我焚香感动了谁
邀明月 让回忆皎洁
爱在月光下完美