SaltStack的ruturn与job组件

文章目录

    • SaltStack组件之return
      • 1.1 return流程
      • 1.2 使用mysql作为return存储方式
    • 2. job cache
      • 2.1 job cache流程

SaltStack组件之return

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

[root@salt1 ~]# salt 'salt2' sys.list_returners
salt2:
    - carbon
    - couchdb
    - elasticsearch
    - 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@salt1 ~]# salt 'salt2' pkg.install python36-mysql.x86_64 
.......
  python36-mysql:
        ----------
        new:
            1.3.12-2.el7
        old:
[root@salt1 ~]# salt 'salt2' cmd.run 'rpm -qa|grep python36-mysql'
salt2:
    python36-mysql-1.3.12-2.el7.x86_64

部署一台mysql服务器用作存储服务器,ip;192.168.174.88

[root@return ~]# yum -y install mariadb-server
[root@return ~]# systemctl start mariadb
[root@return ~]# systemctl enable mariadb
[root@return ~]# ss -tanl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128     *:22                  *:*                  
LISTEN      0      100    127.0.0.1:25                  *:*                  
LISTEN      0      50      *:3306                *:*                  
LISTEN      0      128    :::22                 :::*                  
LISTEN      0      100       ::1:25                 :::*                  

//创建数据库和表
[root@return ~]# mysql
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.01 sec)

MariaDB [(none)]>
MariaDB [(none)]> USE `salt`;
Database changed
MariaDB [salt]>
MariaDB [salt]> --
MariaDB [salt]> -- Table structure for table `jids`
MariaDB [salt]> --
MariaDB [salt]>
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.02 sec)

MariaDB [salt]> CREATE INDEX jid ON jids(jid) USING BTREE;
ERROR 1061 (42000): Duplicate key name 'jid'
MariaDB [salt]>
MariaDB [salt]> drop database salt;
Query OK, 1 row affected (0.02 sec)

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

MariaDB [(none)]>
MariaDB [(none)]> USE `salt`;
 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;Database changed
MariaDB [salt]>
MariaDB [salt]> --
MariaDB [salt]> -- Table structure for table `jids`
MariaDB [salt]> --
MariaDB [salt]>
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.01 sec)

MariaDB [salt]> CREATE INDEX jid ON jids(jid) USING BTREE;
ERROR 1061 (42000): Duplicate key name 'jid'
MariaDB [salt]>
MariaDB [salt]> --
MariaDB [salt]> -- Table structure for table `salt_returns`
MariaDB [salt]> --
MariaDB [salt]>
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(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.02 sec)

MariaDB [salt]>
MariaDB [salt]> --
MariaDB [salt]> -- Table structure for table `salt_events`
MariaDB [salt]> --
MariaDB [salt]>
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.06 sec)

MariaDB [salt]> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids           |
| salt_events    |
| salt_returns   |
+----------------+
3 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

[root@salt2 ~]# vim /etc/salt/minion
...
mysql.host: '192.168.174.88'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
...
[root@salt2 ~]# systemctl restart salt-minion

在master端输入命令测试

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

在存储服务器数据库中查询

MariaDB [salt]> select * from salt_returns\G
*************************** 1. row ***************************
       fun: test.ping
       jid: 20200229111430762678
    return: true
        id: salt2
   success: 1
  full_ret: {"fun_args": [], "jid": "20200229111430762678", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "salt2"}
alter_time: 2020-02-29 19:14:30
1 row in set (0.00 sec)

2. job cache

2.1 job cache流程

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

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

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

[root@salt1 ~]# systemctl restart salt-master

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

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

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

MariaDB [salt]> 

在master端输入命令测试


你可能感兴趣的:(Linux课程)