SaltStack之return与job管理

SaltStack之return与job管理

1. SaltStack组件之return

​ return组件可以理解为SaltStack系统对执行Minion返回后的数据进行存储或者返回给其他程序,它支持多种存储方式,比如用MySQL、MongoDB、Redis、Memcache等,通过return我们可以对SaltStack的每次操作进行记录,对以后日志审计提供了数据来源。目前官方已经支持30种return数据存储与接口,我们可以很方便的配置与使用它。当然也支持自己定义的return,自定义的return需由python来编写。在选择和配置好要使用的return后,只需在salt命令后面指定return即可。

# 查看node1上所有return列表
[root@master ~]# salt 'node1' sys.list_returners
node1:
    - carbon
    - couchdb
    - etcd
    - highstate
    - local
    - local_cache
    - mattermost
    - multi_returner
    - pushover
    - rawfile_json
    - slack
    - slack_webhook
    - smtp
    - splunk
    - sqlite3
    - syslog
    - telegram
1.1 return流程

​ return是在Master端触发任务,然后Minion接受处理任务后直接与return存储服务器建立连接,然后把数据return存到存储服务器。关于这点一定要注意,因为此过程都是Minion端操作存储服务器,所以要确保Minion端的配置跟依赖包是正确的,这意味着我们将必须在每个Minion上安装指定的return方式依赖包,假如使用Mysql作为return存储方式,那么我们将在每台Minion上安装python-mysql模块。

1.2 使用mysql作为return存储方式

在这里插入图片描述

环境说明:

主机IP服务
master192.168.220.9salt-master
minion(node1)192.168.220.10salt-minion mariadb
mariadb192.168.220.17mariadb-server mariadb

三台关闭防火墙并改名

# master
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce 
Permissive

[root@localhost ~]# sed -ri 's/(^SELINUX=).*/\1disabled/' /etc/selinux/config

[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# bash
[root@master ~]#



# node1
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive

[root@localhost ~]# sed -ri 's/(^SELINUX=).*/\1disabled/' /etc/selinux/config

[root@localhost ~]#  hostnamectl set-hostname node1
[root@localhost ~]# bash
[root@node1 ~]#



# mariadb
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@localhost ~]# setenforce 0
[root@localhost ~]#  getenforce
Permissive

[root@localhost ~]# sed -ri 's/(^SELINUX=).*/\1disabled/' /etc/selinux/config

[root@localhost ~]# hostnamectl set-hostname mariadb
[root@localhost ~]# bash
[root@mariadb ~]#

master上安装服务

# 下载仓库
[root@master ~]# rpm --import https://repo.saltproject.io/py3/redhat/8/x86_64/latest/SALTSTACK-GPG-KEY.pub

[root@master ~]# curl -fsSL https://repo.saltproject.io/py3/redhat/8/x86_64/latest.repo | sudo tee /etc/yum.repos.d/salt.repo
[salt-latest-repo]
name=Salt repo for RHEL/CentOS 8 PY3
baseurl=https://repo.saltproject.io/py3/redhat/8/x86_64/latest
skip_if_unavailable=True
failovermethod=priority
enabled=1
enabled_metadata=1
gpgcheck=1
gpgkey=https://repo.saltproject.io/py3/redhat/8/x86_64/latest/SALTSTACK-GPG-KEY.pub

[root@master ~]# ls /etc/yum.repos.d/
CentOS-Stream-AppStream.repo         CentOS-Stream-Media.repo
CentOS-Stream-BaseOS.repo            CentOS-Stream-PowerTools.repo
CentOS-Stream-Debuginfo.repo         CentOS-Stream-RealTime.repo
CentOS-Stream-Extras.repo            salt.repo
CentOS-Stream-HighAvailability.repo

# 安装服务salt-master
[root@master ~]# yum -y install salt-master
......

# 启动
[root@master ~]# systemctl start salt-master
[root@master ~]# ss -antl
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       128            0.0.0.0:4505        0.0.0.0:*            
LISTEN 0       128            0.0.0.0:4506        0.0.0.0:*            
LISTEN 0       128               [::]:22             [::]:*

node1上安装服务

# 下载仓库
[root@node1 ~]# rpm --import https://repo.saltproject.io/py3/redhat/8/x86_64/latest/SALTSTACK-GPG-KEY.pub

[root@node1 ~]# curl -fsSL https://repo.saltproject.io/py3/redhat/8/x86_64/latest.repo | sudo tee /etc/yum.repos.d/salt.repo
[salt-latest-repo]
name=Salt repo for RHEL/CentOS 8 PY3
baseurl=https://repo.saltproject.io/py3/redhat/8/x86_64/latest
skip_if_unavailable=True
failovermethod=priority
enabled=1
enabled_metadata=1
gpgcheck=1
gpgkey=https://repo.saltproject.io/py3/redhat/8/x86_64/latest/SALTSTACK-GPG-KEY.pub

