速云科技

SaltStack Job Cache替代Returnners

1.  Job Cache

通过使用Returners,我们可以将返回结果存放到我们指定的存储系统中,但是Returners的特性是由minion端直接连接对应的存储中,使用特定的Returners还可能需要在Minion安装相关的软件包,在大规模的分布式环境中由于网络等限制,该方式可能并不能完全满足我们的需求。

Salt提供了一种可以直接在Master端将返回存储下来的机制。在Master配置文件中有master_job_cache的选项,默认就是Mysql。我们打开就可以将Job的返回结果直接存入MySQL中。

安装MySQLdb依赖

[root@dev-node1 ~]# yum -y install MySQL-python

创建数据结构

mysql> create database salt DEFAULT CHARACTER SET utf8;

mysql> grant all on salt.* to salt@localhost identified by ‘salt’;

mysql> CREATE TABLE `jids` (

`jid` varchar(255) NOT NULL,

`load` mediumtext NOT NULL,

UNIQUE KEY `jid` (`jid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

mysql> 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;

修改Master配置文件

[root@dev-node1 ~]# vim /etc/salt/master

master_job_cache: mysql

mysql.host: ‘localhost’

mysql.user: ‘oms’

mysql.pass: ‘oms’

mysql.db: ‘oms’

mysql.port: 3306

重启salt-master并测试

[root@dev-node1 ~]# /etc/init.d/salt-master restart

[root@dev-node1 ~]# salt ‘*’ test.ping -v

Executing job with jid 20150327151929600941

——————————————-

 

dev-node1.linuxhot.com:

True

mysql> select * from jids\G;

*************************** 1. row ***************************

jid: 20150327151929600941

load: {“tgt_type”: “glob”, “jid”: “20150327151929600941”, “cmd”: “publish”, “tgt”: “*”, “kwargs”: {“show_timeout”: false, “show_jid”: false}, “ret”: “”, “user”: “root”, “arg”: [], “fun”: “test.ping”}

 

mysql> select * from salt_returns\G;

*************************** 1. row ***************************

fun: test.ping

jid: 20150327151929600941

return: true

id: dev-node1.linuxhot.com

success: 1

full_ret: {“fun_args”: [], “jid”: “20150327151929600941”, “return”: true, “retcode”: 0, “success”: true, “cmd”: “_return”, “_stamp”: “2015-03-27T15:19:29.906231”, “fun”: “test.ping”, “id”: “dev-node1.unixhot.com”}

alter_time: 2015-03-27 15:19:29

欢迎留言