saltstack介绍
Salt,一种全新的基础设施管理方式,部署轻松,在几分钟内可运行起来,扩展性好,很容易管理上万台服务器,速度够快,服务器之间秒级通讯
Salt 命令以一致的数据结构返回结果,以便于使用和存储
saltstack的官方文档:https://docs.saltproject.io/en/getstarted/system/index.html
自动化运维工具
运维人员很大一部分工作是在业务的配置管理和状态维护以及版本发布上,而当业务场景及公司规模上了一定规模后,人为手工的去做这些工作将变得极其困难,此时我们将需要利用一些自动化运维的工具来达到批量管理的目的
常用的自动化运维工具:
puppet
ansible 需安装客户端,无法并行执行,常用于小型企业
saltstack 需要安装客户端工具(不是必须要求装,但是装了后提高效率),可以并行执行,常用于中小型企业
不论其特点如何,本质上均属同类,所以只需要掌握一种即可轻松胜任运维工作*
可以将SaltStack理解为神笔马良的那只笔,也就是说类似于状态管理工具,描绘出一个目标管理状态,将描绘的状态写入状态文件中去(举个例子:我想让apache处于停止状态就停止),拥有幂等性
saltstack的特点
基于python开发的C/S架构配置管理工具(状态管理工具)
底层使用ZeroMQ消息队列pub/sub方式通信(发布订阅)
使用SSL证书签发的方式进行认证管理,传输采用AES加密
服务架构
在saltstack架构中服务器端叫Master,客户端叫Minion
在Master和Minion端都是以守护进程的模式运行(就是后台运行),一直监听配置文件里面定义的ret_port(接受minion请求的一个端口号)和publish_port(发布消息的端口)
minion一但发送请求就会自动连接,里面定义的master地址ret_prot端口进行连接认证
saltstack除了传统的C/S架构外,其实还有一种叫做masterless的架构,其不需要单独安装一台 master 服务器,只需要在每台机器上安装 Minion端,(就是各管各的,没装自动化是需要算每一步做什么,装了的则只用在本机进行执行)然后采用本机只负责对本机的配置管理机制服务的模式。
四大功能
远程执行 要用到执行模块
配置管理/状态管理 描绘什么状态就做什么状态
云管理(cloud) 对接云平台
事件驱动 被动技能
模块分类:
执行模块 :一次性的,用命令行执行
状态模块 :常态化的(经常需要执行的)
四大运行
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 | 是有关底层受管系统的静态信息,包括操作系统,内存和许多其他系统属性。 |
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 | 需要安装的应用 |
---|---|---|
控制机 | 192.168.230.131 | salt salt-cloud salt-master salt-minion salt-ssh salt-syndic |
被控机 | 192.168.230.139 | salt-minion |
官方yum源地址:https://repo.saltproject.io
#修改主机名
[root@localhost ~]# hostnamectl set-hostname master
[root@master ~]# bash
[root@master ~]#
[root@localhost ~]# hostnamectl set-hostname minion
[root@localhost ~]# bash
[root@minion ~]#
#关闭防火墙和selinux(两台)
[root@master ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]# vim /etc/selinux/config
[root@master ~]# setenforce 0
#两台主机配置yum环境
[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@minion ~]# scp 192.168.230.131:/etc/yum.repos.d/salt.repo /etc/yum.repos.d/
The authenticity of host '192.168.230.131 (192.168.230.131)' can't be established.
ECDSA key fingerprint is SHA256:e/KbwT05TmNsBzMPXhJ69N35GYiNuMDUzKT36R76D7Y.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.230.131' (ECDSA) to the list of known hosts.
root@192.168.230.131's password:
salt.repo 100% 292 178.7KB/s 00:00
#安装需要安装的应用
[root@master ~]# yum -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 master
# resolved, then the minion will fail to start.
#master: salt
master: 192.168.230.131 #添加master主机IP,IP前加个空格
# Set http proxy information for the minion when doing requests
[root@master salt]# ss -anlt
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 [::]:22 [::]:*
[root@master salt]# 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 salt]# ss -anlt
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 [::]:*
[root@master salt]# systemctl enable --now salt-minion
Created symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service → /usr/lib/systemd/system/salt-minion.service.
[root@master salt]# ss -anlt
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 [::]:* 注意:minion开启是不显示进程的
#安装需要安装的应用及修改配置文件
[root@minion ~]# yum -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
# resolved, then the minion will fail to start.
#master: salt
master: 192.168.230.131 #添加master主机IP,IP前加个空格
# Set http proxy information for the minion when doing requests
#开启salt-minion
[root@minion salt]# systemctl enable --now salt-minion
Created symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service → /usr/lib/systemd/system/salt-minion.service.
altstack配置文件
saltstack的配置文件在/etc/salt目录
配置文件 | 说明 |
---|---|
/etc/salt/master | 主控端(控制端)配置文件 |
/etc/salt/minion | 受控端配置文件 |
配置文件/etc/salt/minion常用配置参数(需要更改的两个)
master:设置主控端的IP
id:设置受控端本机的唯一标识符,可以是ip也可以是主机名或自取某有意义的单词
日常使用中,经常需要调整或修改Master配置文件,下面几个参数很重要:
max_open_files:可根据Master将Minion数量进行适当的调整
timeout:可根据Master和Minion的网络状况适当调整
auto_accept和autosign_file:在大规模部署Minion时可设置自动签证
master_tops和所有以external开头的参数:这些参数是SaltStack与外部系统进行整合的相关配置参数
SaltStack认证机制
主控端是依靠openssl证书与受控端主机认证通讯的(受控端启动后会发送给主控端一个公钥证书文件,在主控端用salt-key命令来管理证书)
alt-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: #未认证清单
master
Rejected Keys: #拒绝的清单
#master是开启后使用tree命令查看下结构,如果没有做过命令自行下载
[root@master salt]# tree pki
pki
├── master
│ ├── master.pem
│ ├── master.pub
│ ├── minions
│ ├── minions_autosign
│ ├── minions_denied
│ ├── minions_pre
│ │ └── master
│ └── minions_rejected
└── minion
├── minion.pem
└── minion.pub
#接受所有minion的新证书
[root@master ~]# salt-key -yA
The following keys are going to be accepted:
Unaccepted Keys:
master
minion
Key for minion master accepted.
Key for minion minion accepted.
[root@master ~]# salt-key -L
Accepted Keys:
master
minion
Denied Keys:
Unaccepted Keys:
Rejected Keys:
#未开启时进行查看
[root@minion salt]# tree pki
pki
├── master
└── minion
2 directories, 0 files
#开启后查看
[root@minion salt]# systemctl enable --now salt-minion
Created symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service → /usr/lib/systemd/system/salt-minion.service.
#开启后过一会再查看才会出来
[root@minion salt]# tree pki
pki
├── master
└── minion
├── minion.pem
└── minion.pub
2 directories, 2 files
#master主机认证通过后再次查看会发现有master主机的清单
[root@minion salt]# tree pki
pki
├── master
└── minion
├── minion_master.pub
├── minion.pem
└── minion.pub
2 directories, 3 files
SaltStack远程执行
#通过主机名的方式使用模板test里面的ping模块
[root@master ~]# salt "minion" test.ping
minion:
True
#通过IP地址的方式使用模板test里面的ping模块
[root@master ~]# salt -S "192.168.230.139" test.ping
minion:
True
#测试所有受控端主机是否存活
[root@master ~]# salt "*" test.ping
master:
True
minion:
True
salt命令使用
常用的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值匹配 |
实例:
#通过IP地址的方式使用模板test里面的ping模块
[root@master ~]# salt -S "192.168.230.139" test.ping
minion:
True
#通过m开头的方式使用模板test里面的ping模块
[root@master ~]# salt -E 'm*' test.ping
master:
True
minion:
True