saltstack进阶
masterless
应用场景
- master 与 minion 网络不通或通信有延迟,即网络不稳定
- 想在 minion 端直接执行状态
传统的 SaltStack 是需要通过 master 来执行状态控制 minion 从而实现状态的管理,但是当网络不稳定的时候,当想在minion本地执行状态的时候,当在只有一台主机的时候,想执行状态该怎么办呢?这就需要用到 masterless 了。
有了masterless,即使你只有一台主机,也能玩saltstack,而不需要你有N台主机架构。
masterless配置
修改配置文件minion
- 注释master行
- 取消注释file_client并设其值为local
- 设置file_roots
- 设置pillar_roots
[root@node1 ~]# vim /etc/salt/minion
.....
#file_client: remote
file_client: local
......
#file_roots:
# base:
# - /srv/salt
file_roots: #yml文件存放地址
base:
- /srv/salt/base
.....
# also be configured on the minion:
#pillar_roots:
# base:
# - /srv/pillar/
pillar_roots: #环境变量存放地址
base:
- /srv/pillar/base
关闭salt-minion服务
[root@node1 ~]# systemctl disable --now salt-minion
Removed /etc/systemd/system/multi-user.target.wants/salt-minion.service.
salt-call
masterless模式执行模块或状态时需要使用salt-call命令,而不再是salt或者salt-ssh。需要注意的是要使用salt-call的–local选项。
[root@node1 ~]# salt-call --local cmd.run 'date'
local:
Mon Nov 29 22:21:04 CST 2021
创建目录
[root@node1 ~]# mkdir -p /srv/{salt,pillar}/base
[root@node1 ~]# cd /srv/
[root@node1 srv]# ls
pillar salt
[root@node1 srv]# tree
.
|-- pillar
| `-- base
`-- salt
`-- base
随便编写一个yml文件
[root@node1 history]# cat main2.sls
/etc/profile:
file.append:
- text: 'export HISTTIMEFORMAT="%F %T `whoami`"'
执行
[root@node1 history]# salt-call --local state.sls history.main2
local:
----------
ID: /etc/profile
Function: file.append
Result: True
Comment: File /etc/profile is in correct state
Started: 22:34:13.658508
Duration: 7.215 ms
Changes:
Summary for local
------------
Succeeded: 1
Failed: 0
------------
Total states run: 1
Total run time: 7.215 ms
salt-master高可用
salt-master高可用配置
我们需要用salt来管理公司的所有机器,那么salt的master就不能宕机,否则就会整个瘫痪,所以我们必须要对salt进行高可用。
准备工作:在相应的机器上安装相应的服务
| 系统:RedHat/CentOS 7/8 | IP地址 | 安装服务 |
|---|---|---|
| 主 master1 | 192.168.244.131 | salt-master |
| 备 master2 | 192.168.244.133 | salt-master |
| node2 | 192.168.244.135 | salt-minion |
salt-master高可用之数据同步
及到高可用时,数据的同步是个永恒的话题,我们必须保证高可用的2个master间使用的数据是一致的,包括:
- /etc/salt/master配置文件
- /etc/salt/pki目录下的所有key
- /srv/下的salt和pillar目录下的所有文件
保障这些数据同步的方案有:
- nfs挂载
- rsync同步
- 使用gitlab进行版本控制
安全相关:
为保证数据的同步与防止丢失,可将状态文件通过gitlab进行版本控制管理。
如果两台新机设置主备,所有配置要一样,如果不是新机设置主备,则把主上面/etc/salt/master配置文件复制到备机上面,其他设置也要一样。
在备机上
安装salt-master
[root@master2 ~]# yum -y install salt-master
创建目录
[root@master2 ~]# mkdir -p /src/{salt/{base,test,prod,dev},pillar/{base,prod}}
[root@master2 ~]# tree /src/
/src/
|-- pillar
| |-- base
| `-- prod
`-- salt
|-- base
|-- dev
|-- prod
`-- test
修改node2salt-minion配置文件
[root@node2 ~]# vim /etc/salt/minion
...
#master: salt
master:
- 192.168.244.131 主
重启服务
[root@node2 ~]# systemctl restart salt-minion
在主机上查看
[root@master1 ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
node2
Rejected Keys:
[root@master1 ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
node2
Proceed? [n/Y] y
Key for minion node2 accepted.
[root@master1 ~]# salt '*' test.ping
node2:
True
把主机上的master.pem、master.pub文件复制到备机上
[root@master1 ~]# tree /etc/salt/pki/
/etc/salt/pki/
|-- master
| |-- master.pem
| |-- master.pub
| |-- minions
| | `-- node2
| |-- minions_autosign
| |-- minions_denied
| |-- minions_pre
| |-- minions_rejected
| `-- ssh
`-- minion
|-- minion.pem
|-- minion.pub
`-- minion_master.pub
[root@master1 master]# scp master.* 192.168.244.133:/etc/salt/pki/master
root@192.168.244.133's password:
master.pem 100% 1679 1.4MB/s 00:00
master.pub 100% 451 383.6KB/s 00:00
在备机上启动服务
[root@master2 ~]# systemctl start salt-master
修改node2salt-minion配置文件
#master: salt
master:
- 192.168.244.133 备
重启服务
[root@node2 ~]# systemctl restart salt-minion
在备机上查看
[root@master2 ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
node2
Rejected Keys:
[root@master2 ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
node2
Proceed? [n/Y] y
Key for minion node2 accepted.
[root@master2 ~]# salt-key -L
Accepted Keys:
node2
Denied Keys:
Unaccepted Keys:
Rejected Keys:
测试
[root@master2 ~]# salt '*' test.ping
node2:
True
测试完成
配置node2 salt-minion
[root@node2 ~]# vim /etc/salt/minion
.....
#master: salt
master:
- 192.168.244.131
- 192.168.244.133
......
# master_type: str
master_type: failover
.....
# master_alive_interval: 30
master_alive_interval: 15
重启服务
[root@node2 ~]# systemctl restart salt-minion
测试
在主上面ping
[root@master1 ~]# salt '*' test.ping
node2:
True
在备上面ping
[root@master2 ~]# salt '*' test.ping
node2:
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 20211129163346789230
停掉主上面的master
[root@master1 ~]# systemctl stop salt-master
在备上面ping
[root@master2 ~]# salt '*' test.ping
node2:
True
两个服务不能同时开启
查看node2上salt-minion状态
[root@node2 ~]# 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 Tue 2021-11-30 00:31:23 CST; 5min ago
Docs: man:salt-minion(1)
file:///usr/share/doc/salt/html/contents.html
https://docs.saltproject.io/en/latest/contents.html
Main PID: 339525 (salt-minion)
Tasks: 6 (limit: 11301)
Memory: 93.8M
CGroup: /system.slice/salt-minion.service
├─339525 /usr/bin/python3.6 /usr/bin/salt-minion
├─339554 /usr/bin/python3.6 /usr/bin/salt-minion
└─339556 /usr/bin/python3.6 /usr/bin/salt-minion
Nov 30 00:31:23 node2 systemd[1]: Stopped The Salt Minion.
Nov 30 00:31:23 node2 systemd[1]: Starting The Salt Minion...
Nov 30 00:31:23 node2 systemd[1]: Started The Salt Minion.
Nov 30 00:31:44 node2 salt-minion[339525]: [CRITICAL] 'master_type' set to 'failover' but 'retry_dns' is not 0. Setting '>
Nov 30 00:33:56 node2 salt-minion[339525]: [WARNING ] Master ip address changed from 192.168.244.131 to 192.168.244.133
Nov 30 00:33:56 node2 salt-minion[339525]: [WARNING ] Master ip address changed from 192.168.244.131 to 192.168.244.133
把主机上的文件复制到备机上
[root@master1 ~]# scp -r /srv/* 192.168.244.133:/srv
salt-syndic分布式架构
分布在不同地理位置的服务器,需要一个“一个人”来管理它就是salt-syndic,
salt-syndic控制着属于自己管辖范围内的salt-minion。所以master只需管理salt-syndic,就可以间接控制所有salt-minion,这样的好处是减轻master的负荷。
salt-syndic架构图

salt-syndic的优劣势
优势:
- 可以通过syndic实现更复杂的salt架构
- 减轻master的负担
劣势:
- syndic的/srv目录下的salt和pillar目录内容要与最顶层的master下的一致,所以要进行数据同步,同步方案同salt-master高可用
- 最顶层的master不知道自己有几个syndic,它只知道自己有多少个minion,并不知道这些minion是由哪些syndic来管理的
salt-syndic部署
环境:
| 主机名 | IP地址 | 服务 |
|---|---|---|
| master1 | 192.168.244.131 | salt-master |
| master2 | 192.168.244.133 | salt-master salt-syndic |
| node1 | 192.168.244.135 | salt-minion |
| node2 | 192.168.244.138 | salt-minion |
在对应的主机安装相应的服务
[root@master1 ~]# yum -y install salt-master
[root@master2 ~]# yum -y install salt-master salt-syndic
[root@node1 ~]# yum -y install salt-minion
[root@node2 ~]# yum -y install salt-minion
修改master1上面的master文件
[root@master1 ~]# vim /etc/salt/master
.......
#order_masters: False
order_masters: true
.....
启动或重启服务
[root@master1 ~]# systemctl restart salt-master
配置master2上的syndic
修改syndic所在主机的master配置文件
- 取消注释syndic_master
- 将syndic_master的值设为master的IP
[root@master2 ~]# vim /etc/salt/master
.......
#syndic_master: masterofmasters
syndic_master: 192.168.244.131
.....
重启服务
[root@master2 ~]# systemctl restart salt-master
[root@master2 ~]# systemctl restart salt-syndic
.....
配置minion1
配置minion,将master指向syndic所在主机
[root@node1 ~]# vim /etc/salt/minion
.......
#master: salt
master: 192.168.244.133
......
配置minion2
[root@node2 ~]# vim /etc/salt/minion
.......
#master: salt
master: 192.168.244.133
....
重启服务 minion1、minio2
[root@node1 ~]# systemctl restart salt-minion
[root@node2 ~]# systemctl restart salt-minion
在syndic上接受minion主机的key
[root@master2 ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
node1
node2
Rejected Keys:
[root@master2 ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
node1
node2
Proceed? [n/Y] y
Key for minion node1 accepted.
Key for minion node2 accepted.
[root@master2 ~]# salt-key -L
Accepted Keys:
node1
node2
Denied Keys:
Unaccepted Keys:
Rejected Keys:
在master上接受syndic主机的key
[root@master1 ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
master2
Rejected Keys:
[root@master1 ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
master2
Proceed? [n/Y] y
Key for minion master2 accepted.
[root@master1 ~]# salt-key -L
Accepted Keys:
master2
Denied Keys:
Unaccepted Keys:
Rejected Keys:
在master上执行模块或状态检验有几个minion应答
[root@master1 ~]# salt '*' test.ping
node2:
True
node1:
True
eys:
node1
node2
Denied Keys:
Unaccepted Keys:
Rejected Keys:
#### 在master上接受syndic主机的key
[root@master1 ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
master2
Rejected Keys:
[root@master1 ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
master2
Proceed? [n/Y] y
Key for minion master2 accepted.
[root@master1 ~]# salt-key -L
Accepted Keys:
master2
Denied Keys:
Unaccepted Keys:
Rejected Keys:
#### 在master上执行模块或状态检验有几个minion应答
[root@master1 ~]# salt ‘*’ test.ping
node2:
True
node1:
True
本文介绍了SaltStack的masterless模式,适用于网络不稳定或仅有一台主机的情况,详细讲解了如何配置masterless模式,包括修改minion配置和使用salt-call。此外,还探讨了salt-master的高可用配置和数据同步方法。最后,解释了salt-syndic分布式架构,其优点和缺点,并展示了部署步骤。
1247

被折叠的 条评论
为什么被折叠?



