1. masterless
1.1 应用场景
- master 与 minion 网络不通或通信有延迟,即网络不稳定
- 想在 minion 端直接执行状态
传统的 SaltStack 是需要通过 master 来执行状态控制 minion 从而实现状态的管理,但是当网络不稳定的时候,当想在minion本地执行状态的时候,当在只有一台主机的时候,想执行状态该怎么办呢?这就需要用到 masterless 了。
有了masterless,即使你只有一台主机,也能玩saltstack,而不需要你有N台主机架构。
1.2 masterless配置
1.2.1 修改配置文件minion
- 注释master行
- 取消注释file_client并设其值为local
- 设置file_roots
- 设置pillar_roots
[root@minion ~]# vim /etc/salt/minion
....此处省略N行
# resolved, then the minion will fail to start.
# master: salt //注释此行
....此处省略N行
file_client: local //取消此行注释并将值设为local
....此处省略N行
file_roots: //设置file_roots的路径和环境,可有多套环境
base:
- /srv/salt/base
....此处省略N行
pillar_roots: //设置pillar_roots的路径和环境,可有多套环境
base:
- /srv/pillar/base
创建结构
[root@minion ~]# mkdir -p /srv/{pillar,salt}/base
[root@minion ~]# tree /srv/
/srv/
├── pillar
│ └── base
└── salt
└── base
1.2.2 关闭salt-minion服务
使用 masterless 模式时是不需要启动任何服务的,包括salt-master和salt-minion。
[root@minion ~]# systemctl stop salt-minion
[root@minion ~]# systemctl disable salt-minion
Removed symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service.
1.2.3 salt-call
masterless模式执行模块或状态时需要使用salt-call命令,而不再是salt或者salt-ssh。需要注意的是要使用salt-call的–local选项。
[root@minion ~]# salt-call --local cmd.run 'date'
local:
Mon Nov 29 17:26:34 CST 2021
[root@minion ~]# salt-call --local cmd.run 'ls -l /root'
local:
total 4
-rw-------. 1 root root 1384 Oct 11 16:06 anaconda-ks.cfg
[root@minion ~]# salt-call --local cmd.run 'uptime'
local:
17:32:55 up 3:49, 3 users, load average: 0.06, 0.08, 0.08
[root@minion ~]# salt-call --local state.sls init.history.main
local:
----------
ID: history
Function: file.line
Name: /etc/profile
Result: True
Comment: No changes needed to be made
Started: 17:36:34.715565
Duration: 16.63 ms
Changes:
Summary for local
------------
Succeeded: 1
Failed: 0
------------
Total states run: 1
Total run time: 16.630 ms
2. salt-master高可用
2.1 salt-master高可用配置
我们需要用salt来管理公司的所有机器,那么salt的master就不能宕机,否则就会整个瘫痪,所以我们必须要对salt进行高可用。salt的高可用配置非常简单,只需要改一下minion配置文件,将master用列表的形式列出即可。
[root@minion ~]# vim /etc/salt/minion
....此处省略N行
master:
- 192.168.153.139
- 192.168.153.155
....此处省略N行
本例列出的69.201和69.202上必须都安装了salt-master且保证服务都是正常状态。
2.2 salt-master高可用之数据同步
涉及到高可用时,数据的同步是个永恒的话题,我们必须保证高可用的2个master间使用的数据是一致的,包括:
- /etc/salt/master配置文件
- /etc/salt/pki目录下的所有key
- /srv/下的salt和pillar目录下的所有文件
保障这些数据同步的方案有:
- nfs挂载
- rsync同步
- 使用gitlab进行版本控制
安全相关:
为保证数据的同步与防止丢失,可将状态文件通过gitlab进行版本控制管理。
高可用部署
环境
主机类型 | IP | 要安装的应用 |
---|---|---|
master01(主) | 192.168.153.139 | salt-master |
master02(备) | 192.168.153.155 | salt-master |
minion | 192.168.153.153 | salt-minion |
第一步安装
master01和master02安装salt-master(因为配置一样所以省略master02步骤)
//配置yum源
[root@master01 yum.repos.d]# rpm --import https://repo.saltproject.io/py3/redhat/8/x86_64/latest/SALTSTACK-GPG-KEY.pub
[root@master yum.repos.d]# curl -fsSL https://repo.saltproject.io/py3/redhat/8/x86_64/latest.repo | sudo tee /etc/yum.repos.d/salt.repo
[root@master01 ~]# yum clean all
已加载插件:fastestmirror
正在清理软件源: base epel extras salt-latest updates
Cleaning up everything
Cleaning up list of fastest mirrors
//安装saltstack主控端
[root@master01 ~]# yum -y install salt-master
安装过程略....
//启动主控端的salt-master,并设置开机自启
[root@master01 ~]# systemctl start salt-master
[root@master01 ~]# systemctl enable salt-master
Created symlink /etc/systemd/system/multi-user.target.wants/salt-master.service → /usr/lib/systemd/system/salt-master.service.
[root@master01 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 0.0.0.0:4505 0.0.0.0:*
LISTEN 0 128 0.0.0.0:4506 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
minion安装salt-minion
//配置yum源
[root@minion yum.repos.d]# rpm --import https://repo.saltproject.io/py3/redhat/8/x86_64/latest/SALTSTACK-GPG-KEY.pub
[root@minion yum.repos.d]# curl -fsSL https://repo.saltproject.io/py3/redhat/8/x86_64/latest.repo | sudo tee /etc/yum.repos.d/salt.repo
[root@minion ~]# yum clean all
已加载插件:fastestmirror
正在清理软件源: base epel extras salt-latest updates
Cleaning up everything
Cleaning up list of fastest mirrors
[root@minion ~]# yum -y install salt-minion
安装过程略....
//启动受控端的salt-minion并设置开机自启
[root@minion ~]# systemctl start salt-minion
[root@minion ~]# systemctl enable salt-minion
Created symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service → /usr/lib/systemd/system/salt-minion.service.
第二步配置
master01和master02配置(因为配置一样所以省略master02步骤)
[root@master01 ~]# vim /etc/salt/master
....此处省略N行
pillar_roots: //设置pillar_roots的路径和环境
base:
- /srv/pillar/base
test:
- /srv/pillar/test
dev:
- /srv/pillar/dev
prod:
- /srv/pillar/prod
....此处省略N行
file_roots: //设置file_roots的路径和环境
base:
- /srv/salt/base
test:
- /srv/salt/test
dev:
- /srv/salt/dev
prod:
- /srv/salt/prod
[root@master01 ~]# mkdir -p /srv/salt/{base,test,dev,prod}
[root@master01 ~]# tree /srv/salt/
/srv/salt/
├── base
├── dev
├── prod
└── test
[root@master01 ~]# mkdir -p /srv/pillar/{base,test,dev,prod}
[root@master01 ~]# tree /srv/pillar/
/srv/pillar/
├── base
├── dev
├── prod
└── test
minion配置
[root@minion ~]# vim /etc/salt/minion #添加master01服务端
....此处省略N行
master: 192.168.153.139
....此处省略N行
[root@minion ~]# systemctl restart salt-minion.service
第三步测试
首先master01测试
[root@master01 ~]# salt-key -L #查看minion的键值
Accepted Keys:
Denied Keys:
Unaccepted Keys:
node1
Rejected Keys:
[root@master01 ~]# salt-key -yA #接受minion的键值
The following keys are going to be accepted:
Unaccepted Keys:
node1
[root@master01 ~]# salt-key -L
Accepted Keys:
node1
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@master01 ~]# salt '*' test.ping #测试是否能ping通
node1:
True
当主master01与minion端ping通后,再将master01主机上的/etc/salt/pki/master目录中的公钥与私钥传(master.pem master.pub)输到备(masters主机)的/etc/salt/pki/master目录中。
[root@master01 ~]# cd /etc/salt/pki
[root@master01 pki]# scp -r master/master.pem 192.168.153.155:/etc/salt/pki/master/
root@192.168.153.155's password:
master.pem 100% 1675 3.4MB/s 00:00
[root@master01 pki]# scp -r master/master.pub 192.168.153.155:/etc/salt/pki/master/
root@192.168.153.155's password:
master.pub 100% 451 1.0MB/s 00:00
传输完成后,再去修改minion的配置文件内容如下:
[root@minion ~]# vim /etc/salt/minion
.....
# Set the location of the salt master server. If the master server cannot be
# resolved, then the minion will fail to start.
#master: salt
master: 192.168.153.155 //指定备masters02
# Set http proxy information for the minion when doing requests
......
修改完成后重启salt-minion
[root@minion ~]# systemctl restart salt-minion
等待证书生成后,授权证书,进行test.ping检测(注意防火墙)
[root@master02 master]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
node1
Rejected Keys:
[root@master02 ~]# salt-key -ya node1
The following keys are going to be accepted:
Unaccepted Keys:
node1
Key for minion node1 accepted.
[root@master02 ~]# salt-key -L
Accepted Keys:
node1
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@master02 ~]# salt '*' test.ping
node1:
True
当两台master都能ping通之后,最后再进行高可用设置,如下:
[root@minion ~]# vim /etc/salt/minion
.....
# Set the location of the salt master server. If the master server cannot be
# resolved, then the minion will fail to start.
#master: salt
master:
- 192.168.153.139 //指定主master01
- 192.168.153.155 //指定备masters02
# Set http proxy information for the minion when doing requests
......
配置故障转移
[root@minion ~]# vim /etc/salt/minion
# beacons) without a master connection
master_type: failover //高可用(故障转移)
----------
# connection events.
#
master_alive_interval: 10 //主机等待的时间间隔
配置完成后重启salt-minion
[root@minion ~]# systemctl restart salt-minion
此时两台salt-master都为开启状态
[root@master01 ]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:4505 0.0.0.0:*
LISTEN 0 128 0.0.0.0:4506 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@masters02 ]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:4505 0.0.0.0:*
LISTEN 0 128 0.0.0.0:4506 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
在主master01上test.ping测试
[root@master01 master]# salt '*' test.ping
node1:
True
在备masters02上test.ping测试
[root@master02 ~]# salt '*' test.ping
node1:
Minion did not return. [No response]
The minions may not have all finished running and any remaining minions will return upon completion. To look up the return data for this job later, run the following command:
salt-run jobs.lookup_jid 20211129121458500646
(当两台主机都在运行状态时,minion只能连接到主master01上
此时的状态文件为
[root@minion ~]# systemctl status salt-minion
● salt-minion.service - The Salt Minion
Loaded: loaded (/usr/lib/systemd/system/salt-minion.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2021-11-29 20:12:46 CST; 3min 51s ago
Docs: man:salt-minion(1)
file:///usr/share/doc/salt/html/contents.html
https://docs.saltproject.io/en/latest/contents.html
Main PID: 168263 (salt-minion)
Tasks: 6 (limit: 23648)
Memory: 94.4M
CGroup: /system.slice/salt-minion.service
├─168263 /usr/bin/python3.6 /usr/bin/salt-minion
├─168268 /usr/bin/python3.6 /usr/bin/salt-minion
└─168270 /usr/bin/python3.6 /usr/bin/salt-minion
11月 29 20:12:46 node1 systemd[1]: Stopped The Salt Minion.
11月 29 20:12:46 node1 systemd[1]: Starting The Salt Minion...
11月 29 20:12:46 node1 systemd[1]: Started The Salt Minion.
11月 29 20:13:38 node1 salt-minion[168263]: [CRITICAL] 'master_type' set to 'failover' but 'retry_dns' is not 0. Setting 'retry_dns' to 0 to failover to the next master >
模拟主master01挂彩
[root@master01 ~]# systemctl stop salt-master
[root@master01 ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
再备master02主机上进行test.ping检测
[root@master02 master]# salt '*' test.ping
node1:
True
此时的状态文件是
[root@minion ~]# systemctl status salt-minion
● salt-minion.service - The Salt Minion
Loaded: loaded (/usr/lib/systemd/system/salt-minion.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2021-11-29 20:12:46 CST; 8min ago
Docs: man:salt-minion(1)
file:///usr/share/doc/salt/html/contents.html
https://docs.saltproject.io/en/latest/contents.html
Main PID: 168263 (salt-minion)
Tasks: 7 (limit: 23648)
Memory: 95.0M
CGroup: /system.slice/salt-minion.service
├─168263 /usr/bin/python3.6 /usr/bin/salt-minion
├─168268 /usr/bin/python3.6 /usr/bin/salt-minion
└─168270 /usr/bin/python3.6 /usr/bin/salt-minion
11月 29 20:12:46 node1 systemd[1]: Stopped The Salt Minion.
11月 29 20:12:46 node1 systemd[1]: Starting The Salt Minion...
11月 29 20:12:46 node1 systemd[1]: Started The Salt Minion.
11月 29 20:13:38 node1 salt-minion[168263]: [CRITICAL] 'master_type' set to 'failover' but 'retry_dns' is not 0. Setting 'retry_dns' to 0 to failover to the next master >
11月 29 20:21:10 node1 salt-minion[168263]: [WARNING ] Master ip address changed from 192.168.153.155 to 192.168.153.139
11月 29 20:21:10 node1 salt-minion[168263]: [WARNING ] Master ip address changed from 192.168.153.155 to 192.168.153.139
3. salt-syndic分布式架构
3.1 salt-syndic架构图
3.2 salt-syndic的优劣势
优势:
- 可以通过syndic实现更复杂的salt架构
- 减轻master的负担
劣势:
- syndic的/srv目录下的salt和pillar目录内容要与最顶层的master下的一致,所以要进行数据同步,同步方案同salt-master高可用
- 最顶层的master不知道自己有几个syndic,它只知道自己有多少个minion,并不知道这些minion是由哪些syndic来管理的
3.3 salt-syndic部署
3.3.1 环境说明
主机IP | 角色 | 安装的应用 |
---|---|---|
192.168.153.139 | Master | salt-master |
192.168.153.130 | Syndic | salt-master salt-syndic |
192.168.153.153 | Minion | salt-minion |
192.168.153.161 | Minion | salt-minion |
3.3.2 安装salt-master与salt-syndic
在192.168.153.130
上安装salt-master
与salt-syndic
,安装前请自行配置yum源
[root@syndic ~]# yum -y install salt-master salt-syndic
3.3.3 配置master
修改master的master配置文件
- 取消注释order_master
- 将order_master的值设为True
[root@master ~]# vim /etc/salt/master
.....此处省略N行
# masters' syndic interfaces.
order_masters: True
[root@master ~]# systemctl enable salt-master
[root@master ~]# systemctl restart salt-master
3.3.4 配置syndic
修改syndic所在主机的master配置文件
- 取消注释syndic_master
- 将syndic_master的值设为master的IP
[root@syndic ~]# vim /etc/salt/master
.....此处省略N行
syndic_master: 192.168.153.139
[root@syndic ~]# systemctl enable --now salt-master
Created symlink from /etc/systemd/system/multi-user.target.wants/salt-master.service to /usr/lib/systemd/system/salt-master.service.
[root@syndic ~]# systemctl enable --now salt-syndic
Created symlink from /etc/systemd/system/multi-user.target.wants/salt-syndic.service to /usr/lib/systemd/system/salt-syndic.service.
[root@syndic ~]# systemctl restart salt-master
[root@syndic ~]# systemctl restart salt-syndic
3.3.5 配置minion
配置minion,将master指向syndic所在主机
[root@minion ~]# vim /etc/salt/minion
.....此处省略N行
master: 192.168.153.130
[root@minion ~]# systemctl restart salt-minion
[root@minion ~]# systemctl enable salt-minion
Created symlink from /etc/systemd/system/multi-user.target.wants/salt-minion.service to /usr/lib/systemd/system/salt-minion.service.
在所有minion上做同样的操作,注意,要设置minion配置文件中的id参数,指向minion自身的ip地址或主机名,必须能够唯一标识minion本机。
3.3.6 在syndic上接受minion主机的key
[root@syndic ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
192.168.153.153
192.168.153.161
Proceed? [n/Y] Y
Key for minion 192.168.153.153 accepted.
Key for minion 192.168.153.161 accepted.
[root@syndic ~]# salt-key
Accepted Keys:
192.168.153.153
192.168.153.161
Denied Keys:
Unaccepted Keys:
Rejected Keys:
3.3.7 在master上接受syndic主机的key
[root@master ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
192.168.153.130
Proceed? [n/Y] Y
Key for minion 192.168.153.130 accepted.
[root@master ~]# salt-key
Accepted Keys:
192.168.153.130
Denied Keys:
Unaccepted Keys:
Rejected Keys:
3.3.8 在master上执行模块或状态检验有几个minion应答
[root@master ~]# salt '*' test.ping
192.168.153.161:
True
192.168.153.153:
True