[root@node1 ~]# ls /etc/yum.repos.d/
CentOS-Stream-AppStream.repo         CentOS-Stream-Media.repo
CentOS-Stream-BaseOS.repo            CentOS-Stream-PowerTools.repo
CentOS-Stream-Debuginfo.repo         CentOS-Stream-RealTime.repo
CentOS-Stream-Extras.repo            salt.repo
CentOS-Stream-HighAvailability.repo

# 安装服务salt-minion, python3-PyMySQL mariadb
[root@node1 ~]# yum -y install salt-minion
....
[root@node1 ~]# yum -y install python3-PyMySQL
......


# 修改配置文件
[root@node1 ~]# vim /etc/salt/minion
......
15 # resolved, then the minion will fail to start.
16 #master: salt
17 master: 192.168.220.9 # IP是master的,前面有一个空格
.......

# 启动salt-minion
[root@node1 ~]# systemctl start salt-minion
[root@node1 ~]# systemctl status salt-minion
● salt-minion.service - The Salt Minion
   Loaded: loaded (/usr/lib/systemd/system/salt-minion.service; disabl>
   Active: active (running) since Fri 2021-11-05 06:08:37 EDT; 6s ago
     Docs: man:salt-minion(1)
           file:///usr/share/doc/salt/html/contents.html
           https://docs.saltproject.io/en/latest/contents.html
 Main PID: 89941 (salt-minion)
    Tasks: 15 (limit: 11201)
   Memory: 81.8M
   CGroup: /system.slice/salt-minion.service
           ├─89941 /usr/bin/python3.6 /usr/bin/salt-minion
           

mariadb安装服务并配置

[root@mariadb ~]# yum -y install mariadb-server mariadb
.......

# 启动
[root@mariadb ~]# systemctl start mariadb

[root@mariadb ~]# ss -antl
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       128               [::]:22             [::]:*            
LISTEN 0       80                   *:3306              *:*  

[root@mariadb ~]# 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)]> set password = password('123456');
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> exit
Bye

[root@mariadb ~]# mysql -uroot -p123456
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)]> CREATE DATABASE  `salt`
    ->   DEFAULT CHARACTER SET utf8
    ->   DEFAULT COLLATE utf8_general_ci;
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| salt               |
+--------------------+
4 rows in set (0.000 sec)

MariaDB [(none)]> USE `salt`;
Database changed
MariaDB [salt]> DROP TABLE IF EXISTS `jids`;
Query OK, 0 rows affected, 1 warning (0.001 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.003 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.003 sec)

MariaDB [salt]> DROP TABLE IF EXISTS `salt_events`;
Query OK, 0 rows affected, 1 warning (0.000 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.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)


master上接收node1的认证

[root@master ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
node1
Rejected Keys:

[root@master ~]# salt-key -ya node1
The following keys are going to be accepted:
Unaccepted Keys:
node1
Key for minion node1 accepted.

[root@master ~]# salt 'node1' test.ping
node1:
    True

node1上安装mariadb

[root@node1 ~]# yum -y install mariadb
.......

# 尝试使用mariadb主机创建的用户和密码登录
[root@node1 ~]# mysql -usalt -psalt -h192.168.220.17(mariadb主机的ip)
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
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)]>exit

# 修改配置文件
[root@node1 ~]# vim /etc/salt/minion
......
925 #return: mysql
926 #
927 #return:
928 #  - mysql
929 #  - hipchat
930 #  - slack
931 mysql.host: '192.168.220.17'  # IP是mariadb的主机
932 mysql.user: 'salt'
933 mysql.pass: 'salt'
934 mysql.db: 'salt'
935 mysql.port: 3306
.......

# 重启salt-minion
[root@node1 ~]# systemctl restart salt-minion

master上ping 测试连通性

[root@master ~]# salt 'node1' test.ping
node1:
    True

mariadb上查看是否有数据

[root@mariadb ~]# mysql -uroot -p123456
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.

MariaDB [salt]> select * from salt.salt_returns;
Empty set (0.000 sec)

master上测试存储到mariadb

[root@master ~]# salt 'node1' test.ping --return mysql
node1:
    True

mariadb查看


MariaDB [salt]> select * from salt.salt_returns\G
*************************** 1. row ***************************
       fun: test.ping
       jid: 20211105111823191240
    return: true
        id: node1
   success: 1
  full_ret: {"success": true, "return": true, "retcode": 0, "jid": "20211105111823191240", "fun": "test.ping", "fun_args": [], "id": "node1"}
alter_time: 2021-11-05 07:18:23
1 row in set (0.000 sec)

2. job cache

2.1 job cache流程

return时是由Minion直接与存储服务器进行交互,因此需要在每台Minion上安装指定的存储方式的模块,比如python-mysql,那么我们能否直接在Master上就把返回的结果给存储到存储服务器呢?

答案是肯定的,这种方式被称作 job cache 。意思是当Minion将结果返回给Master后,由Master将结果给缓存在本地,然后将缓存的结果给存储到指定的存储服务器,比如存储到mysql中。

在这里插入图片描述

master默认的存储位置

