SaltStack组件
组件—return
return组件可以理解为SaltStack系统对执行Minion返回后的数据进行存储或者返回给其他程序,它支持多种存储方式,比如用MySQL、MongoDB、Redis、Memcache等,通过return我们可以对SaltStack的每次操作进行记录,对以后日志审计提供了数据来源。目前官方已经支持30种return数据存储与接口,我们可以很方便的配置与使用它。当然也支持自己定义的return,自定义的return需由python来编写。在选择和配置好要使用的return后,只需在salt命令后面指定return即可
[root@master ~]# salt 'minion' sys.list_returners
minion:
- carbon
- couchdb
- etcd
- highstate
- local
- local_cache
- mattermost
- multi_returner
- pushover
- rawfile_json
- slack
- slack_webhook
- smtp
- splunk
- sqlite3
- syslog
- telegram
常用的命令:
django (开发网站的,很高调)
etcd
kafka (大数据里面的)
redis (直接和redis对接)
zabbix (直接和zabbix对接,进行实时监控)
还要很多,就不一一列举了
return是在Master端触发任务,然后Minion接受处理任务后直接与return存储服务器建立连接,然后把数据return存到存储服务器。关于这点一定要注意,因为此过程都是Minion端操作存储服务器,所以要确保Minion端的配置跟依赖包是正确的(就是说如果我们要用数据库装包的话,就得需要使用return安装个依赖包,后面会说,大概了解下就好),这意味着我们将必须在每个Minion上安装指定的return方式依赖包,假如使用Mysql作为return存储方式,那么我们将在每台Minion上安装python-mysql模块
官方mysql链接如下(里面有更详细的步骤)https://docs.saltproject.io/en/latest/ref/returners/all/salt.returners.mysql.html#module-salt.returners.mysql
实例:
#在minion上安装Mysql-python模块
[root@master ~]# yum list all|grep -i mysql|grep -i python
python2-PyMySQL.noarch 0.8.0-10.module_el8.5.0+743+cd2f5d28 appstream
python3-PyMySQL.noarch 0.10.1-2.module_el8.5.0+761+faacb0fb appstream
python38-PyMySQL.noarch 0.10.1-1.module_el8.5.0+742+dbad1979 appstream
python39-PyMySQL.noarch 0.10.1-2.module_el8.5.0+738+dc19af12 appstream
[root@master ~]# salt 'minion' pkg.install python3-PyMySQL
minion:
----------
python3-PyMySQL:
----------
new:
0.10.1-2.module_el8.5.0+761+faacb0fb
old:
python3-cffi:
----------
new:
1.11.5-5.el8
old:
python3-cryptography:
----------
new:
3.2.1-5.el8
old:
python3-ply:
----------
new:
3.9-9.el8
old:
python3-pycparser:
----------
new:
2.14-14.el8
old:
#使用测试端下载mariadb,进行部署mysql服务器用作存储服务器
[root@localhost ~]# yum -y install mariadb mariadb-server
[root@localhost ~]# systemctl enable --now mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
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.001 sec)
MariaDB [(none)]>
MariaDB [(none)]> USE `salt`;
Database changed
MariaDB [salt]> DROP TABLE IF EXISTS `jids`;
Query OK, 0 rows affected, 1 warning (0.163 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.050 sec)
MariaDB [salt]> DROP TABLE IF EXISTS `salt_returns`;
Query OK, 0 rows affected, 1 warning (0.000 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.013 sec)
MariaDB [salt]> DROP TABLE IF EXISTS `salt_events`;
Query OK, 0 rows affected, 1 warning (0.001 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.012 sec)
#上面编写完成后,查看表
MariaDB [salt]> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids |
| salt_events |
| salt_returns |
+----------------+
3 rows in set (0.000 sec)
MariaDB [salt]> desc jids;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| jid | varchar(255) | NO | PRI | NULL | |
| load | mediumtext | NO | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.002 sec)
#进行授权后刷新
MariaDB [salt]> grant all on salt.* to 'salt'@'%' identified by 'salt';
Query OK, 0 rows affected (0.000 sec)
MariaDB [salt]> flush privileges;
Query OK, 0 rows affected (0.000 sec)
上面用到的命令如下:
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;
--
-- 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;
#使用minion主机测试能否登录到测试端
[root@minion ~]# yum -y install mariadb
[root@minion ~]# mysql -usalt -psalt -h192.168.230.132
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
[root@minion1 ~]# vim /etc/salt/minion
......省
# - slack
mysql.host: '192.168.230.132' #主机是MySQL存储服务器主机
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
[root@minion1 ~]# systemctl restart salt-minion
#测试下是否可以在master主机上ping通
[root@master ~]# salt minion1 test.ping
minion1:
True
#在Master上测试存储到mysql中
[root@master ~]# salt 'minion' test.ping
minion:
True
#在Master上测试存储到mysql中
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, 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]> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids |
| salt_events |
| salt_returns |
+----------------+
3 rows in set (0.000 sec)
MariaDB [salt]> select * from salt_returns;
Empty set (0.001 sec)
#在Master上测试存储到mysql中
#测试下是否可以添加东西
[root@master ~]# salt 'minion' test.ping --return mysql
minion:
True
#在数据库中查询
MariaDB [salt]> select * from salt_returns\G
*************************** 1. row ***************************
fun: test.ping
jid: 20211107082816727379
return: true
id: minion
success: 1
full_ret: {"success": true, "return": true, "retcode": 0, "jid": "20211107082816727379", "fun": "test.ping", "fun_args": [], "id": "minion"}
alter_time: 2021-11-07 03:28:09
1 row in set (0.001 sec)
组件—job_cache
流程:
上面是需要在每台minion上安装指定模块(例如python-mysql),那么我们能不能直接使用在master上就把返回的结果给存储到存储服务器呢?
当然,这种方式被称作 job cache 。意思是当Minion将结果返回给Master后,由Master将结果给缓存在本地,然后将缓存的结果给存储到指定的存储服务器,比如存储到mysql中
#以下方式也行,但是看不懂,所以需要使用job cache进行操作
[root@master ~]# ls /var/cache/salt/master/jobs/
01 16 25 3b 48 56 64 77 86 94 a7 b7 c4 d1 e7 f6
02 18 29 3c 49 57 65 7a 87 97 a8 b8 c5 d2 e8 f7
04 19 2b 3d 4b 58 67 7b 88 99 a9 ba c6 d3 e9 f8
06 1a 2c 3e 4c 59 68 7c 89 9b ae bb c7 d6 eb fa
09 1c 2d 3f 4e 5b 6e 7d 8b 9f b0 bc c8 d7 ec fc
0a 1f 2e 41 4f 5c 6f 7e 8c a0 b1 be ca d8 ed fe
0d 20 32 42 50 5d 70 7f 8d a1 b2 bf cb da ee
0f 21 35 43 51 5e 71 81 8e a2 b3 c0 cd de ef
13 22 36 44 53 5f 73 83 90 a3 b4 c1 ce e3 f0
14 23 37 45 54 60 75 84 91 a4 b5 c2 cf e4 f1
15 24 39 47 55 61 76 85 92 a6 b6 c3 d0 e6 f5
[root@master ~]# vim /etc/salt/master
#job_cache: True #这是开启默认数据存放得位置
master_job_cache: mysql #相当于上面return上那个没有注释的那个,添加此项后数据默认存到MySQL
mysql.host: '192.168.230.132'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
#测试master能否连到mysql储存端
[root@master ~]# yum -y install mariadb
[root@master ~]# which mysql
/usr/bin/mysql
[root@master ~]# mysql -usalt -psalt -h192.168.230.132
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#使用return时需要所有minion安装python3-MyMSQL包,但是使用job只需要master装,千万不要忘记,会报错
[root@master ~]# yum -y install python3-PyMySQL
#在数据库服务器中清空表内容
MariaDB [salt]> delete from salt_returns;
Query OK, 1 row affected (0.001 sec)
MariaDB [salt]> select * from salt_returns\G
Empty set (0.000 sec)
#在master上再次测试能否存储至数据库
[root@master ~]# salt 'minion' cmd.run 'cat /root/123'
minion:
奶茶
#在数据库中查询
MariaDB [salt]> select * from salt_returns\G
*************************** 1. row ***************************
fun: cmd.run
jid: 20211107081950215279
return: "奶茶"
id: minion
success: 1
full_ret: {"cmd": "_return", "id": "minion", "success": true, "return": "奶茶", "retcode": 0, "jid": "20211107081950215279", "fun": "cmd.run", "fun_args": ["cat /root/123"], "_stamp": "2021-11-07 17:33:52.684948"}
alter_time: 2021-11-07 17:33:52
1 row in set (0.000 sec)
job管理
#获取任务的jid
##可以显示任务的Jid,-v 显示命令执行的详细过程
[root@master ~]# salt 'minion' cmd.run 'uptime' -v
Executing job with jid 20211107083848344928
-------------------------------------------
minion:
16:38:48 up 2:50, 2 users, load average: 0.12, 0.14, 0.17
#通过jid获取此任务的返回结果
[root@master ~]# salt-run jobs.lookup_jid 20211107083848344928
minion:
16:38:48 up 2:50, 2 users, load average: 0.12, 0.14, 0.17
当作业被执行时,返回的数据被发送回主服务器并被缓存。默认情况下,它会缓存 24 小时,但这可以通过keep_jobs主配置中的 选项进行配置。使用 lookup_jid runner 将显示与使用 salt 命令进行初始作业调用将显示的相同返回数据。
[root@master ~]# salt-run jobs.lookup_jid 20211107083848344928
minion:
16:38:48 up 2:50, 2 users, load average: 0.12, 0.14, 0.17
jobs.list_jobs
在查找历史工作之前,可能需要查找工作 ID。list_jobs 将解析缓存的执行数据并显示已经或部分返回的作业的所有作业数据。
[root@master ~]# salt-run jobs.list_jobs
20211107080229734881:
----------
Arguments:
- python3-PyMySQL
Function:
pkg.install
StartTime:
2021, Nov 07 08:02:29.734881
Target:
minion
Target-type:
glob
User:
root
20211107080234840388:
----------
Arguments:
- 20211107080229734881
Function:
saltutil.find_job
StartTime:
2021, Nov 07 08:02:34.840388
Target:
- minion
Target-type:
list
User:
root
20211107080244980907:
----------
Arguments:
- 20211107080229734881
Function:
saltutil.find_job
StartTime:
2021, Nov 07 08:02:44.980907
Target:
- minion
Target-type:
list
User:
root
20211107080255021194:
----------
Arguments:
- 20211107080229734881
Function:
saltutil.find_job
StartTime:
2021, Nov 07 08:02:55.021194
Target:
- minion
Target-type:
list
User:
root
20211107080305087053:
----------
Arguments:
- 20211107080229734881
Function:
saltutil.find_job
StartTime:
2021, Nov 07 08:03:05.087053
Target:
- minion
Target-type:
list
User:
root
----------