SaltStack进阶
文章目录
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
...
# 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 IP删除
...
...
# minion in masterless mode.
file_client: local //取消注释并设置成local
...
...
file_roots: //设置file_roots的路径和环境
base:
- /srv/salt/base
...
[root@minion ~]# mkdir -p /srv/salt/base
1.2.2 关闭salt-minion服务
使用 masterless 模式时是不需要启动任何服务的,包括salt-master和salt-minion。
[root@minion ~]# systemctl disable --now 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 test.ping
local:
True
[root@minion ~]# salt-call --local cmd.run 'uptime'
local:
05:54:27 up 7 min, 1 user, load average: 0.02, 0.10, 0.07
2. salt-master高可用
2.1 salt-master高可用配置
我们需要用salt来管理公司的所有机器,那么salt的master就不能宕机,否则就会整个瘫痪,所以我们必须要对salt进行高可用。
//接收minion的key,修改master配置文件并同步两个master的pki目录
[root@master1 ~]# salt-key -ya minion
The following keys are going to be accepted:
Unaccepted Keys:
minion
Key for minion minion accepted.
[root@master1 ~]# vim /etc/salt/master
...
master_sign_pubkey: True //取消注释
...
[root@master1 ~]# systemctl restart salt-master
[root@master1 ~]# scp -r /etc/salt/pki root@192.168.79.129:/etc/salt/
root@192.168.79.129's password:
minion 100% 450 433.8KB/s 00:00
master 100% 450 355.7KB/s 00:00
master.pem 100% 1674 1.0MB/s 00:00
master.pub 100% 450 240.2KB/s 00:00
master_sign.pem 100% 1674 722.8KB/s 00:00
master_sign.pub 100% 450 73.5KB/s 00:00
minion.pem 100% 1674 1.2MB/s 00:00
minion.pub 100% 450 236.9KB/s 00:00
[root@master1 ~]# scp -r /etc/salt/master root@192.168.79.129:/etc/salt/master
root@192.168.79.129's password:
master 100% 50KB 9.4MB/s 00:00
//重启master2服务
[root@master2 ~]# systemctl restart salt-master
//修改minion配置文件
[root@master1 ~]# scp -r /etc/salt/pki/master/master_sign.pub root@192.168.79.130:/etc/salt/pki/minion
root@192.168.79.130's password:
master_sign.pub 100% 450 337.0KB/s 00:00
[root@minion ~]# vim /etc/salt/minion
...
#default_include: minion.d/*.conf
//添加以下两行
verify_master_pubkey_sign: True
always_verify_signature: True
# 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.79.128
- 192.168.79.129
master_type: failover
master_shuffle: True
master_alive_interval: 1
...
[root@minion ~]# systemctl restart salt-minion
//查看两个master与minion的连接状态
[root@master1 ~]# salt '*' test.ping
minion:
True
[root@master2 ~]# salt '*' test.ping
minion:
Minion did not return. [No response]
ERROR: Minions returned with non-zero exit code
//停止master1服务测试
[root@master1 ~]# systemctl stop salt-master
[root@master1 ~]# salt '*' test.ping
[ERROR ]
Salt request timed out. The master is not responding. You may need to run your command with `--async` in order to bypass the congested event bus. With `--async`, the CLI tool will print the job id (jid) and exit immediately without listening for responses. You can then use `salt-run jobs.lookup_jid` to look up the results of the job in the job cache later.
[root@master2 ~]# salt '*' test.ping //看到True即表示配置成功
minion:
True
2.2 salt-master高可用之数据同步
涉及到高可用时,数据的同步是个永恒的话题,我们必须保证高可用的2个master间使用的数据是一致的,包括:
- /etc/salt/master配置文件
- /etc/salt/pki目录下的所有key
- /srv/下的salt和pillar目录下的所有文件
保障这些数据同步的方案有:
- nfs挂载
- rsync同步
- 使用gitlab进行版本控制
安全相关:
为保证数据的同步与防止丢失,可将状态文件通过gitlab进行版本控制管理。
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部署
环境说明:
主机IP | 角色 | 安装的应用 |
---|---|---|
192.168.79.128 | master | salt-master |
192.168.79.129 | syndic | salt-master salt-syndic |
192.168.79.130 | minion | salt-minion |
配置yum源
yum -y install https://repo.saltstack.com/yum/redhat/salt-repo-3000.el7.noarch.rpm
安装应用
[root@master ~]# yum -y install salt-master
[root@syndic ~]# yum -y install salt-master salt-syndic
[root@minion ~]# yum -y install salt-minion
修改master的master配置文件
- 取消注释order_master
- 将order_master的值设为True
[root@master ~]# vim /etc/salt/master
...
order_masters: True
...
[root@master ~]# systemctl enable --now salt-master
修改syndic所在主机的master配置文件
- 取消注释syndic_master
- 将syndic_master的值设为master的IP
[root@syndic ~]# vim /etc/salt/master
...
syndic_master: 192.168.79.128
...
[root@syndic ~]# systemctl enable --now salt-master
[root@syndic ~]# systemctl enable --now salt-syndic
配置minion,将master指向syndic所在主机
[root@minion ~]# vim /etc/salt/minion
...
master: 192.168.79.129
...
[root@minion ~]# systemctl enable --now salt-minion
在syndic上接受minion主机的key
[root@syndic ~]# salt-key -yA
The following keys are going to be accepted:
Unaccepted Keys:
minion
Key for minion minion accepted.
在master上接受syndic主机的key
[root@master ~]# salt-key -yA
The following keys are going to be accepted:
Unaccepted Keys:
syndic
Key for minion syndic accepted.
在master上执行模块或状态检验minion是否应答
[root@master ~]# salt '*' test.ping
minion:
True
[root@master ~]# salt '*' cmd.run uptime
minion:
10:21:12 up 1:09, 1 user, load average: 0.00, 0.01, 0.05