SaltStack之return与job管理

一、SaltStack组件之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

1.1 return流程

return是在Master端触发任务,然后Minion接受处理任务后直接与return存储服务器建立连接,然后把数据return存到存储服务器。关于这点一定要注意,因为此过程都是Minion端操作存储服务器,所以要确保Minion端的配置跟依赖包是正确的,这意味着我们将必须在每个Minion上安装指定的return方式依赖包,假如使用Mysql作为return存储方式,那么我们将在每台Minion上安装python-mysql模块。

1.2 使用mysql作为return存储方式

在所有minion上安装Mysql-python模块

[root@master ~]# salt 'minion' pkg.install python3-PyMySQL
minion:
    ----------
    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:
[root@minion ~]# rpm -qa|grep -i pymysql
python3-PyMySQL-0.10.1-2.module_el8.5.0+761+faacb0fb.noarch

部署一台mysql服务器用作存储服务器

//部署mysql
[root@localhost ~]# yum -y install mariadb mariadb-server
[root@localhost ~]# systemctl start mariadb

//创建数据库和表结构
[root@localhost ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-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.00 sec)

MariaDB [(none)]> use `salt`;
Database changed
MariaDB [salt]> drop table if exists `jids`;
Query OK, 0 rows affected, 1 warning (0.00 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.00 sec)

MariaDB [salt]> drop table if exists `salt_returns`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [salt]> create table `salt_returns` (
    -> `fun` varchar(50) not null,
    -> `jid` varchar(255) not null,
    -> `return` mediumtext not null,
    -> `id` varchar(259) 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, 1 warning (0.00 sec)

MariaDB [salt]> drop table if exists `salt_events`;
Query OK, 0 rows affected, 1 warning (0.00 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.00 sec)

MariaDB [salt]> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids           |
| salt_events    |
| salt_returns   |
+----------------+
3 rows in set (0.00 sec)

MariaDB [salt]> desc jids;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| jid   | varchar(255) | NO   | PRI | NULL    |       |
| load  | mediumtext   | NO   |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

//授权访问
MariaDB [salt]> grant all on salt.* to 'salt'@'%' identified by 'salt';
Query OK, 0 rows affected (0.00 sec)

MariaDB [salt]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

在minion安装mariadb,测试连接mysql服务器

[root@minion ~]# yum -y install mariadb
[root@minion ~]# which mysql
/usr/bin/mysql
[root@minion ~]# mysql -usalt -psalt -h192.168.47.161
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-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)]> quit
Bye

配置minion

[root@minion ~]# vim /etc/salt/minion
927 #return:
928 #  - mysql
929 #  - hipchat
930 #  - slack
## 在配置文件里面添加以下几行
931 mysql.host: '192.168.47.161'     ## mysql服务器的IP
932 mysql.user: 'salt'
933 mysql.pass: 'salt'
934 mysql.db: 'salt'
935 mysql.port: 3306
[root@minion ~]# systemctl restart salt-minion

在Master上测试存储到mysql中

[root@master ~]# salt 'minion' test.ping    ## 因为重启的minion端的salt-minion服务,所以这边连接会很慢,要多连几次,连上去了再进行下面操作
minion:
    True
[root@master ~]# salt 'minion' test.ping --return mysql
minion:
    True

在数据库中查询

MariaDB [salt]> select * from salt_returns\G
*************************** 1. row ***************************
       fun: test.ping
       jid: 20211106120042228872
    return: true
        id: minion
   success: 1
  full_ret: {"success": true, "return": true, "retcode": 0, "jid": "20211106120042228872", "fun": "test.ping", "fun_args": [], "id": "minion"}
alter_time: 2021-11-06 20:53:02
1 row in set (0.00 sec)

总结return执行过程:由master向minion发送指令,然后minion去执行,执行完后minion需要向两边去汇报

二、job cache

2.1 job cache流程

在这里插入图片描述
return时是由Minion直接与存储服务器进行交互,因此需要在每台Minion上安装指定的存储方式的模块,比如python-mysql,那么我们能否直接在Master上就把返回的结果给存储到存储服务器呢?

答案是肯定的,这种方式被称作 job cache 。意思是当Minion将结果返回给Master后,由Master将结果给缓存在本地,然后将缓存的结果给存储到指定的存储服务器,比如存储到mysql中。官方文档

开启master端的master_job_cache

[root@master ~]# vim /etc/salt/master
 137 #job_cache: True
 ## 在配置文件中添加以下几行
 138 master_job_cache: mysql
 139 mysql.host: '192.168.47.161'
 140 mysql.user: 'salt'
 141 mysql.pass: 'salt'
 142 mysql.db: 'salt'
 143 mysql.port: 3306
[root@master ~]# systemctl restart salt-master

在master上安装mariadb和Mysql-python模块

[root@master ~]# yum -y install mariadb
[root@master ~]# which mysql
/usr/bin/mysql
[root@master ~]# yum -y install python3-PyMySQL
[root@master ~]# rpm -qa|grep -i pymysql
python3-PyMySQL-0.10.1-2.module_el8.5.0+761+faacb0fb.noarch

在数据库服务器中清空表内容

[root@localhost ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 21
Server version: 5.5.68-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)]> use salt;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [salt]> delete from salt_returns;
Query OK, 4 rows affected (0.00 sec)

MariaDB [salt]> select * from salt_returns\G
Empty set (0.00 sec)

在master上再次测试能否存储至数据库

[root@master ~]# salt '*' test.ping
master:
    True
minion:
    True
[root@master ~]# salt 'minion' cmd.run 'df -h'
minion:
    Filesystem           Size  Used Avail Use% Mounted on
    devtmpfs             371M     0  371M   0% /dev
    tmpfs                391M  140K  391M   1% /dev/shm
    tmpfs                391M   16M  375M   4% /run
    tmpfs                391M     0  391M   0% /sys/fs/cgroup
    /dev/mapper/cs-root   66G  2.4G   63G   4% /
    /dev/sda1           1014M  195M  820M  20% /boot
    /dev/mapper/cs-home   32G  260M   32G   1% /home
    tmpfs                 79M     0   79M   0% /run/user/0
[root@master ~]# salt 'minion' cmd.run 'date'
minion:
    Sat Nov  6 21:00:03 CST 2021

在数据库中查询

MariaDB [salt]> select * from salt_returns\G
*************************** 1. row ***************************
       fun: test.ping
       jid: 20211106125442844031
    return: true
        id: master
   success: 1
  full_ret: {"cmd": "_return", "id": "master", "success": true, "return": true, "retcode": 0, "jid": "20211106125442844031", "fun": "test.ping", "fun_args": [], "_stamp": "2021-11-06T12:54:43.038821"}
alter_time: 2021-11-06 21:47:03
*************************** 2. row ***************************
       fun: test.ping
       jid: 20211106125442844031
    return: true
        id: minion
   success: 1
  full_ret: {"cmd": "_return", "id": "minion", "success": true, "return": true, "retcode": 0, "jid": "20211106125442844031", "fun": "test.ping", "fun_args": [], "_stamp": "2021-11-06T12:54:43.046980"}
alter_time: 2021-11-06 21:47:03
2 rows in set (0.00 sec)

2.2 job管理

获取任务的jid

[root@master ~]# salt 'minion' cmd.run 'date' -v
Executing job with jid 20211106130413963177
-------------------------------------------

minion:
    Sat Nov  6 21:04:14 CST 2021

通过jid获取此任务的返回结果

[root@master ~]# salt-run jobs.lookup_jid 20211106130413963177
minion:
    Sat Nov  6 21:04:14 CST 2021
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值