Swarm集群搭建(6)

Swarm集群搭建

官方文档:https://docs.docker.com/engine/swarm/

# 管理配置文件
docker config
    # 查看已创建配置文件
    - docker config ls
    # 将已有配置文件添加到docker配置文件中
    - docker config create docker 配置文件名 本地配置文件
 # 管理swarm节点
docker node
    # 查看集群中的节点
    - docker node ls
    # 将manager角色降级为worker
    - docker node demote 主机名
    # 将worker角色升级为manager
    - docker node promote 主机名
    # 查看节点的详细信息,默认json格式
    - docker node inspect 主机名
          # 查看节点信息平铺格式
          - docker node inspect --pretty 主机名
    # 查看运行的一个或多个及节点任务数,默认当前节点
    - docker node ps
    # 从swarm中删除一个节点
    - docker node rm 主机名
    # 更新一个节点
    - docker node update
          # 对节点设置状态(“active”正常|“pause”暂停|“drain”排除自身work任务)
          - docker node update --availability
 # 管理敏感数据存储
docker secret
# 服务栈,栈的形式,一般作为编排使用,格式与docker compose相同。
docker stack
    # 通过.yml文件指令部署
    - docker stack deploy -c 文件名.yml 编排服务名
    # 查看编排服务
    - docker stack ls

工作原理

Docker Engine 1.12 introduces swarm mode that enables you to create a cluster of one or more Docker Engines called a swarm. A swarm consists of one or more nodes: physical or virtual machines running Docker Engine 1.12 or later in swarm mode.
There are two types of nodes: managers and workers.
Docker 引擎 1.12 引入了群模式,使您能够创建一个或多个 Docker 引擎(称为群)的集群。群由一个或多个节点组成:在群模式下运行 Docker 引擎 1.12 或更高版本的物理机或虚拟机。

image.png
If you haven’t already, read through the swarm mode overview and key concepts.

进行搭建:

$ docker swarm --help
Usage: docker swarm COMMAND
Manage Swarm
Commands:
ca # Display and rotate the root CA
init # 初始化一个Swarm
join # 加入一个集群作为一个节点 和/或 管理者
join-token # 管理生成的令牌
leave # 离开swarm
unlock # 解锁swarm
unlock-key # 解锁密钥管理
update # 更新swarm

docker swarm init --help
Initialize a swarm

Options:
--advertise-addr string Advertised address (format: <ip|interface>[:port])
--autolock Enable manager autolocking (requiring an unlock key to start a
stopped manager)
--availability string Availability of the node ("active"|"pause"|"drain") (default "active")
--cert-expiry duration Validity period for node certificates (ns|us|ms|s|m|h) (default
2160h0m0s)
--data-path-addr string Address or interface to use for data path traffic (format:
<ip|interface>)
--data-path-port uint32 Port number to use for data path traffic (1024 - 49151). If no
value is set or is set to 0, the default port (4789) is used.
--default-addr-pool ipNetSlice default address pool in CIDR format (default [])
--default-addr-pool-mask-length uint32 default address pool subnet mask length (default 24)
--dispatcher-heartbeat duration Dispatcher heartbeat period (ns|us|ms|s|m|h) (default 5s)
--external-ca external-ca Specifications of one or more certificate signing endpoints
--force-new-cluster Force create a new cluster from current state
--listen-addr node-addr Listen address (format: <ip|interface>[:port]) (default 0.0.0.0:2377)
--max-snapshots uint Number of additional Raft snapshots to retain
--snapshot-interval uint Number of log entries between Raft snapshots (default 10000)
--task-history-limit int Task history retention limit (default 5)

1.设置主要节点

$docker swarm init --advertise-addr 192.168.64.134

docker swarm init --advertise-addr 192.168.64.134
Swarm初始化: 当前节点(...)现在是一个manager(管理节点)。
Swarm initialized: current node (n2ws0fm7fvsiniuo9wjmq3dfh) is now a manager.

