利用MySQL存储SaltStack推送信息

在server1(master)端向minion端推送之后,minion端会保存推送信息24小时,可是为了日后便于查看推送的信息,我们应该将这些信息长期存储起来,而存放在数据库中是个不错的选择

这里要先确保server1开启了salt-master
server2和server3开启了salt-minion如果未开启的话先开启
服务部署:

1.在server1端安装 mariadb-server和MySQL-python

[[email protected] ~]# yum install -y mariadb-server
[[email protected] ~]# yum install -y MySQL-python

2.在mysql中为其他节点上的用户授权

[[email protected] ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> grant all on salt.* to [email protected]'%' identified by 'westos';
Query OK, 0 rows affected (0.01 sec)

3.在mysql中导入数据,添加推送信息模块

[[email protected] ~]# vim add.sql
CREATE DATABASE  `salt`
  DEFAULT CHARACTER SET utf8
  DEFAULT COLLATE utf8_general_ci;

USE `salt`;

--
-- Table structure for table `jids`
--

DROP TABLE IF EXISTS `jids`;
CREATE TABLE `jids` (
  `jid` varchar(255) NOT NULL,
  `load` mediumtext NOT NULL,
  UNIQUE KEY `jid` (`jid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#CREATE INDEX jid ON jids(jid) USING BTREE;		##这一行一定要注释掉,否则可能会出错

--
-- Table structure for table `salt_returns`
--

DROP TABLE IF EXISTS `salt_returns`;
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;

--
-- Table structure for table `salt_events`
--

DROP TABLE IF EXISTS `salt_events`;
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;

[[email protected] ~]# mysql < add.sql

推送其他节点的信息
1.在server2上minion的配置文件中添加推送数据库主机

[[email protected] minion]# cd /etc/salt/
[[email protected] salt]# vim minion
876 mysql.host: '172.25.4.1'
877 mysql.user: 'salt'
878 mysql.pass: 'westos'
879 mysql.db: 'salt'
880 mysql.port: 3306
[[email protected] ~]# systemctl restart salt-minion

2.测试

[[email protected] ~]# salt '*' test.ping --return mysql
server3:
    True
server2:
    True
[[email protected] ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, 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]> select * from salt_returns;
+-----------+----------------------+--------+---------+---------+-------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| fun       | jid                  | return | id      | success | full_ret                                                                                                                            | alter_time          |
+-----------+----------------------+--------+---------+---------+-------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| test.ping | 20190618175110150692 | true   | server2 | 1       | {"fun_args": [], "jid": "20190618175110150692", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "server2"} | 2019-06-18 17:51:11 |
+-----------+----------------------+--------+---------+---------+-------------------------------------------------------------------------------------------------------------------------------------+---------------------+
1 row in set (0.00 sec)
可以看出这里只记录了server2的推送信息

在这里插入图片描述
推送主节点下的所有从节点的信息到mysql
1.在master配置文件中添加所要导入的mysql数据库的信息

[[email protected] ~]# vim /etc/salt/master
1261 mysql.host: 'localhost'
1262 mysql.user: 'salt'
1263 mysql.pass: 'westos'
1264 mysql.db: 'salt'
1265 mysql.port: 3306
1266 
1267 master_job_cache: mysql
[[email protected] salt]# systemctl restart salt-master

2.在mysql中对本地用户授权

[[email protected] jobs]# mysql 
MariaDB [(none)]> grant all on salt.* to [email protected]'localhost' identified by 'westos';

3.测试查看

[[email protected] ~]# salt '*' cmd.run 'hostname'
server2:
    server2
server3:
    server3
[[email protected] ~]# salt server3 test.ping
server3:
    True
[[email protected] ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 30
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, 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]>  select * from salt_returns;
+-----------+----------------------+-----------+---------+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| fun       | jid                  | return    | id      | success | full_ret                                                                                                                                                                                                   | alter_time          |
+-----------+----------------------+-----------+---------+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| test.ping | 20190618175110150692 | true      | server2 | 1       | {"fun_args": [], "jid": "20190618175110150692", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "server2"}                                                                        | 2019-06-18 17:51:11 |
| cmd.run   | 20190618175512563837 | "server2" | server2 | 1       | {"fun_args": ["hostname"], "jid": "20190618175512563837", "return": "server2", "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2019-06-18T09:55:12.700642", "fun": "cmd.run", "id": "server2"} | 2019-06-18 17:55:12 |
| cmd.run   | 20190618175512563837 | "server3" | server3 | 1       | {"fun_args": ["hostname"], "jid": "20190618175512563837", "return": "server3", "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2019-06-18T09:55:12.706330", "fun": "cmd.run", "id": "server3"} | 2019-06-18 17:55:12 |
| test.ping | 20190618175526922053 | true      | server3 | 1       | {"fun_args": [], "jid": "20190618175526922053", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2019-06-18T09:55:27.048644", "fun": "test.ping", "id": "server3"}              | 2019-06-18 17:55:27 |
+-----------+----------------------+-----------+---------+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+
4 rows in set (0.02 sec)
我们没有设置server3上的minion配置文件,但也做了推送,说明主节点的所有节点信息都推送成功

在这里插入图片描述

原文链接:加载失败,请重新获取