文章目录
一、SaltStack介绍
1.1 自动化运维工具
作为一个运维人员,很大一部分工作是在业务的配置管理和状态维护以及版本发布上,而当业务场景及公司规模上了一定规模后,人为手工的去做这些工作将变得极其困难,此时我们将需要利用一些自动化运维的工具来达到批量管理的目的。
常用的自动化运维工具有:
- puppet
- ansible
- saltstack
此三款属同类工具,皆可用来提高运维管理的效率,但它们又各有优势,目前主流的自动化运维工具是ansible和saltstack。其中ansible无需安装客户端,这是其最大的优势,而saltstack则需要安装客户端工具,类似zabbix的agent。应用场景方面,ansible常用于小型企业,而saltstack则常用于中大型企业,因为ansible无法并行执行而saltstack可以并行。但不论其特点如何,本质上均属同类,所以只需要掌握一种即可轻松胜任运维工作。
可以将SaltStack理解为神笔马良的那只笔!
1.2 saltstack的特点
- 基于python开发的C/S架构配置管理工具
- 底层使用ZeroMQ消息队列pub/sub方式通信
- 使用SSL证书签发的方式进行认证管理,传输采用AES加密
1.3 saltstack服务架构
在saltstack架构中服务器端叫Master,客户端叫Minion。
在Master和Minion端都是以守护进程的模式运行,一直监听配置文件里面定义的ret_port(接受minion请求)和publish_port(发布消息)的端口。
当Minion运行时会自动连接到配置文件里面定义的Master地址ret_port端口进行连接认证。
saltstack除了传统的C/S架构外,其实还有一种叫做masterless的架构,其不需要单独安装一台 master 服务器,只需要在每台机器上安装 Minion端,然后采用本机只负责对本机的配置管理机制服务的模式。
二、SaltStack四大功能与四大运行方式
SaltStack有四大功能,分别是:
- 远程执行
- 配置管理/状态管理
- 云管理(cloud)
- 事件驱动
SaltStack可以通过远程执行实现批量管理,并且通过描述状态来达到实现某些功能的目的。
SaltStack四大运行方式:
- local本地运行 (masterless 无网络模式)
- Master/Minion传统方式(默认使用这一种)
- Syndic分布式
- Salt ssh(agentless)
三、SaltStack组件介绍
组件 | 功能 |
---|---|
Salt Master | 用于将命令和配置发送到在受管系统上运行的Salt minion |
Salt Minions | 从Salt master接收命令和配置 |
Execution Modules | 从命令行针对一个或多个受管系统执行的临时命令。对…有用: 1. 实时监控,状态和库存 2. 一次性命令和脚本 3. 部署关键更新 |
Formulas (States) | 系统配置的声明性或命令式表示 |
Grains Grains | 是有关底层受管系统的静态信息,包括操作系统,内存和许多其他系统属性。 |
Pillar | 用户定义的变量。这些安全变量被定义并存储在Salt Master中,然后使用目标“分配”给一个或多个Minion。 Pillar数据存储诸如端口,文件路径,配置参数和密码之类的值。 |
Top File | 将Formulas (States)和Salt Pillar数据与Salt minions匹配。 |
Runners | 在Salt master上执行的模块,用于执行支持任务。Salt runners报告作业状态,连接状态,从外部API读取数据,查询连接的Salt minions等。 |
Returners | 将Salt minions返回的数据发送到另一个系统,例如数据库。Salt Returners可以在Salt minion或Salt master上运行。 |
Reactor | 在SaltStack环境中发生事件时触发反应。 |
Salt Cloud / Salt Virt | 在云提供商/虚拟机管理程序上提供系统,并立即将其置于管理之下。 |
Salt SSH | 在没有Salt minion的系统上通过SSH运行Salt命令。 |
四、SaltStack安装
环境说明:
主机名字 | 主机类型 | IP | 要安装的应用 |
---|---|---|---|
master | 控制机 | 192.168.91.135 | salt-master salt-minion |
minion | 被控机 | 192.168.91.137 | salt-minion |
4.1 在控制机上安装saltstack主控端软件
// 关闭防火墙和selinux
[root@master ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]# vi /etc/selinux/config
SELINUX=disabled
[root@master ~]# setenforce 0
// 配置yum源
[root@master ~]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# ls
CentOS-Stream-AppStream.repo
CentOS-Stream-BaseOS.repo
CentOS-Stream-Debuginfo.repo
CentOS-Stream-Extras.repo
CentOS-Stream-HighAvailability.repo
CentOS-Stream-Media.repo
CentOS-Stream-PowerTools.repo
CentOS-Stream-RealTime.repo
[root@master 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
[salt-latest-repo]
name=Salt repo for RHEL/CentOS 8 PY3
baseurl=https://repo.saltproject.io/py3/redhat/8/x86_64/latest
skip_if_unavailable=True
failovermethod=priority
enabled=1
enabled_metadata=1
gpgcheck=1
gpgkey=https://repo.saltproject.io/py3/redhat/8/x86_64/latest/SALTSTACK-GPG-KEY.pub
// 清理缓存
[root@master yum.repos.d]# yum clean all
Failed to set locale, defaulting to C.UTF-8
21 files removed
[root@master yum.repos.d]# ls
CentOS-Stream-AppStream.repo
CentOS-Stream-BaseOS.repo
CentOS-Stream-Debuginfo.repo
CentOS-Stream-Extras.repo
CentOS-Stream-HighAvailability.repo
CentOS-Stream-Media.repo
CentOS-Stream-PowerTools.repo
CentOS-Stream-RealTime.repo
salt.repo
// 安装saltstack主控端
[root@master yum.repos.d]# cd
[root@master ~]# dnf -y install salt-master salt-minion
安装过程略....
// 修改主控端的配置文件
[root@master ~]# vim /etc/salt/master
#master: salt
master: 192.168.91.135 // 在#master后面加上这一行,指向控制主机IP地址
// 启动主控端的salt-master和salt-master,并设置开机自启
[root@master ~]# systemctl start salt-master
[root@master ~]# systemctl start salt-minion
[root@master ~]# systemctl enable --now salt-master
Created symlink /etc/systemd/system/multi-user.target.wants/salt-master.service → /usr/lib/systemd/system/salt-master.service.
[root@master ~]# systemctl enable --now salt-minion
Created symlink /etc/systemd/system/multi-user.target.wants/salt-master.service → /usr/lib/systemd/system/salt-minion.service.
// 查看有没有4405、4406端口号,验证启动是否成功
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
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 [::]:*
4.2 在被控机上安装salt-minion客户端
// 关闭防火墙和selinux
[root@minion ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@minion ~]# vi /etc/selinux/config
SELINUX=disabled
[root@minion ~]# setenforce 0
// 传yun源到被控机
[root@master ~]# scp /etc/yum.repos.d/salt.repo 192.168.91.137:/etc/yum.repos.d/
The authenticity of host '192.168.91.137 (192.168.91.137)' can't be established.
ECDSA key fingerprint is SHA256:CHl+F/ZU1pC0SQ7a5/IlsoVQoRXN/TMa3ikGYM1hsoA.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.91.137' (ECDSA) to the list of known hosts.
root@192.168.91.137's password:
salt.repo 100% 292 95.2KB/s 00:00
[root@minion ~]# cd /etc/yum.repos.d/
[root@minion yum.repos.d]# ls
CentOS-Stream-AppStream.repo
CentOS-Stream-BaseOS.repo
CentOS-Stream-Debuginfo.repo
CentOS-Stream-Extras.repo
CentOS-Stream-HighAvailability.repo
CentOS-Stream-Media.repo
CentOS-Stream-PowerTools.repo
CentOS-Stream-RealTime.repo
salt.repo
[root@minion yum.repos.d]# cd
[root@minion ~]# dnf -y install salt-minion
安装过程略....
// 修改被控端的配置文件
[root@minion ~]# vim /etc/salt/master
#master: salt
master: 192.168.91.135 // 在#master后面加上这一行,指向控制主机IP地址
// 启动受控端的salt-minion并设置开机自启
[root@minion ~]# systemctl start --now salt-minion
[root@minion ~]# systemctl enable --now salt-minion
Created symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service → /usr/lib/systemd/system/salt-minion.service.
4.3 saltstack配置文件
saltstack的配置文件在/etc/salt目录
saltstack配置文件说明:
配置文件 | 说明 |
---|---|
/etc/salt/master | 主控端(控制端)配置文件 |
/etc/salt/minion | 受控端配置文件 |
配置文件/etc/salt/master默认的配置就可以很好的工作,故无需修改此配置文件。
配置文件/etc/salt/minion常用配置参数
- master:设置主控端的IP
- id:设置受控端本机的唯一标识符,可以是ip也可以是主机名或自取某有意义的单词
在日常使用过程中,经常需要调整或修改Master配置文件,SaltStack大部分配置都已经指定了默认值,只需根据自己的实际需求进行修改即可。下面的几个参数是比较重要的
- max_open_files:可根据Master将Minion数量进行适当的调整
- timeout:可根据Master和Minion的网络状况适当调整
- auto_accept和autosign_file:在大规模部署Minion时可设置自动签证
- master_tops和所有以external开头的参数:这些参数是SaltStack与外部系统进行整合的相关配置参数
五、SaltStack认证机制
saltstack主控端是依靠openssl证书来与受控端主机认证通讯的,受控端启动后会发送给主控端一个公钥证书文件,在主控端用salt-key命令来管理证书。
salt-minion与salt-master的认证过程:
- minion在第一次启动时,会在/etc/salt/pki/minion/下自动生成一对密钥,然后将公钥发给master
- master收到minion的公钥后,通过salt-key命令接受该公钥。此时master的/etc/salt/pki/master/minions目录将会存放以minion id命名的公钥,然后master就能对minion发送控制指令了
salt-key常用选项
参数名 | 作用 |
---|---|
-L | 列出所有公钥信息 |
-a minion | 接受指定minion等待认证的key |
-A | 接受所有minion等待认证的key |
-r minion | 拒绝指定minion等待认证的key |
-R | 拒绝所有minion等待认证的key |
-f minion | 显示指定key的指纹信息 |
-F | 显示所有key的指纹信息 |
-d minion | 删除指定minion的key |
-D | 删除所有minion的key |
-y | 自动回答yes |
自动生成公钥
[root@minion ~]# cd /etc/salt
[root@minion salt]# dnf -y install tree
[root@minion salt]# tree pki/
pki/
|-- master
`-- minion
|-- minion.pem //
|-- minion.pub // 这两行就是自动生成的一对密钥
`-- minion_master.pub
[root@master salt]# tree pki/
pki/
|-- master
| |-- master.pem
| |-- master.pub
| |-- minions // 接受了就在这里
| | `-- minion // 这个就是接受了minion主机的公钥
| |-- minions_autosign
| |-- minions_denied // 拒绝就到这里来了
| |-- minions_pre // 密钥接受在这里
| | `-- master
| `-- minions_rejected // -r 指定拒绝到这里来
`-- minion
|-- minion.pem
`-- minion.pub
7 directories, 6 files
// 查看两边公钥对比一下
[root@master salt]# cat pki/master/minions/minion
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo/aG5BHskiYf7sM0WHEo
yMlP5maj5VuIwPE1ZbN0rrbK6T6WKjUuLBJiezEtsGGi7MwQJ/v7GqiyucSoyGX1
f9tBpoPy86US8iAdTXCtc6BXc0wzrcQWmH2BCQ0I+WrmKfhX4Q9OHJyqaJOM8eNn
s+uNjObMs2P/A0Ucc0hHS6l74KaslMqWpo/iQ8fEN6VUO1QwKQ/jcJpvlH9/SirV
obIcdDqwtQtLc3Oo1A6s/ejZ2tb4fI4i3Y3bpeM8sjP5CbABiTTXTdycsTnKmMfD
LCvcZb0WCxWQ2snJjwTxJk12hi5tTObraQYf/5Qwl3S3iVfOBztq7e0vbBqUvA5E
IQIDAQAB
-----END PUBLIC KEY-----
[root@minion salt]# cat pki/minion/minion.pub
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo/aG5BHskiYf7sM0WHEo
yMlP5maj5VuIwPE1ZbN0rrbK6T6WKjUuLBJiezEtsGGi7MwQJ/v7GqiyucSoyGX1
f9tBpoPy86US8iAdTXCtc6BXc0wzrcQWmH2BCQ0I+WrmKfhX4Q9OHJyqaJOM8eNn
s+uNjObMs2P/A0Ucc0hHS6l74KaslMqWpo/iQ8fEN6VUO1QwKQ/jcJpvlH9/SirV
obIcdDqwtQtLc3Oo1A6s/ejZ2tb4fI4i3Y3bpeM8sjP5CbABiTTXTdycsTnKmMfD
LCvcZb0WCxWQ2snJjwTxJk12hi5tTObraQYf/5Qwl3S3iVfOBztq7e0vbBqUvA5E
IQIDAQAB // 发现公钥是一样的的
-----END PUBLIC KEY-----
// 使用-r 指定拒绝 master 之后就到minions_rejected里面来了
[root@master salt]# salt-key -r master
The following keys are going to be rejected:
Unaccepted Keys:
master
Proceed? [n/Y] Y
Key for minion master rejected.
[root@master salt]# tree pki
pki
|-- master
| |-- master.pem
| |-- master.pub
| |-- minions
| | `-- minion
| |-- minions_autosign
| |-- minions_denied
| |-- minions_pre
| `-- minions_rejected
| `-- master
`-- minion
|-- minion.pem
`-- minion.pub
7 directories, 6 files
// 如果想继续接受master,就移动回去
[root@master salt]# mv pki/master/minions_rejected/master pki/master/minions_pre/
[root@master salt]# tree pki
pki
|-- master
| |-- master.pem
| |-- master.pub
| |-- minions
| | `-- minion
| |-- minions_autosign
| |-- minions_denied
| |-- minions_pre
| | `-- master
| `-- minions_rejected
`-- minion
|-- minion.pem
`-- minion.pub
7 directories, 6 files
部分参数举例
// 查看所有公钥key
[root@master ~]# salt-key -L
Accepted Keys: // 接受的key
Denied Keys: // 拒绝的key
Unaccepted Keys: // 没有接受的key
master
minion
Rejected Keys: // 拒绝接受的key
// 接受指定的minion主机的key
[root@master ~]# salt-key -a minion
The following keys are going to be accepted:
Unaccepted Keys:
minion
Proceed? [n/Y] Y
Key for minion minion accepted.
[root@master salt]# salt-key -L
Accepted Keys:
minion
Denied Keys:
Unaccepted Keys:
master
Rejected Keys:
// 控制minion主机
[root@master ~]# salt '*' test.ping
minion:
True // 成功
// 接受所有主机的key
[root@master ~]# salt-key -yA
The following keys are going to be accepted:
Unaccepted Keys:
master
Key for minion master accepted.
[root@master ~]# salt-key -L
Accepted Keys:
master
minion
Denied Keys:
Unaccepted Keys:
Rejected Keys:
证书移动
[root@master salt]# tree pki/
pki/
|-- master
| |-- master.pem
| |-- master.pub
| |-- minions
| | |-- master
| | `-- minion
| |-- minions_autosign
| |-- minions_denied
| |-- minions_pre
| `-- minions_rejected
`-- minion
|-- minion.pem
|-- minion.pub
`-- minion_master.pub
// 因为转移文件到了拒绝接受key的目录下面
[root@master salt]# mv /etc/salt/pki/master/minions/minion /etc/salt/pki/master/minions_rejected/
[root@master salt]# tree pki/
pki/
|-- master
| |-- master.pem
| |-- master.pub
| |-- minions
| | `-- master
| |-- minions_autosign
| |-- minions_denied
| |-- minions_pre
| `-- minions_rejected
| `-- minion
`-- minion
|-- minion.pem
|-- minion.pub
`-- minion_master.pub
7 directories, 7 files
// 所以无法ping通
[root@master salt]# salt "minion" test.ping
No minions matched the target. No command was sent, no jid was assigned.
ERROR: No return received
// 文件移动回去就可ping通
[root@master salt]# mv /etc/salt/pki/master/minions_rejected/minion /etc/salt/pki/master/minions/
[root@master salt]# tree pki/pki/
|-- master
| |-- master.pem
| |-- master.pub
| |-- minions
| | |-- master
| | `-- minion
| |-- minions_autosign
| |-- minions_denied
| |-- minions_pre
| `-- minions_rejected
`-- minion
|-- minion.pem
|-- minion.pub
`-- minion_master.pub
[root@master salt]# salt "minion" test.ping
minion:
True
修改id,指定IP访问
// 列出所有公钥
[root@master ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
master
minion
Rejected Keys:
// 指定删除minion主机
[root@master ~]# salt-key -yd minion
The following keys are going to be deleted:
Accepted Keys:
minion
Key for minion minion deleted.
[root@master ~]# salt-key -L
Accepted Keys:
master
Denied Keys:
Unaccepted Keys:
Rejected Keys:
// 指定master主机认证
[root@master ~]# salt-key -ya "master"
The following keys are going to be accepted:
Unaccepted Keys:
master
Key for minion master accepted
[root@master ~]# salt-key -L
Accepted Keys:
master
Denied Keys:
Unaccepted Keys:
Rejected Keys:
// 删除被控机的密钥
[root@minion ~]# cd /etc/salt/pki/minion/
[root@minion minion]# ls
minion.pem minion.pub minion_master.pub
[root@minion minion]# rm -f *
[root@minion minion]# ls
// 修改id
[root@minion minion]# vim /etc/salt/minion
#id:
id: 192.168.91.137 // 添加被控机的IP地址
// 重新启动
[root@minion minion]# systemctl restart --now salt-minion
// 自动生成证书
[root@minion minion]# ls
minion.pem minion.pub
// 查看ip命名的主机是否存在
[root@master ~]# salt-key -L
Accepted Keys:
master
Denied Keys:
Unaccepted Keys:
192.168.91.137
Rejected Keys:
// 认证该主机
[root@master ~]# salt-key -ya 192.168.91.137
The following keys are going to be accepted:
Unaccepted Keys:
192.168.91.137
Key for minion 192.168.91.137 accepted.
// 查看认证情况
[root@master ~]# salt-key -L
Accepted Keys:
192.168.91.137
master
Denied Keys:
Unaccepted Keys:
Rejected Keys:
// ping所有主机
[root@master ~]# salt "*" test.ping
master:
True
192.168.91.137:
True
// 主控制机证书自动传到被控制机
[root@minion minion]# ls
minion.pem minion.pub minion_master.pub
六、SaltStack远程执行
// 测试指定受控端minion主机是否存活
[root@master ~]# salt "minion" test.ping
minion:
True
[root@master ~]# salt "minion" cmd.run hostname
minion:
minion
// 测试所有受控端主机是否存活
[root@master ~]# salt "*" test.ping
minion:
True
master:
True
七、salt命令使用
语法:salt [options] '<target>' <function> [arguments]
常用的options
参数名 | 作用 |
---|---|
–version | 查看saltstack的版本号 |
–versions-report | 查看saltstack以及依赖包的版本号 |
-h | 查看帮助信息 |
-c CONFIG_DIR | 指定配置文件目录(默认为/etc/salt/) |
-t TIMEOUT | 指定超时时间(默认是5s) |
–async | 异步执行 |
-v | verbose模式,详细显示执行过程 |
–username=USERNAME | 指定外部认证用户名 |
–password=PASSWORD | 指定外部认证密码 |
–log-file=LOG_FILE | 指定日志记录文件 |
常用target参数
参数名 | 作用 |
---|---|
-E | 正则匹配 |
-L | 列表匹配 |
-S | CIDR匹配网段 |
-G | grains匹配 |
–grain-pcre | grains加正则匹配 |
-N | 组匹配 |
-R | 范围匹配 |
-C | 综合匹配(指定多个匹配) |
-I | pillar值匹配 |
// 正则匹配
[root@master ~]# salt -E 'min*' test.ping
minion:
True
// 列表匹配
[root@master ~]# salt -L master,minion test.ping
master:
True
minion:
True
// 网段匹配
[root@master ~]# salt -S '192.168.91.0/24' test.ping
master:
True
minion:
True
// 根据系统匹配
[root@master ~]# salt -G 'os:CentOS Stream' test.pingmaster:
True
minion:
True
// 组匹配
[root@master ~]# vim /etc/salt/master
# as the main master config file).
default_include: master.d/*.conf // 取消注释
// 创建相应的目录及文件
[root@master ~]# mkdir -p /etc/salt/master.d/
[root@master ~]# touch /etc/salt/master.d/nodegroups.conf
添加新的组和组员(组后的主机,主机名要写salt-key接受的主机名.不需要重启master)
[root@master ~]# vim /etc/salt/master.d/nodegroups.conf
nodegroups:
group1: 'master'
[root@master ~]# salt -N 'group1' 'test.ping'
master:
True
// 组合匹配
[root@master ~]# salt -C 'L@master,minion or S@192.168.91.0/24' test.ping
minion:
True
master:
True
#G@os:CentOS Stream or L@192.168.91.137是一个复合组合,支持使用and和or关联多个条件
// 万能模块使用 -- cmd.run
这里使用了四台主机
[root@master ~]# salt-key -L
Accepted Keys:
192.168.91.137
master
node2
node3
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@master ~]# salt "*" test.ping
node2:
True
master:
True
192.168.91.137:
True
node3:
True
// 万能模块看运行时间
[root@master ~]# salt "*" cmd.run "uptime"
node2:
14:02:17 up 3:08, 2 users, load average: 0.14, 0.09, 0.03
192.168.91.137:
14:02:17 up 4:24, 2 users, load average: 0.18, 0.20, 0.09
master:
14:02:17 up 4:24, 2 users, load average: 0.24, 0.08, 0.02
node3:
14:02:17 up 3:01, 2 users, load average: 0.03, 0.05, 0.01
// 万能模块看日期
[root@master ~]# salt "*" cmd.run "date"
node2:
Tue Nov 2 14:04:40 CST 2021
node3:
Tue Nov 2 14:04:40 CST 2021
192.168.91.137:
Tue Nov 2 14:04:40 CST 2021
master:
Tue Nov 2 14:04:40 CST 2021