翻译:要添加一个worker(工作节点)到这个swarm,运行以下命令(令牌)
To add a worker to this swarm, run the following command:

docker swarm join --token SWMTKN-1-56fs1oaww5jkhsq7f3v2f3fazgm1jgxrvjc1n4cttab8v6bmr8-04efiil88xkppw186vk82y5f1 192.168.64.134:2377

翻译:要添加一个swarm(管理节点)到swarm,运行'docker swarm join-token manager',
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

2.生成令牌(管理生成的令牌)

生成管理节点的令牌
docker swarm join-token manager

生成工作节点的令牌
docker swarm join-token worker

docker swarm join-token manager
To add a manager to this swarm, run the following command:

【添加一个管理节点的命令】
docker swarm join --token SWMTKN-1-56fs1oaww5jkhsq7f3v2f3fazgm1jgxrvjc1n4cttab8v6bmr8-4bocoqdhfypuppql3yqxt0u2p 192.168.64.134:2377

[root@192 ~]# docker swarm join-token worker
To add a worker to this swarm, run the following command:
【添加一个工作节点的命令】
docker swarm join --token SWMTKN-1-56fs1oaww5jkhsq7f3v2f3fazgm1jgxrvjc1n4cttab8v6bmr8-04efiil88xkppw186vk82y5f1 192.168.64.134:2377

将服务器(192.168.64.135)加入工作节点(在192.168.64.135运行命令)
:::tips
【添加一个工作节点的命令】
$ docker swarm join --token SWMTKN-1-56fs1oaww5jkhsq7f3v2f3fazgm1jgxrvjc1n4cttab8v6bmr8-04efiil88xkppw186vk82y5f1 192.168.64.134:2377

docker swarm join --token SWMTKN-1-56fs1oaww5jkhsq7f3v2f3fazgm1jgxrvjc1n4cttab8v6bmr8-04efiil88xkppw186vk82y5f1 192.168.64.134:2377
This node joined a swarm as a worker.# 成功变为工作节点

4、查看所有节点信息

docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
n2ws0fm7fvsiniuo9wjmq3dfh * 192.168.64.134 Ready Active Leader 20.10.12
y1clyylemoiqbrquha4hpgdg7 192.168.64.135 Ready Active 20.10.12

6、移除节点

从swarm中删除一个节点 | -f 删除多个节点
$ docker node rm 节点id

7、将服务器(192.168.64.137)加入管理节点
这里manager加不进去的话应该也是要防火墙开放2377端口或关闭防火墙

永久关闭防火墙
$ systemctl disable firewalld
$ systemctl stop firewalld

停止并禁用开机启动
$ systemctl disable firewalld

【添加一个管理节点的命令】
docker swarm join --token SWMTKN-1-56fs1oaww5jkhsq7f3v2f3fazgm1jgxrvjc1n4cttab8v6bmr8-4bocoqdhfypuppql3yqxt0u2p 192.168.64.134:2377

docker swarm join --token SWMTKN-1-56fs1oaww5jkhsq7f3v2f3fazgm1jgxrvjc1n4cttab8v6bmr8-4bocoqdhfypuppql3yqxt0u2p 192.168.64.134:2377
This node joined a swarm as a manager. # 成功进入管理节点

8、查看所有节点信息

docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
n2ws0fm7fvsiniuo9wjmq3dfh * 192.168.64.134 Ready Active Leader 20.10.12
y1clyylemoiqbrquha4hpgdg7 192.168.64.135 Ready Active 20.10.12
29g0dvdvrtajsxr5d6lgutddr 192.168.64.136 Ready Active 20.10.12
mbtufs92q1q1kbsrdkfh3gx7s 192.168.64.137 Ready Active Reachable 20.10.12

posted @ 2022-11-22 15:04  yutoujun  阅读(42)  评论(0编辑  收藏  举报