SaltStack组件之return
return流程
return组件可以理解为SaltStack系统对执行Minion返回后的数据进行存储或者返回给其他程序,它支持多种存储方式,比如用MySQL、MongoDB、Redis、Memcache等,通过return我们可以对SaltStack的每次操作进行记录,对以后日志审计提供了数据来源。目前官方已经支持30种return数据存储与接口,我们可以很方便的配置与使用它。当然也支持自己定义的return,自定义的return需由python来编写。在选择和配置好要使用的return后,只需在salt命令后面指定return即可。
//查看所有return列表
[root@master ~]# salt 'minion' sys.list_returners
minion:
- carbon
- couchdb
- etcd
- highstate
- local
- local_cache
- mattermost
- multi_returner
- pushover
- rawfile_json
- slack
- slack_webhook
- smtp
- splunk
- sqlite3
- syslog
- telegram
return流程
return是在Master端触发任务,然后Minion接受处理任务后直接与return存储服务器建立连接,然后把数据return存到存储服务器。关于这点一定要注意,因为此过程都是Minion端操作存储服务器,所以要确保Minion端的配置跟依赖包是正确的,这意味着我们将必须在每个Minion上安装指定的return方式依赖包,假如使用Mysql作为return存储方式,那么我们将在每台Minion上安装python-mysql模块。
使用mysql作为return存储方式
在所有minion上安装Mysql-python模块
//master上启动服务
[root@master ~]# systemctl start salt-master.service
[root@master ~]# systemctl stop salt-minion.service
[root@master ~]# ss -antl
//minion上安装python3-PyMySQLbing启动服务
[root@minion ~]# systemctl start salt-minion.service
[root@minion ~]# yum -y install python3-PyMySQL
[root@minion ~]# systemctl status salt-minion.service
//安装服务并配置
[root@minion1 ~]# yum -y install mariadb mariadb-serve
[root@minion1 ~]# systemctl start mariadb.service
[root@minion1 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
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)]> set password = password("EDgnb123!");
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> CREATE DATABASE `salt` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| salt |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [(none)]> use salt;
Database changed
MariaDB [salt]> DROP TABLE IF EXISTS `jids`;
Query OK, 0 rows affected, 1 warning (0.001 sec)
MariaDB [salt]> CREATE TABLE `jids` (`jid` varchar(255) NOT NULL,`load` mediumtext NOT NULL, UNIQUE KEY `jid` (`jid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.015 sec)
MariaDB [salt]> DROP TABLE IF EXISTS `salt_returns`;
Query OK, 0 rows affected, 1 warning (0.000 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.020 sec)
MariaDB [salt]> 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;
Query OK, 0 rows affected (0.003 sec)
##授权访问
MariaDB [salt]> grant all on salt.* to salt@'%' identified by 'salt';
Query OK, 0 rows affected (0.001 sec)
MariaDB [salt]> flush privileges;
Query OK, 0 rows affected (0.000 sec)
//master上接收minion的认证
[root@master ~]# salt-key -L
Accepted Keys:
master
minion
minion1
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@master ~]# salt '*' test.ping
minion:
True
minion2:
True
master:
True
//minion上安装mariadb
[root@minion ~]# yum -y install mariadb
##使用mariadb主机创建的用户和密码登录
[root@minion ~]# mysql -usalt -psalt -h192.168.126.160
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)]> exit
Bye
##修改配置文件
[root@minion ~]# vim /etc/salt/minion
#return:
# - mysql
# - hipchat
# - slack
mysql.host: '192.168.14.16'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
###### Miscellaneous settings ######
##重启minion
[root@minion ~]# systemctl restart salt-minion.service
//master上测试存储到mariadb中
[root@master ~]# salt 'minion' test.ping --return mysql
minion:
True
MariaDB [(none)]> select * from salt.salt_returns\G
*************************** 1. row ***************************
fun: test.ping
jid: 20211107055302740262
return: true
id: minion
success: 1
full_ret: {"success": true, "return": true, "retcode": 0, "jid": "20211107055302740262", "fun": "test.ping", "fun_args": [], "id": "node1"}
alter_time: 2021-11-07 01:53:02
1 row in set (0.002 sec)
job cache
job cache流程
return时是由Minion直接与存储服务器进行交互,因此需要在每台Minion上安装指定的存储方式的模块,比如python-mysql,那么我们能否直接在Master上就把返回的结果给存储到存储服务器呢?
答案是肯定的,这种方式被称作 job cache 。意思是当Minion将结果返回给Master后,由Master将结果给缓存在本地,然后将缓存的结果给存储到指定的存储服务器,比如存储到mysql中。
//之前改的minion上的配置文件注释掉
[root@minion ~]# vim /etc/salt/minion
# - hipchat
# - slack
#mysql.host: '192.168.14.16'
#mysql.user: 'salt'
#mysql.pass: 'salt'
#mysql.db: 'salt'
#mysql.port: 3306
###### Miscellaneous settings ######
############################################
[root@minion ~]# systemctl restart salt-minion.service
//开启master端的master_job_cache
[root@master jobs]# yum -y install python3-PyMySQL
//修改master的配置文件
[root@master jobs]# vim /etc/salt/master
136 #job_cache: True
137 master_job_cache: mysql
138 mysql.host: '192.168.126.160'
139 mysql.user: 'salt'
140 mysql.pass: 'salt'
141 mysql.db: 'salt'
142 mysql.port: 3306
143 # Cache minion grains, pillar and mine data via the cache subsy stem in the
//重启master
[root@master jobs]# systemctl restart salt-master.service
//master安装mariadb服务
[root@master jobs]# yum -y install mariadb
//删除mariadb上之前的数据
[root@minion ~]# mysql -uroot -plq123!
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
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)]> delete from salt.salt_returns;
Query OK, 1 row affected (0.003 sec)
MariaDB [(none)]> select * from salt.salt_returns;
Empty set (0.000 sec)
MariaDB [(none)]> exit
Bye
//在mariadb数据库中查询
MariaDB [(none)]> select * from salt.salt_returns\G
*************************** 1. row ***************************
fun: cmd.run
jid: 20211107074931938473
return: ".\n..\n.bash_history\n.bash_logout\n.bash_profile\n.bashrc\n.config\n.cshrc\n.mysql_history\n.tcshrc\n.viminfo\n1\nanaconda-ks.cfg\nbest\nliu.sh\nyou\n!"
id: minion
success: 1
full_ret: {"cmd": "_return", "id": "node1", "success": true, "return": ".\n..\n.bash_history\n.bash_logout\n.bash_profile\n.bashrc\n.config\n.cshrc\n.mysql_history\n.tcshrc\n.viminfo\n1\nanaconda-ks.cfg\nbest\nliu.sh\nyou\n!", "retcode": 0, "jid": "20211107074931938473", "fun": "cmd.run", "fun_args": ["ls -a"], "_stamp": "2021-11-07T07:49:32.541180"}
alter_time: 2021-11-07 02:49:32
1 row in set (0.001 sec)
job 管理
Salt 0.9.7 为管理作业的saltutil引入了一些新功能。这些功能是:
running返回在proc目录中找到的所有运行作业的数据。
find_job根据工作 ID 返回有关某项工作的具体数据。
signal_job允许向给定夹具发送信号。
term_job向控制指定作业的流程发送终止信号(SIGTERM,15)。
kill_job向控制指定作业的流程发送杀伤信号(SIGKILL,9)。
获取任务的jid
[root@master ~]# salt 'minion' cmd.run 'date' -v
Executing job with jid 20216375130413962754
-------------------------------------------
minion:
Sat Nov 6 10:30:19 CST 2021
通过jid获取此任务的返回结果
[root@master ~]# salt-run jobs.lookup_jid 20216375130413962754
minion:
Sat Nov 6 10:36:34 CST 2021