目 录CONTENT

文章目录

SaltStack之return与job管理

ZiChen D
2021-11-07 / 0 评论 / 0 点赞 / 497 阅读 / 10,598 字 / 正在检测是否收录...

SaltStack组件之return

return组件可以理解为SaltStack系统对执行Minion返回后的数据进行存储或者返回给其他程序,它支持多种存储方式,比如用MySQL、MongoDB、Redis、Memcache等,通过return我们可以对SaltStack的每次操作进行记录,对以后日志审计提供了数据来源。目前官方已经支持30种return数据存储与接口,我们可以很方便的配置与使用它。当然也支持自己定义的return,自定义的return需由python来编写。在选择和配置好要使用的return后,只需在salt命令后面指定return即可。

[root@master ~]# salt 'node1' sys.list_returners
node1:
    - carbon
    - couchdb
    - etcd
    - highstate
    - local
    - local_cache
    - mattermost
    - multi_returner
    - pushover
    - rawfile_json
    - slack
    - slack_webhook
    - smtp
    - splunk
    - sqlite3
    - syslog
    - telegram

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

流程图

使用mysql作为return存储方式

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

[root@master ~]# salt 'node1' pkg.install python3-PyMySQL
node1:
    ----------
    python3-PyMySQL:
        ----------
        new:
            0.8.0-10.module+el8.1.0+3334+5cb623d7
        old:

部署一台mysql服务器用作存储服务器,此处就直接在192.168.159.14这台主机上部署

[root@node1 ~]# yum -y install mariadb mariadb-server
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:0:06:55 前,执行于 2021年11月07日 星期日 09时36分37秒。
软件包 mariadb-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64 已安装。
软件包 mariadb-server-3:10.3.17-1.module+el8.1.0+3974+90eded84.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!
[root@node1 ~]# 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.
[root@node1 ~]# ss -antl
State                     Recv-Q                    Send-Q                                       Local Address:Port                                         Peer Address:Port                    
LISTEN                    0                         128                                                0.0.0.0:22                                                0.0.0.0:*                       
LISTEN                    0                         128                                                      *:80                                                      *:*                       
LISTEN                    0                         128                                                   [::]:22                                                   [::]:*                       
LISTEN                    0                         80                                                       *:3306                                                    *:*                       

//创建数据库和表结构
MariaDB [(none)]> CREATE DATABASE  `salt`
    ->   DEFAULT CHARACTER SET utf8
    ->   DEFAULT COLLATE utf8_general_ci;
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> 
MariaDB [(none)]> USE `salt`;
Database changed

MariaDB [salt]> DROP TABLE IF EXISTS `jids`;
Query OK, 0 rows affected (0.007 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.012 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.009 sec)


MariaDB [salt]> DROP TABLE IF EXISTS `salt_events`;
Query OK, 0 rows affected, 1 warning (0.000 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.009 sec)

MariaDB [salt]> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids           |
| salt_events    |
| salt_returns   |
+----------------+
3 rows in set (0.000 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.001 sec)

MariaDB [salt]> grant all on salt.* to 'salt'@'%' identified by 'salt';
Query OK, 0 rows affected (0.000 sec)

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

配置minion

//因为node1用作mysql了,所以这里开始用node2做后面操作
[root@node2 ~]# yum -y install mariadb
完毕!

[root@node2 ~]# mysql -usalt -psalt -h192.168.159.14
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.17-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

[root@node2 ~]# vim /etc/salt/minion
#return: mysql
mysql.host: '192.168.159.14'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
#
#return: mysql,slack,redis

[root@node2 ~]# systemctl restart salt-minion

[root@master ~]# salt '*' test.ping
node2:
    True
node1:
    True

在Master上测试存储到mysql中

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

MariaDB [salt]> select * from salt_returns;
Empty set (0.000 sec)

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

//测试,添加东西
[root@master ~]# salt 'node2' test.ping --return mysql
node2:
    True

在数据库中查询

MariaDB [salt]> select * from salt_returns\G
*************************** 1. row ***************************
       fun: test.ping
       jid: 20211107111725669412
    return: true
        id: node2
   success: 1
  full_ret: {"success": true, "return": true, "retcode": 0, "jid": "20211107111725669412", "fun": "test.ping", "fun_args": [], "id": "node2"}
alter_time: 2021-11-07 11:17:27
1 row in set (0.000 sec)

job cache

job cache流程

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

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

开启master端的master_job_cache

[root@master ~]# vim /etc/salt/master
#job_cache: True
master_job_cache: mysql
mysql.host: '192.168.159.14'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306

测试master能否连到mysql储存端

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

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

MariaDB [salt]> delete from salt_returns;
Query OK, 1 row affected (0.025 sec)

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

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

[root@master ~]# salt 'node2' cmd.run 'cat /root/dzc'
node2:
    welcome to saltstack

在数据库中查询

MariaDB [salt]> select * from salt_returns\G
*************************** 1. row ***************************
       fun: cmd.run
       jid: 20211107120722684165
    return: "welcome to saltstack"
        id: node2
   success: 1
  full_ret: {"cmd": "_return", "id": "node2", "success": true, "return": "welcome to saltstack", "retcode": 0, "jid": "20211107120722684165", "fun": "cmd.run", "fun_args": ["cat /root/dzc"], "_stamp": "2021-11-07T12:07:22.917781"}
alter_time: 2021-11-07 12:07:24

job管理

获取任务的jid

[root@master ~]# salt 'node2' cmd.run 'uptime' -v
Executing job with jid 20211107120836082426
-------------------------------------------

node2:
     12:08:36 up  1:14,  2 users,  load average: 0.02, 0.07, 0.08

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

[root@master ~]# salt-run jobs.lookup_jid 20211107120836082426
node2:
     12:08:36 up  1:14,  2 users,  load average: 0.02, 0.07, 0.08

jobs.list_jobs

在查找历史工作之前,可能需要查找工作 ID。list_jobs 将解析缓存的执行数据并显示已经或部分返回的作业的所有作业数据。

[root@master ~]# salt-run jobs.list_jobs
 20211107120722684165:
    ----------
    Arguments:
        - cat /root/dzc
    Function:
        cmd.run
    StartTime:
        2021, Nov 07 12:07:22.684165
    Target:
        node2
    Target-type:
        glob
    User:
        root
20211107120722710308:
    ----------
    Arguments:
        - cat /root/dzc
    Function:
        cmd.run
    StartTime:
        2021, Nov 07 12:07:22.710308
    Target:
        node2
    Target-type:
        glob
    User:
        root
20211107120727327809:
    ----------
    Arguments:
        - cat /root/dzc
    Function:
        cmd.run
    StartTime:
        2021, Nov 07 12:07:27.327809
    Target:
        node2
    Target-type:
        glob
    User:
        root
20211107120836082426:
    ----------
    Arguments:
        - uptime
    Function:
        cmd.run
    StartTime:
        2021, Nov 07 12:08:36.082426
    Target:
        node2
    Target-type:
        glob
    User:
        root
20211107120908256620:
    ----------
    Arguments:
    Function:
        runner.jobs.lookup_jid
    StartTime:
        2021, Nov 07 12:09:08.256620
    Target:
        master_master
    Target-type:
        list
    User:
        root
0

评论区