[root@master ~]# cd /var/cache/salt/master/jobs/
[root@master jobs]# ls
25  b1  cd  f8

之前改的node1上的配置文件还原

[root@node1 ~]# vim /etc/salt/minion
......
931 mysql.host: '192.168.220.17'  # 删除添加的五行
932 mysql.user: 'salt'
933 mysql.pass: 'salt'
934 mysql.db: 'salt'
935 mysql.port: 3306
......

# 重启salt-minion服务
[root@node1 ~]# systemctl restart salt-minion

master操作

# 安装

开启master端的master_job_cache

# 安装 python3-PyMySQL
[root@master jobs]# cd
[root@master ~]# yum -y install python3-PyMySQL
......

# 修改master的配置文件
[root@master ~]# vim /etc/salt/master
......
 137 #job_cache: True
 138 master_job_cache: mysql
 139 mysql.host: '192.168.220.17'   # mariadb主机的IP
 140 mysql.user: 'salt'
 141 mysql.pass: 'salt'
 142 mysql.db: 'salt'
 143 mysql.port: 3306
 ......
 
 # 重启salt-master
 [root@master ~]# systemctl restart salt-master

测试连通性

[root@master ~]# salt 'node1' test.ping
node1:
    True

master安装mariadb服务

[root@master ~]# yum -y install mariadb
......

# 尝试登录使用salt用户密码
[root@master ~]# mysql -usalt -psalt -h192.168.220.17
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 16
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)]> exit
Bye

删除mariadb上之前的数据

[root@mariadb ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 25
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)]> delete from salt.salt_returns;
  
Query OK, 2 rows affected (0.001 sec)

MariaDB [(none)]> select * from salt.salt_returns;
Empty set (0.000 sec)

MariaDB [(none)]> exit

master上再次测试能否存储至数据库

[root@master ~]# salt 'node1' cmd.run 'df -h'
node1:
    Filesystem           Size  Used Avail Use% Mounted on
    devtmpfs             876M     0  876M   0% /dev
    tmpfs                895M   60K  895M   1% /dev/shm
    tmpfs                895M   17M  879M   2% /run
    tmpfs                895M     0  895M   0% /sys/fs/cgroup
    /dev/mapper/cs-root   17G  2.0G   16G  12% /
    /dev/sda1           1014M  195M  820M  20% /boot
    tmpfs                179M     0  179M   0% /run/user/0

mariadb数据库中查询

MariaDB [(none)]> select * from salt.salt_returns\G
*************************** 1. row ***************************
       fun: cmd.run
       jid: 20211105115047743305
    return: "Filesystem           Size  Used Avail Use% Mounted on\ndevtmpfs             876M     0  876M   0% /dev\ntmpfs                895M   60K  895M   1% /dev/shm\ntmpfs                895M   17M  879M   2% /run\ntmpfs                895M     0  895M   0% /sys/fs/cgroup\n/dev/mapper/cs-root   17G  2.0G   16G  12% /\n/dev/sda1           1014M  195M  820M  20% /boot\ntmpfs                179M     0  179M   0% /run/user/0"
        id: node1
   success: 1
  full_ret: {"cmd": "_return", "id": "node1", "success": true, "return": "Filesystem           Size  Used Avail Use% Mounted on\ndevtmpfs             876M     0  876M   0% /dev\ntmpfs                895M   60K  895M   1% /dev/shm\ntmpfs                895M   17M  879M   2% /run\ntmpfs                895M     0  895M   0% /sys/fs/cgroup\n/dev/mapper/cs-root   17G  2.0G   16G  12% /\n/dev/sda1           1014M  195M  820M  20% /boot\ntmpfs                179M     0  179M   0% /run/user/0", "retcode": 0, "jid": "20211105115047743305", "fun": "cmd.run", "fun_args": ["df -h"], "_stamp": "2021-11-05T11:50:47.859197"}
alter_time: 2021-11-05 07:50:47
1 row in set (0.000 sec)

3. job 管理

Salt 0.9.7 为管理作业的saltutil引入了一些新功能。这些功能是:

  1. running返回在proc目录中找到的所有运行作业的数据。
  2. find_job根据工作 ID 返回有关某项工作的具体数据。
  3. signal_job允许向给定夹具发送信号。
  4. term_job向控制指定作业的流程发送终止信号(SIGTERM,15)。
  5. kill_job向控制指定作业的流程发送杀伤信号(SIGKILL,9)。

获取任务的jid

[root@master ~]# salt 'node1' cmd.run 'date' -v
Executing job with jid 20211105115845865556   # 此处就是此命令的jid
-------------------------------------------

node1:
    Fri Nov  5 07:58:45 EDT 2021    

通过jid获取任务的返回结果

[root@master ~]# salt-run jobs.lookup_jid 20211105115845865556
node1:
    Fri Nov  5 07:58:45 EDT 2021

列出正在执行的任务,可以通过上面的 kill_job jid 杀死一个正在执行的任务

salt-run jobs.active

列出执行过的任务

salt-run jobs.list_jobs
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值