当前位置:网站首页>SaltStack之return与job管理
SaltStack之return与job管理
2022-07-28 17:28:00 【阿木690】
1. SaltStack组件之return
return组件可以理解为SaltStack系统对执行Minion返回后的数据进行存储或者返回给其他程序,它支持多种存储方式,比如用MySQL、MongoDB、Redis、Memcache等,通过return我们可以对SaltStack的每次操作进行记录,对以后日志审计提供了数据来源。目前官方已经支持30种return数据存储与接口,我们可以很方便的配置与使用它。当然也支持自己定义的return,自定义的return需由python来编写。在选择和配置好要使用的return后,只需在salt命令后面指定return即可。
环境说明:
| 主机名字 | 主机类型 | IP | 系统 | 需要安装的应用 |
|---|---|---|---|---|
| master | 控制机 | 192.168.91.135 | CentOS8 | salt-master salt-minion python3-PyMySQL |
| node2 | 被控机 | 192.168.91.134 | CentOS8 | salt-minion python3-PyMySQL mariadb-server、mariadb |
| node3 | 被控机 | 192.168.91.138 | CentOS8 | salt-minion python3-PyMySQL |
// 查看所有return列表
[[email protected] ~]# salt '*' sys.list_returners
[[email protected] ~]# salt '*' sys.list_returners
master:
- carbon
- couchdb
- etcd
- highstate
- local
- local_cache
- mattermost
- multi_returner
- pushover
- rawfile_json
- slack
- slack_webhook
- smtp
- splunk
- sqlite3
- syslog
- telegram
node2:
- carbon
- couchdb
- etcd
- highstate
- local
- local_cache
- mattermost
- multi_returner
- pushover
- rawfile_json
- slack
- slack_webhook
- smtp
- splunk
- sqlite3
- syslog
- telegram
node3:
- carbon
- couchdb
- etcd
- highstate
- local
- local_cache
- mattermost
- multi_returner
- pushover
- rawfile_json
- slack
- slack_webhook
- smtp
- splunk
- sqlite3
- syslog
- telegram
1.1 return流程
return是在Master端触发任务,然后Minion接受处理任务后直接与return存储服务器建立连接,然后把数据return存到存储服务器。关于这点一定要注意,因为此过程都是Minion端操作存储服务器,所以要确保Minion端的配置跟依赖包是正确的,这意味着我们将必须在每个Minion上安装指定的return方式依赖包,假如使用Mysql作为return存储方式,那么我们将在每台Minion上安装python-mysql模块。
1.2 使用mysql作为return存储方式
在所有minion上安装Mysql-python模块
[[email protected] ~]# yum list all|grep -i 'mysql'|grep python
Failed to set locale, defaulting to C.UTF-8
python3-PyMySQL.noarch 0.10.1-2.module_el8.5.0+761+faacb0fb @appstream
python2-PyMySQL.noarch 0.8.0-10.module_el8.5.0+743+cd2f5d28 appstream
python38-PyMySQL.noarch 0.10.1-1.module_el8.5.0+742+dbad1979 appstream
python39-PyMySQL.noarch 0.10.1-2.module_el8.5.0+738+dc19af12 appstream
[[email protected] ~]# salt '*' pkg.install python3-PyMySQL
node2:
----------
python3-PyMySQL:
----------
new:
0.10.1-2.module_el8.5.0+761+faacb0fb
old:
python3-cffi:
----------
new:
1.11.5-5.el8
old:
python3-cryptography:
----------
new:
3.2.1-5.el8
old:
python3-ply:
----------
new:
3.9-9.el8
old:
python3-pycparser:
----------
new:
2.14-14.el8
old:
master:
----------
python3-PyMySQL:
----------
new:
0.10.1-2.module_el8.5.0+761+faacb0fb
old:
python3-cffi:
----------
new:
1.11.5-5.el8
old:
python3-cryptography:
----------
new:
3.2.1-5.el8
old:
python3-ply:
----------
new:
3.9-9.el8
old:
python3-pycparser:
----------
new:
2.14-14.el8
old:
node3:
----------
python3-PyMySQL:
----------
new:
0.10.1-2.module_el8.5.0+761+faacb0fb
old:
python3-cffi:
----------
new:
1.11.5-5.el8
old:
python3-cryptography:
----------
new:
3.2.1-5.el8
old:
python3-ply:
----------
new:
3.9-9.el8
old:
python3-pycparser:
----------
new:
2.14-14.el8
old:
部署一台mysql服务器用作存储服务器,此处就直接在node2这台主机上部署
[[email protected] ~]# yum -y install mariadb-server mariadb
安装过程省略...
// 启动服务
[[email protected] ~]# systemctl enable --now mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
// 创建数据库和表结构
[[email protected] ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE `salt`
-> DEFAULT CHARACTER SET utf8
-> DEFAULT COLLATE utf8_general_ci;
Query OK, 1 row affected (0.012 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| salt |
+--------------------+
4 rows in set (0.015 sec)
// 进入数据库
MariaDB [(none)]> use salt;
Database changed
MariaDB [salt]> DROP TABLE IF EXISTS `jids`;
Query OK, 0 rows affected, 1 warning (0.016 sec)
MariaDB [salt]> CREATE TABLE `jids` (
-> `jid` varchar(225) NOT NULL,
-> `load` mediumtext NOT NULL,
-> UNIQUE KEY `jid` (`jid`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.019 sec)
MariaDB [salt]> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids |
+----------------+
1 row in set (0.001 sec)
MariaDB [salt]> CREATE TABLE `salt_returns` (
-> `fun` varchar(50) NOT NULL,
-> `jid` varchar(255) NOT NULL,
-> `return` mediumtext NOT NULL,
-> `id` varchar(255) NOT NULL,
-> `success` varchar(10) NOT NULL,
-> `full_ret` mediumtext NOT NULL,
-> `alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
-> KEY `id` (`id`),
-> KEY `jid` (`jid`),
-> KEY `fun` (`fun`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.030 sec)
CREATE TABLE `salt_events` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`tag` varchar(255) NOT NULL,
`data` mediumtext NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`master_id` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `tag` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
MariaDB [salt]> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids |
| salt_events |
| salt_returns |
+----------------+
3 rows in set (0.000 sec)
// 授予权限访问
MariaDB [salt]> grant all on salt.* to [email protected]'%' identified by 'salt';
Query OK, 0 rows affected (0.006 sec)
MariaDB [salt]> flush privileges;
Query OK, 0 rows affected (0.015 sec)
配置minion
[[email protected] ~]# vim /etc/salt/minio
mysql.host: '192.168.91.135' // 写mysql主机的ip
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
// 服务重启
[[email protected] ~]# systemctl restart salt-minion
在Master上测试存储到mysql中
[[email protected] ~]# salt 'node3' test.ping --return mysql
node3:
True
在数据库中查询
MariaDB [salt]> select * from salt_returns\G
*************************** 1. row ***************************
fun: test.ping
jid: 20211107053320338072
return: true
id: node3
success: 1
full_ret: {
"success": true, "return": true, "retcode": 0, "jid": "20211107053320338072", "fun": "test.ping", "fun_args": [], "id": "node3"}
alter_time: 2021-11-07 13:33:23
1 row in set (0.011 sec)
2. job cache
2.1 job cache流程
return时是由Minion直接与存储服务器进行交互,因此需要在每台Minion上安装指定的存储方式的模块,比如python-mysql,那么我们能否直接在Master上就把返回的结果给存储到存储服务器呢?
答案是肯定的,这种方式被称作 job cache 。意思是当Minion将结果返回给Master后,由Master将结果给缓存在本地,然后将缓存的结果给存储到指定的存储服务器,比如存储到mysql中。
开启master端的master_job_cache
[[email protected] ~]# yum -y install python3-PyMySQL
[[email protected] ~]# vim /etc/salt/master
#job_cache: True // 在这行后面添加信息
master_job_cache: mysql
mysql.host: '192.168.91.134' // 写mysql主机的ip
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
// 服务重启
[[email protected] ~]# systemctl restart salt-master
在数据库服务器中清空表内容
MariaDB [(none)]> delete from salt.salt_returns;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> select * from salt.salt_returns\G
Empty set (0.000 sec)
在master主机上再次测试能否存储至数据库
[[email protected] ~]# salt node3 cmd.run 'df -h'
node3:
Filesystem Size Used Avail Use% Mounted on
devtmpfs 371M 0 371M 0% /dev
tmpfs 391M 40K 391M 1% /dev/shm
tmpfs 391M 5.6M 385M 2% /run
tmpfs 391M 0 391M 0% /sys/fs/cgroup
/dev/mapper/cs-root 17G 1.9G 16G 11% /
/dev/sda1 1014M 195M 820M 20% /boot
tmpfs 79M 0 79M 0% /run/user/0
MariaDB [(none)]> select * from salt.salt_returns\G
*************************** 1. row ***************************
fun: cmd.run
jid: 20211107054058768090
return: "Filesystem Size Used Avail Use% Mounted on\ndevtmpfs 371M 0 371M 0% /dev\ntmpfs 391M 40K 391M 1% /dev/shm\ntmpfs 391M 5.6M 385M 2% /run\ntmpfs 391M 0 391M 0% /sys/fs/cgroup\n/dev/mapper/cs-root 17G 1.9G 16G 11% /\n/dev/sda1 1014M 195M 820M 20% /boot\ntmpfs 79M 0 79M 0% /run/user/0"
id: node3
success: 1
后面省略...
2.2 job管理
获取任务的jid
[[email protected] ~]# salt node3 cmd.run 'uptime' -v
Executing job with jid 20211107054539653392 // 此处就是此命令的jid
-------------------------------------------
node3:
13:45:41 up 2:07, 2 users, load average: 0.02, 0.04, 0.07
通过jid获取此任务的返回结果
[[email protected] ~]# salt-run jobs.lookup_jid 20211107054539653392
node3:
13:45:41 up 2:07, 2 users, load average: 0.02, 0.04, 0.07
边栏推荐
- Mid 2022 summary
- 【已解决】AC86U ML改版固件虚拟内存创建失败,提示USB磁盘读写速度不满足要求
- Remember a uniapp experience
- IMU heating
- 用于异常检测的Transformer - InTra《Inpainting Transformer for Anomaly Detection》
- [physical application] atmospheric absorption loss with matlab code
- Pytorch:快速求得NxN矩阵的主对角线(diagonal)元素与非对角线元素
- Application of time series database in cigarette factory
- Application of time series database in Hydropower Station
- An intern's journey to cnosdb
猜你喜欢

Pyg builds heterogeneous graph attention network han to realize DBLP node prediction
![[filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code](/img/c7/e149e35a544b7a89bbd167c45637a4.png)
[filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code

Libgdx learning road 02: draw game map with tiled

Cv5200 wireless WiFi communication module, wireless video image transmission, real-time wireless communication technology

【滤波跟踪】基于EKF、时差和频差定位实现目标跟踪附matlab代码

优麒麟系统安装BeyondComare
![[image hiding] digital image information hiding system based on DCT, DWT, LHA, LSB, including various attacks and performance parameters, with matlab code](/img/a4/5c5a90508e2f9c6b4f8e234bdfdc9e.png)
[image hiding] digital image information hiding system based on DCT, DWT, LHA, LSB, including various attacks and performance parameters, with matlab code

SQL custom automatic calculation

The ever-changing pointer ----- C language

SQL审核工具自荐Owls
随机推荐
R语言与数据分析实战11-数据的删除
C语言循环语句强化练习题
It is the best tool to evaluate six kinds of map visualization software in three categories
vim学习手册
Pandownload revival tutorial
[solved] ac86u ml revision firmware virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements
Parity rearrangement of Bm14 linked list
4、 Interface requests data to update input information interactively
Pytorch:快速求得NxN矩阵的主对角线(diagonal)元素与非对角线元素
CVPR21-无监督异常检测《CutPaste:Self-Supervised Learning for Anomaly Detection and Localization》
身份证号的奥秘
Tikz draw Gantt chart in FJSP -trans necessary
Mid 2022 summary
【已解决】AC86U ML改版固件虚拟内存创建失败,提示USB磁盘读写速度不满足要求
BM14 链表的奇偶重排
How to write a JMeter script common to the test team
用于异常检测的Transformer - InTra《Inpainting Transformer for Anomaly Detection》
RFs self study notes (III): clutter model - first determine the number with Poisson distribution, and then use uniform distribution as probability distribution
图书管理数据库系统设计
Application of time series database in museum environment detection