六:Saltstack之十七:Saltmaster 将返回写入数据库以及job管理

阅读(894)

一:目的是在master端将minion的返回的结果直接写到数据库,默认minion的任务执行结果返回到master的,master会把job保存在/etc/salt/master配置文件中指定的#cachedir: /var/cache/salt/master,内容如下:

#keep_jobs: 24 #保存时间,如果服务器较多或任务较多可以减少时间

#timeout: 5 #命令执行超时时间,默认5秒

#publish_port: 4505 #默认消息发布端口

#ret_port: 4506 #默认数据返回端口

#job_cache: True #缓存job

#minion_data_cache: True #缓存minion的pillar和grains数据

1.1:默认的保存路径结架:

二:将返回写入数据库:

#官方文档:https://docs.saltstack.com/en/latest/ref/returners/all/salt.returners.mysql.html

2.1:数据库操作步骤:

2.1.1:创建数据库:

MariaDB [salt]> CREATE DATABASE  `salt1`  DEFAULT CHARACTER SET utf8  DEFAULT COLLATE utf8_general_ci;

2.1.2:切换数据库:

MariaDB [salt]> use salt1;

2.1.2:创建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;
4.创建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;

2.1.3:创建sakt_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;

2.1.4:创建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;

2.1.5:数据库授权:

MariaDB [salt1]> grant all privileges on salt1.* to  jack@"192.168.10.%" identified by '123456';  #授权从网络访问
MariaDB [(none)]> grant all privileges on salt1.* to  jack@"saltmaster.com.cn" identified by '123456'; #授权从本机访问
MariaDB [salt1]> flush privileges;

2.1.6:用户测试连接:

2.2:saltmaster配置:

2.2.1:编辑saltmaster配置文件,将返回写入数据库:

# vim /etc/salt/master #在最下方填写Mysql服务器信息:

master_job_cache: mysql
mysql.host: "192.168.10.101"
mysql.user: 'jack'
mysql.db: 'salt1'
mysql.pass: '123456'
mysql.port: 3306

2.2.2:重启saltmaster:

# systemctl  restart salt-master

2.2.3:安装MySQL-python插件:

# yum install MySQL-python

2.3:测试部分:

2.3.1:在saltmaster端执行的任何命令都将写入到数据库,如:

2.3.2:在master端执行一条远程执行命令:

# salt “*” cmd.run “df -TH”

2.3.3:验证数据库是否有数据:

MariaDB [salt1]> select * from salt_returns\G

二:job管理:

#job是有master端分发到minion的任务,每一个运行在minion端的任务都有一个job id,

#官方文档:https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.saltutil.html#module-salt.modules.saltutil

2.1:如何获取minion的job id:

2.2.1:在master端执行一个任务,在minion端休眠一段时间,在时间内查看job id:

#https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.saltutil.html#module-salt.modules.saltutil

#saltutil模块

[root@saltmaster ~]# salt “*” cmd.run “sleep 10 && echo sleep ok”

2.2.2:在另一个master中断查看job:

# salt “*” saltutil.running

2.2.3:在minion端查看job的路径:

# ll /var/cache/salt/minion/proc/         

2.2.4:job在master端的保存路径:

# ll /var/cache/salt/master/jobs

2.2.4:结束正在运行的job:

# salt “*” saltutil.kill_job  20160728134732585586


2.3:列出当前保存在cache当中历史执行过的job:

# salt-run  jobs.list_jobs

2.4:查询cache中某个执行过的历史job的执行结果:

# salt-run  jobs.lookup_jid 20160728122722257707

2.5:通过manag模块管理minion:

2.5.1:#查看所有minion的主机状态:

[root@saltmaster ~]# salt-run  manage.status
down:
up:
    - saltmaster.com.cn
    - saltminion.com.cn

2.5.2:查看所有minion当中在线的:

[root@saltmaster ~]# salt-run  manage.up
- saltmaster.com.cn
- saltminion.com.cn

2.5.3:查查所有minion当中离线的:

[root@saltmaster ~]# salt-run  manage.down
- saltminion.com.cn

2.5.4:查看minion的版本:

Master:
    2016.3.1
Up to date:
    ----------
    saltmaster.com.cn:
        2016.3.1
    saltminion.com.cn:
        2016.3.1

#更多模块使用方法参考官方文档:https://docs.saltstack.com/en/latest/salt-modindex.html


Warning: count(): Parameter must be an array or an object that implements Countable in /home/nginx/wordpress/wp-includes/class-wp-comment-query.php on line 405

发表评论

邮箱地址不会被公开。 必填项已用*标注