SaltStack之return与job管理

return介绍

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

查看所有return列表
[root@node01 ~]# salt 'node02' sys.list_returners
node02:
    - carbon
    - couchdb
    - elasticsearch
    - 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模块
环境说明

主机名IP应用
node01192.168.175.150salt-master
node02192.168.175.151mariadb
node03192.168.175.152salt-minion
测试是否能够ping通
[root@node01 ~]# salt '*' test.ping
node03:
    True

minion安装	MySQL-Python
[root@node03 ~]# yum list all|grep -i mysql-python
MySQL-python.x86_64                         1.2.5-1.el7                base     
[root@node03 ~]# yum -y install MySQL-python

安装数据库
[root@node02 ~]# yum -y install mariadb*
启动
[root@node02 ~]# systemctl enable --now mariadb

配置
[root@node02 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.02 sec)

MariaDB [(none)]> CREATE DATABASE  `salt`
    ->   DEFAULT CHARACTER SET utf8
    ->   DEFAULT COLLATE utf8_general_ci;
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> 
MariaDB [(none)]> USE `salt`;
Database changed
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.04 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.01 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.01 sec)

MariaDB [salt]> 

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

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

配置minion

[root@node03 ~]# vim /etc/salt/minion
添加下面内容,IP为安装mysql的IP
mysql.host: '192.168.175.151'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
重启服务
[root@node03 ~]# systemctl restart salt-minion

在master上测试存储到mysql中

[root@node01 ~]# salt '*' test.ping --return mysql
node03:
    True

在数据库中查询

可以看到上一条执行的已经被写入到mysql中
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]> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids           |
| salt_events    |
| salt_returns   |
+----------------+
3 rows in set (0.00 sec)

MariaDB [salt]> select * from salt_returns\G
*************************** 1. row ***************************
       fun: test.ping
       jid: 20200824132809702304
    return: true
        id: node03
   success: 1
  full_ret: {"fun_args": [], "jid": "20200824132809702304", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "node03"}
alter_time: 2020-08-24 21:28:09
1 row in set (0.00 sec)

job cache

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

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

[root@node01 ~]# yum -y install MySQL-python
[root@node01 ~]# vim /etc/salt/master
最后添加,把mysqlhost改为mysql的IP
master_job_cache: mysql
mysql.host: '192.168.20.151'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306

重启服务
[root@node01 ~]# systemctl restart salt-master

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

[root@node01 ~]# salt 'node03' test.ping 
node03:
    True

mysql上面查询

MariaDB [salt]> select * from salt_returns\G
*************************** 1. row ***************************
       fun: test.ping
       jid: 20200824142653111106
    return: true
        id: node03
   success: 1
  full_ret: {"fun_args": [], "jid": "20200824142653111106", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "node03"}
alter_time: 2020-08-24 22:26:53
1 row in set (0.00 sec)

MariaDB [salt]> select * from salt_returns\G
*************************** 1. row ***************************
       fun: test.ping
       jid: 20200824142653111106
    return: true
        id: node03
   success: 1
  full_ret: {"fun_args": [], "jid": "20200824142653111106", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "node03"}
alter_time: 2020-08-24 22:26:53
*************************** 2. row ***************************
       fun: test.ping
       jid: 20200824142853190049
    return: true
        id: node03
   success: 1
  full_ret: {"fun_args": [], "jid": "20200824142853190049", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2020-08-24T14:28:53.350187", "fun": "test.ping", "id": "node03"}
alter_time: 2020-08-24 22:28:53
2 rows in set (0.00 sec)

job管理
获取任务的jid

[root@node01 ~]# salt '*' cmd.run 'uptime' -v
Executing job with jid 20200824143313912286
-------------------------------------------

node03:
     22:33:14 up  7:44,  3 users,  load average: 0.01, 0.04, 0.07

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

[root@node01 ~]# salt-run jobs.lookup_jid 20200824143313912286
node03:
     22:33:14 up  7:44,  3 users,  load average: 0.01, 0.04, 0.07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值