1. SaltStack介绍
SALTSTACK COMPONENTS 组件
SaltStack Fundamentals 基本
Agentless SaltStack 无代理
Cofiguration Management 配置管理
Event Driven Infrastructure 核心驱动
1.1 自动化运维工具
目前常见的运维自动化管理工具主要是Puppet、SaltStack、Ansible下面分别简述它们的特点。
1、Puppet特点
Puppet是早期的Linux自动化运维工具,是一种LINUX、WINDOWS、UNIX平台的集中配置管理系统,到现在已经非常成熟,可以批量管理远程服务器,模块丰富,配置复杂,基于Ruby语言编写。是最典型的C/S结构,需要安装服务端和客户端 。
Puppet采用C/S星状的结构,所有的客户端和一个或者多个服务器交互,每个客户端周期地(默认半个小时)向服务器发送请求,获得最新的配置信息,保证和配置信息同步。
每个Puppet客户端周期地连接一次服务器,下载最新的配置文件,并且严格按照配置文件来配置客户端。配置完成后,Puppet客户端可以反馈给服务器端一个消息,如果出错也会给服务器端反馈一个消息。
Puppet适用于服务器管理的整个过程,比如初始安装、配置、更新等
2、SaltStack特点
SaltStack和Puppet一样,也是C/S模式,需要安装服务端和客户端,基于Python编写,加入了MQ消息同步,可以使执行命令和执行结果高效返回,但其执行过程需要等待客户端全部返回,如果客户端没有及时返回或者没有响应的话,可能会导致部分机器没有执行结果。
3、Ansible特点
Ansible和SaltStack一样是基于Python开发,Ansible只需要在一台普通的服务器上运行即可,不需要在客户端服务器上安装客户端。因为Ansible基于SSH远程管理,而Linux服务器大部分都离不开SSH,所以Ansible不需要为配置添加额外的支持。
Ansible安装使用都很简单,而且基于上千个插件和模块,实现各种软件、平台、版本的管理,支持虚拟容器多层级的部署。
有时候会觉得Ansible比SaltStack执行效率慢,其实并不是软件本身的问题,二是由于SSH服务慢,可以通过优化SSH连接速度和使用Ansible加速模块提高效率.
1.2 saltstack的特点
- 基于python开发的C/S架构配置管理工具
- 底层使用ZeroMQ消息队列pub/sub方式通信
- 使用SSL证书签发的方式进行认证管理,传输采用AES加密
1.3 saltstack服务架构
服务架构
saltstack是基于C/S服务模式,在该架构中,服务器端Master,客户端Minion。在saltstack架构中,不仅有传统的C/S服务模式,而且有消息队列中的发布与订阅(pub/sub)服务模式。目前我们一般用其C/S架构做批量管理。
minion与master通信模式如下图:
Master与Minion的连接
在master和minion端都是以守护进程的模式运行。SaltStack master启动后默认监听4505和4506两个端口。4505(publish_port)为saltstack的消息发布系统,4506(ret_port)为saltstack客户端与服务端通信的端口。如果使用lsof 查看4505端口,会发现所有的minion在4505端口持续保持在ESTABLISHED状态。整个通信过程可以用debug查看。
master:
salt-master –l debug
ss –a | grep '4505|4506'
minion:
salt-minion –l debug
2. SaltStack四大功能与四大运行方式
SaltStack有四大功能,分别是:
- 远程执行
- 配置管理/状态管理
- 云管理(cloud)
- 事件驱动
SaltStack可以通过远程执行实现批量管理,并且通过描述状态来达到实现某些功能的目的。
SaltStack四大运行方式:
- local本地运行
- Master/Minion传统方式
- Syndic分布式
- Salt ssh
3. 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命令。 |
4. SaltStack安装与最小化配置
环境说明:
centos8
主机名 | IP地址 | 部署功能 | 性能 |
---|---|---|---|
master | 192.168.143.101 | salt-master salt-minion | 4核8G |
minion | 192.168.143.102 | salt-minion | 2核4G |
官方yum源地址:https://repo.saltstack.com
4.1 在控制机上安装saltstack主控端软件
#关闭防火墙、selinux
systemctl disable --now firewalld
sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
reboot
[root@master ~]# ls /etc/yum.repos.d/
CentOS-Linux-AppStream.repo
CentOS-Linux-BaseOS.repo
CentOS-Linux-ContinuousRelease.repo
CentOS-Linux-Debuginfo.repo
CentOS-Linux-Devel.repo
CentOS-Linux-Extras.repo
CentOS-Linux-FastTrack.repo
CentOS-Linux-HighAvailability.repo
CentOS-Linux-Media.repo
CentOS-Linux-Plus.repo
CentOS-Linux-PowerTools.repo
CentOS-Linux-Sources.repo
[root@master ~]# sudo rpm --import https://repo.saltproject.io/py3/redhat/8/x86_64/latest/SALTSTACK-GPG-KEY.pub
[root@master ~]# 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 ~]# ls /etc/yum.repos.d/
CentOS-Linux-AppStream.repo
CentOS-Linux-BaseOS.repo
CentOS-Linux-ContinuousRelease.repo
CentOS-Linux-Debuginfo.repo
CentOS-Linux-Devel.repo
CentOS-Linux-Extras.repo
CentOS-Linux-FastTrack.repo
CentOS-Linux-HighAvailability.repo
CentOS-Linux-Media.repo
CentOS-Linux-Plus.repo
CentOS-Linux-PowerTools.repo
CentOS-Linux-Sources.repo
salt.repo
[root@master ~]# yum clean all
21 文件已删除
[root@master ~]# dnf -y install salt-master salt-minion
[root@master ~]# cd /etc/salt/
[root@master salt]# ls
cloud cloud.maps.d master minion.d proxy.d
cloud.conf.d cloud.profiles.d master.d pki roster
cloud.deploy.d cloud.providers.d minion proxy
[root@master salt]# vim minion
...
#master: salt
master: 192.168.143.101
...
[root@master salt]# systemctl enable --now salt-master.service
Created symlink /etc/systemd/system/multi-user.target.wants/salt-master.service → /usr/lib/systemd/system/salt-master.service.
[root@master salt]# systemctl enable --now salt-minion.service
Created symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service → /usr/lib/systemd/system/salt-minion.service.
[root@master salt]# ss -atnl
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客户端
[root@master ~]# scp /etc/yum.repos.d/salt.repo 192.168.143.102:/etc/yum.repos.d/
The authenticity of host '192.168.143.102 (192.168.143.102)' can't be established.
ECDSA key fingerprint is SHA256:aoqmVdzqpbos7N9l6RpRLBbT+g7cVDWT2Yu5AiZ4xVY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.143.102' (ECDSA) to the list of known hosts.
root@192.168.143.102's password:
salt.repo 100% 292 231.0KB/s 00:00
#关闭防火墙、selinux
systemctl disable --now firewalld
sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
reboot
[root@minion ~]# ls /etc/yum.repos.d/
CentOS-Linux-AppStream.repo
CentOS-Linux-BaseOS.repo
CentOS-Linux-ContinuousRelease.repo
CentOS-Linux-Debuginfo.repo
CentOS-Linux-Devel.repo
CentOS-Linux-Extras.repo
CentOS-Linux-FastTrack.repo
CentOS-Linux-HighAvailability.repo
CentOS-Linux-Media.repo
CentOS-Linux-Plus.repo
CentOS-Linux-PowerTools.repo
CentOS-Linux-Sources.repo
salt.repo
[root@minion ~]# yum clean all
26 文件已删除
[root@minion ~]# dnf -y install salt-minion
[root@minion ~]# cd /etc/salt/
[root@minion salt]# ls
cloud cloud.maps.d master minion.d proxy.d
cloud.conf.d cloud.profiles.d master.d pki roster
cloud.deploy.d cloud.providers.d minion proxy
[root@minion salt]# vim minion
...
#master: salt
master: 192.168.143.101
...
[root@minion salt]# systemctl enable --now salt-minion.service
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与外部系统进行整合的相关配置参数
5. 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@master ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
192.168.69.201
192.168.69.202
Rejected Keys:
//接受指定minion的新证书
[root@master ~]# salt-key -ya 192.168.69.202
The following keys are going to be accepted:
Unaccepted Keys:
192.168.69.202
Key for minion 192.168.69.202 accepted.
[root@localhost ~]# salt-key -L
Accepted Keys:
192.168.69.202
Denied Keys:
Unaccepted Keys:
192.168.69.201
Rejected Keys:
//接受所有minion的新证书
[root@master ~]# salt-key -yA
The following keys are going to be accepted:
Unaccepted Keys:
192.168.69.201
Key for minion 192.168.69.201 accepted.
[root@localhost ~]# salt-key -L
Accepted Keys:
192.168.69.201
192.168.69.202
Denied Keys:
Unaccepted Keys:
Rejected Keys:
#实例
[root@master ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
master
minion
Rejected Keys:
[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-key -L
Accepted Keys:
minion
Denied Keys:
Unaccepted Keys:
master
Rejected Keys:
[root@master ~]# salt '*' test.ping
minion:
True
[root@master ~]# #salt-key -ya master//接受指定证书
[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 '*' test.ping
minion:
True
master:
True
#master结构对比公钥
[root@master salt]# tree pki
pki
├── master
│ ├── master.pem
│ ├── master.pub
│ ├── minions
│ │ ├── master
│ │ └── minion
│ ├── minions_autosign
│ ├── minions_denied
│ ├── minions_pre
│ └── minions_rejected
└── minion
├── minion_master.pub
├── minion.pem
└── minion.pub
7 directories, 7 files
[root@master salt]# cat pki/master/minions/minion
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA420yLF4To6/gc0Y3yQRp
VnhI1eYbBDO+0j9GecmoBxm1VTj7J/GU6O3jvDA/PWU8f/WvmsyrT0T0FzWmpH9Z
WhM4oLwk/5WMYhlrQ9LWxQt1G5t9CFSINQxtwGQm6jpMhQh8oMM5ifNQ1iJKQBcx
tCr4jtsir55MijVpOr+jqJG8XG4yfK3Xsw1iaWnEuBGBbtxU0rHIUiJrOPY2Sqnp
fJ9aVwa8PU9obsjlMhFtA2WYGkyQzZyOSbOJN9ahJHeCDem/humqIaMnFnhcJu6t
ESOa8y5d4U/InBOuICxwytcbi3lGbe5FBMs7/5AtdaV1Yv8LinEb7LoUWtU/PX5s
kQIDAQAB
-----END PUBLIC KEY-----
[root@minion salt]# tree pki
pki
├── master
└── minion
├── minion_master.pub
├── minion.pem
└── minion.pub
2 directories, 3 files
#minion结构对比公钥
[root@minion salt]# cat pki/minion/minion.pub
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA420yLF4To6/gc0Y3yQRp
VnhI1eYbBDO+0j9GecmoBxm1VTj7J/GU6O3jvDA/PWU8f/WvmsyrT0T0FzWmpH9Z
WhM4oLwk/5WMYhlrQ9LWxQt1G5t9CFSINQxtwGQm6jpMhQh8oMM5ifNQ1iJKQBcx
tCr4jtsir55MijVpOr+jqJG8XG4yfK3Xsw1iaWnEuBGBbtxU0rHIUiJrOPY2Sqnp
fJ9aVwa8PU9obsjlMhFtA2WYGkyQzZyOSbOJN9ahJHeCDem/humqIaMnFnhcJu6t
ESOa8y5d4U/InBOuICxwytcbi3lGbe5FBMs7/5AtdaV1Yv8LinEb7LoUWtU/PX5s
kQIDAQAB
-----END PUBLIC KEY-----
#master从未认证,到拒绝主机minions_pre > 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_master.pub
├── minion.pem
└── minion.pub
7 directories, 7 files
#minion id命名的公钥
[root@master salt]# ls
cloud cloud.maps.d master minion.d proxy
cloud.conf.d cloud.profiles.d master.d minion_id proxy.d
cloud.deploy.d cloud.providers.d minion pki roster
[root@master salt]# vim minion
...
#id:
id: 192.168.143.101 //添加为id,不添加为主机名
...
#minion对比公钥文件
[root@minion salt]# tree pki/
pki/
├── master
└── minion
├── minion_master.pub
├── minion.pem
└── minion.pub
2 directories, 3 files
#.pub文件内容不一样
[root@minion salt]# vimdiff pki/minion/minion_master.pub pki/minion/minion.pub
6. SaltStack远程执行
#实例
[root@master salt]# salt-key -L
Accepted Keys:
minion
Denied Keys:
Unaccepted Keys:
Rejected Keys:
master
[root@master salt]# salt '*' test.ping
minion:
True
[root@master salt]# tree pki/
pki/
├── master
│ ├── master.pem
│ ├── master.pub
│ ├── minions
│ │ └── minion
│ ├── minions_autosign
│ ├── minions_denied
│ ├── minions_pre
│ └── minions_rejected
│ └── master
└── minion
├── minion_master.pub
├── minion.pem
└── minion.pub
7 directories, 7 files
[root@master salt]# mv pki/master/minions_rejected/master pki/master/minions_pre/
[root@master salt]# salt-key -L
Accepted Keys:
minion
Denied Keys:
Unaccepted Keys:
master
Rejected Keys:
[root@master salt]# salt-key -yA
The following keys are going to be accepted:
Unaccepted Keys:
master
Key for minion master accepted.
[root@master salt]# salt-key -L
Accepted Keys:
master
minion
Denied Keys:
Unaccepted Keys:
Rejected Keys:
#如果一直ping不通重新启动主机
[root@master pki]# salt '*' test.ping
master:
True
minion:
True
7. 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 //组匹配
//此处的centos是一个组名,需要在master配置文件中定义nodegroups参数,且需要知道minion的id信 \
//息才能将其定义至某个组中
-R //范围匹配
-C //综合匹配(指定多个匹配)
-I //pillar值匹配
#实例:
[root@master pki]# salt --version
salt 3004
[root@master pki]# salt --versions-report
Salt Version:
Salt: 3004
Dependency Versions:
cffi: Not Installed
cherrypy: Not Installed
dateutil: 2.6.1
docker-py: Not Installed
gitdb: Not Installed
gitpython: Not Installed
Jinja2: 2.10.1
libgit2: Not Installed
M2Crypto: 0.35.2
Mako: Not Installed
msgpack: 0.6.2
msgpack-pure: Not Installed
mysql-python: Not Installed
pycparser: Not Installed
pycrypto: Not Installed
pycryptodome: Not Installed
pygit2: Not Installed
Python: 3.6.8 (default, Mar 19 2021, 05:13:41)
python-gnupg: Not Installed
PyYAML: 3.12
PyZMQ: 19.0.0
smmap: Not Installed
timelib: Not Installed
Tornado: 4.5.3
ZMQ: 4.3.4
System Versions:
dist: centos 8
locale: UTF-8
machine: x86_64
release: 4.18.0-305.3.1.el8.x86_64
system: Linux
version: CentOS Linux 8
[root@master pki]# salt -h
Usage: salt [options] '<target>' <function> [arguments]
...
[root@master pki]# salt -E 'm*' test.ping
master:
True
minion:
True
[root@master pki]# salt -S '192.168.143.0/24' test.ping
master:
True
minion:
True
[root@master pki]# salt -G 'os:centos' test.ping
minion:
True
master:
True
# 把名字修改为ip地址,使用ip地址ping
[root@minion salt]# ls
cloud cloud.maps.d master minion.d proxy
cloud.conf.d cloud.profiles.d master.d minion_id proxy.d
cloud.deploy.d cloud.providers.d minion pki roster
[root@minion salt]# vim minion
...
#id:
id: 192.168.143.102
...
[root@master pki]# salt-key -L
Accepted Keys:
master
minion
Denied Keys:
Unaccepted Keys:
192.168.143.102
Rejected Keys:
[root@master pki]# salt-key -yA
The following keys are going to be accepted:
Unaccepted Keys:
192.168.143.102
Key for minion 192.168.143.102 accepted.
[root@master pki]# salt-key -L
Accepted Keys:
192.168.143.102
master
minion
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@master pki]# salt "192.168.143.102" test.ping
192.168.143.102:
True
[root@master pki]# salt -C 'G@os:centos or L@192.168.143.102' test.ping
master:
True
192.168.143.102:
True