saltstack将数据返回到mysql服务器:
1、在master上安装mysql-server
<code>[root@master ~]</code><code># yum -y install mysql-server</code>
<code>[root@master ~]</code><code># service mysqld start && chkconfig mysqld on</code>
2、创建相关的表结构
<code>[root@master ~]</code><code># mysql</code>
<code>mysql></code>
<code>CREATE DATABASE `salt`</code>
<code> </code><code>DEFAULT CHARACTER SET utf8</code>
<code> </code><code>DEFAULT COLLATE utf8_general_ci;</code>
<code>USE `salt`;</code>
<code>--</code>
<code>-- Table structure </code><code>for</code> <code>table `jids`</code>
<code>DROP TABLE IF EXISTS `jids`;</code>
<code>CREATE TABLE `jids` (</code>
<code> </code><code>`jid` varchar(255) NOT NULL,</code>
<code> </code><code>`load` mediumtext NOT NULL,</code>
<code> </code><code>UNIQUE KEY `jid` (`jid`)</code>
<code>) ENGINE=InnoDB DEFAULT CHARSET=utf8;</code>
<code>CREATE INDEX jid ON jids(jid) USING BTREE;</code>
<code>-- Table structure </code><code>for</code> <code>table `salt_returns`</code>
<code>DROP TABLE IF EXISTS `salt_returns`;</code>
<code>CREATE TABLE `salt_returns` (</code>
<code> </code><code>`fun` varchar(50) NOT NULL,</code>
<code> </code><code>`</code><code>return</code><code>` mediumtext NOT NULL,</code>
<code> </code><code>`</code><code>id</code><code>` varchar(255) NOT NULL,</code>
<code> </code><code>`success` varchar(10) NOT NULL,</code>
<code> </code><code>`full_ret` mediumtext NOT NULL,</code>
<code> </code><code>`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,</code>
<code> </code><code>KEY `</code><code>id</code><code>` (`</code><code>id</code><code>`),</code>
<code> </code><code>KEY `jid` (`jid`),</code>
<code> </code><code>KEY `fun` (`fun`)</code>
<code>-- Table structure </code><code>for</code> <code>table `salt_events`</code>
<code>DROP TABLE IF EXISTS `salt_events`;</code>
<code>CREATE TABLE `salt_events` (</code>
<code>`</code><code>id</code><code>` BIGINT NOT NULL AUTO_INCREMENT,</code>
<code>`tag` varchar(255) NOT NULL,</code>
<code>`data` mediumtext NOT NULL,</code>
<code>`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,</code>
<code>`master_id` varchar(255) NOT NULL,</code>
<code>PRIMARY KEY (`</code><code>id</code><code>`),</code>
<code>KEY `tag` (`tag`)</code>
<code>说明:Required python modules: MySQLdb</code>
3、mysql授权
<code>mysql> grant all on salt.* to salt@</code><code>'%'</code> <code>identified by </code><code>'salt'</code><code>;</code>
4、客户端安装MySQL-python
<code>[root@master ~]</code><code># salt '*' cmd.run 'yum -y install MySQL-python'</code>
<code>[root@node01 ~]</code><code># mysql -u satl -p -h 10.10.10.140</code>
<code>Enter password: </code>
<code>ERROR 1045 (28000): Access denied </code><code>for</code> <code>user </code><code>'satl'</code><code>@</code><code>'node01'</code> <code>(using password: YES)</code>
5、客户端进行连接测试
<code>[root@node01 ~]</code><code># mysql -usalt -psalt -h 10.10.10.140</code>
<code>Welcome to the MySQL monitor. Commands end with ; or \g.</code>
<code>Your MySQL connection </code><code>id</code> <code>is 5</code>
<code>Server version: 5.1.73 Source distribution</code>
<code>Copyright (c) 2000, 2013, Oracle and</code><code>/or</code> <code>its affiliates. All rights reserved.</code>
<code>Oracle is a registered trademark of Oracle Corporation and</code><code>/or</code> <code>its</code>
<code>affiliates. Other names may be trademarks of their respective</code>
<code>owners.</code>
<code>Type </code><code>'help;'</code> <code>or </code><code>'\h'</code> <code>for</code> <code>help. Type </code><code>'\c'</code> <code>to </code><code>clear</code> <code>the current input statement.</code>
<code>mysql> show databases;</code>
<code>+--------------------+</code>
<code>| Database |</code>
<code>| information_schema |</code>
<code>| salt |</code>
<code>| </code><code>test</code> <code>|</code>
<code>3 rows </code><code>in</code> <code>set</code> <code>(0.01 sec)</code>
<code>mysql> use salt;</code>
<code>Reading table information </code><code>for</code> <code>completion of table and column names</code>
<code>You can turn off this feature to get a quicker startup with -A</code>
<code>Database changed</code>
<code>mysql> show tables;</code>
<code>+----------------+</code>
<code>| Tables_in_salt |</code>
<code>| jids |</code>
<code>| salt_events |</code>
<code>| salt_returns |</code>
<code>3 rows </code><code>in</code> <code>set</code> <code>(0.00 sec)</code>
6、配置master及minion端相关配置
<code>[root@master ~]</code><code># vim /etc/salt/master</code>
<code>mysql.host: </code><code>'10.10.10.140'</code>
<code>mysql.user: </code><code>'salt'</code>
<code>mysql.pass: </code><code>'salt'</code>
<code>mysql.db: </code><code>'salt'</code>
<code>mysql.port: 3306</code>
<code>[root@master ~]</code><code># /etc/init.d/salt-master restart</code>
<code>Stopping salt-master daemon: [确定]</code>
<code>Starting salt-master daemon: [确定]</code>
<code>[root@node01 ~]</code><code># vim /etc/salt/minion</code>
<code>[root@node01 ~]</code><code># /etc/init.d/salt-minion restart</code>
<code>Stopping salt-minion daemon: [确定]</code>
<code>Starting salt-minion daemon: [确定]</code>
7、测试返回值到mysql的情况
<code>[root@master ~]</code><code># salt '*' cmd.run 'df' --return mysql</code>
<code>node02.saltstack.com:</code>
<code> </code><code>Filesystem 1K-blocks Used Available Use% Mounted on</code>
<code> </code><code>/dev/sda5</code> <code>14114736 8728708 4662368 66% /</code>
<code> </code><code>tmpfs 953964 12 953952 1% </code><code>/dev/shm</code>
<code> </code><code>/dev/sda1</code> <code>194241 42160 141841 23% </code><code>/boot</code>
<code> </code><code>/dev/sda3</code> <code>1998672 18204 1875612 1% </code><code>/tmp</code>
<code>node01.saltstack.com:</code>
<code> </code><code>/dev/sda5</code> <code>14114736 8729432 4661644 66% /</code>
<code> </code><code>tmpfs 953964 84 953880 1% </code><code>/dev/shm</code>
<code> </code><code>/dev/sda3</code> <code>1998672 18224 1875592 1% </code><code>/tmp</code>
测试结果:
<code>mysql> </code><code>select</code> <code>* from salt_returns\G;</code>
<code>*************************** 1. row ***************************</code>
<code> </code><code>fun: cmd.run</code>
<code> </code><code>jid: 20170220120522696829</code>
<code> </code><code>return</code><code>: </code><code>"Filesystem 1K-blocks Used Available Use% Mounted on\n/dev/sda5 14114736 8729432 4661644 66% /\ntmpfs 953964 84 953880 1% /dev/shm\n/dev/sda1 194241 42160 141841 23% /boot\n/dev/sda3 1998672 18224 1875592 1% /tmp"</code>
<code> </code><code>id</code><code>: node01.saltstack.com</code>
<code> </code><code>success: 1</code>
<code> </code><code>full_ret: {</code><code>"fun_args"</code><code>: [</code><code>"df"</code><code>], </code><code>"jid"</code><code>: </code><code>"20170220120522696829"</code><code>, </code><code>"return"</code><code>: </code><code>"Filesystem 1K-blocks Used Available Use% Mounted on\n/dev/sda5 14114736 8729432 4661644 66% /\ntmpfs 953964 84 953880 1% /dev/shm\n/dev/sda1 194241 42160 141841 23% /boot\n/dev/sda3 1998672 18224 1875592 1% /tmp"</code><code>, </code><code>"retcode"</code><code>: 0, </code><code>"success"</code><code>: </code><code>true</code><code>, </code><code>"fun"</code><code>: </code><code>"cmd.run"</code><code>, </code><code>"id"</code><code>: </code><code>"node01.saltstack.com"</code><code>}</code>
<code>alter_time: 2017-02-20 12:05:24</code>
<code>*************************** 2. row ***************************</code>
<code> </code><code>jid: 20170220120528485243</code>
<code> </code><code>full_ret: {</code><code>"fun_args"</code><code>: [</code><code>"df"</code><code>], </code><code>"jid"</code><code>: </code><code>"20170220120528485243"</code><code>, </code><code>"return"</code><code>: </code><code>"Filesystem 1K-blocks Used Available Use% Mounted on\n/dev/sda5 14114736 8729432 4661644 66% /\ntmpfs 953964 84 953880 1% /dev/shm\n/dev/sda1 194241 42160 141841 23% /boot\n/dev/sda3 1998672 18224 1875592 1% /tmp"</code><code>, </code><code>"retcode"</code><code>: 0, </code><code>"success"</code><code>: </code><code>true</code><code>, </code><code>"fun"</code><code>: </code><code>"cmd.run"</code><code>, </code><code>"id"</code><code>: </code><code>"node01.saltstack.com"</code><code>}</code>
<code>alter_time: 2017-02-20 12:05:28</code>
<code>2 rows </code><code>in</code> <code>set</code> <code>(0.00 sec)</code>
<code>ERROR: </code>
<code>No query specified</code>
8、如果每次执行不加--return=mysql,由master端将返回的数据写入数据库
<code>master_job_cache: mysql</code>
<code>注意:如果配置了</code><code>/etc/salt/roster</code><code>的话,</code>
<code>要注释掉master_job_cache: mysql,否则会有冲突</code>
<code>#master_job_cache: mysql</code>
如果之前已经配置过salt-ssh的话,在设置master_job_cache: mysql的话,就会出现报错信息:
<code>[root@master salt]</code><code># salt 'node01.saltstack.com' cmd.run 'ls -ld /' </code>
<code>Failed to allocate a jid. The requested returner </code><code>'mysql'</code> <code>could not be loaded.</code>
<code>我暂时没找到解决办法</code>
其它报错信息:
<code>Salt request timed out. The master is not responding. If this error persists after verifying the master is up, worker_threads may need to be increased.</code>
<code>可以尝试在</code><code>/etc/salt/master</code><code>中修改worker_threads的值</code>
<code>#worker_threads: 5</code>
<code>worker_threads: 20</code>
<code>如果仍旧报错,可能是网络的问题,重新执行几次,可能就恢复了</code>
参考资料:http://docs.saltstack.cn/ref/returners/all/salt.returners.mysql.html#module-salt.returners.mysql
本文转自 冰冻vs西瓜 51CTO博客,原文链接:http://blog.51cto.com/molewan/1899474,如需转载请自行联系原作者