1. 程式人生 > 實用技巧 >自動化運維工具SaltStack之return與job管理(5)

自動化運維工具SaltStack之return與job管理(5)

目錄

1. SaltStack元件之return


return元件可以理解為SaltStack系統對執行Minion返回後的資料進行儲存或者返回給其他程式,它支援多種儲存方式,比如用MySQL、MongoDB、Redis、Memcache等,通過return我們可以對SaltStack的每次操作進行記錄,對以後日誌審計提供了資料來源。目前官方已經支援30種return資料儲存與介面,我們可以很方便的配置與使用它。當然也支援自己定義的return,自定義的return需由python來編寫。在選擇和配置好要使用的return後,只需在salt命令後面指定return即可。

[root@node01 ~]# salt 'node02*' sys.list_returners
node02.localdomain:
    - carbon
    - couchdb
    - elasticsearch
    - 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儲存方式

在所有minion上安裝Mysql-python模組

[root@node01 ~]# yum -y install MySQL-python
........

[root@node01 ~]# salt 'node02*' cmd.run 'rpm -qa|grep MySQL-python'
node02.localdomain:
    MySQL-python-1.2.5-1.el7.x86_64

部署一臺mysql伺服器用作儲存伺服器,此處就直接在192.168.159.136這臺主機上部署

//安裝mysql
[root@longnian ~]# yum -y install mariadb*
........

[root@longnian ~]# systemctl start mariadb
[root@longnian ~]# ss -anlt
State      Recv-Q Send-Q Local Address:Port                Peer Address:Port              
LISTEN     0      50                 *:3306                           *:*                  
LISTEN     0      128                *:22                             *:*                  
LISTEN     0      100        127.0.0.1:25                             *:*                  
LISTEN     0      128               :::22                            :::*                  
LISTEN     0      100              ::1:25                            :::* 

//建立資料庫和表結構
[root@longnian ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.65-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.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> USE `salt`;
Database changed
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.04 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.00 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.00 sec)

MariaDB [salt]> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids           |
| salt_events    |
| salt_returns   |
+----------------+
3 rows in set (0.00 sec)

//授權訪問
MariaDB [salt]> grant all on salt.* to salt@'%' identified by 'salt';
Query OK, 0 rows affected (0.00 sec)

MariaDB [salt]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

配置minion

[root@node02 ~]# vim /etc/salt/minion
........
mysql.host: '192.168.159.136'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306

[root@node02 ~]# systemctl restart salt-minion

在Master上測試儲存到mysql中

[root@node01 ~]# salt 'node02*' test.ping --return mysql
node02.localdomain:
    True

在資料庫中查詢

MariaDB [salt]> select * from salt_returns\G
*************************** 1. row ***************************
       fun: test.ping
       jid:  20200822168483456871
    return: true
        id: 192.168.159.136
   success: 1
  full_ret: {"fun_args": [], "jid": " 20200822168483456871", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2020-08-22T19:56:24.457140", "fun": "test.ping", "id": "192.168.159.136"}
alter_time: 2020-08-22T19:56:24

2. job cache


2.1 job cache流程

return時是由Minion直接與儲存伺服器進行互動,因此需要在每臺Minion上安裝指定的儲存方式的模組,比如python-mysql,那麼我們能否直接在Master上就把返回的結果給儲存到儲存伺服器呢?

答案是肯定的,這種方式被稱作 job cache 。意思是當Minion將結果返回給Master後,由Master將結果給快取在本地,然後將快取的結果給儲存到指定的儲存伺服器,比如儲存到mysql中。

開啟master端的master_job_cache

[root@node01~]# vim /etc/salt/master
........
master_job_cache: mysql
mysql.host: '192.168.159.136'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306

[root@master ~]# systemctl restart salt-master

在資料庫伺服器中清空表內容

[root@longnian ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.65-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, 0 rows affected (0.00 sec)

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

在master上再次測試能否儲存至資料庫

[root@node01 ~]# salt 'node02*' cmd.run 'df -h'
node02.localdomain:
    Filesystem               Size  Used Avail Use% Mounted on
    devtmpfs                 475M     0  475M   0% /dev
    tmpfs                    487M   40K  487M   1% /dev/shm
    tmpfs                    487M  7.7M  479M   2% /run
    tmpfs                    487M     0  487M   0% /sys/fs/cgroup
    /dev/mapper/centos-root   17G  5.9G   12G  35% /
    /dev/sda1               1014M  137M  878M  14% /boot
    tmpfs                     98M     0   98M   0% /run/user/0

在資料庫中查詢

MariaDB [(none)]> select * from salt.salt_returns\G
*************************** 1. row ***************************
       fun: cmd.run
       jid: 20200822168483687156
    return: "Filesystem               Size  Used Avail Use% Mounted on\n/dev/mapper/centos-root   17G  5.9G   12G  35% /\ndevtmpfs                475M     0  475M   0% /dev\ntmpfs                   487M   40K  487M   1% /dev/shm\ntmpfs                   487M  7.7M  479M   2% /run\ntmpfs                    487M    0  487M   0% /sys/fs/cgroup\n/dev/sda1               1014M  137M  878M  14% /boot\ntmpfs                   98M     0  98M   0% /run/user/0"
        id: 192.168.159.136
   success: 1
  full_ret: {"fun_args": ["df -h"], "jid": "20200822168483687156", "return": "Filesystem               Size  Used Avail Use% Mounted on\n/dev/mapper/centos-root   17G  5.9G   12G  35% /\ndevtmpfs                 475M     0  475M   0% /dev\ntmpfs                    475M   7.7M  479G   2% /dev/shm\ntmpfs                    487M  0  487M   0% /run\ntmpfs                   487M     0  487M   0% /sys/fs/cgroup\n/dev/sda1               1014M  137M  878M  14% /boot\ntmpfs                    781M     0  781M   0% /run/user/0", "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2020-08-22T19:58:11.457140", "fun": "cmd.run", "id": "192.168.159.136"}
alter_time: 2020-08-22T19:58:11
1 row in set (0.00 sec)

2.2 job管理

獲取任務的jid

[root@node01 ~]# salt 'node02*' cmd.run 'uptime' -v
Executing job with jid 20200821153252781247
-------------------------------------------

node02.localdomain:
     20:09:51 up  5:01,  1 user,  load average: 0.00, 0.01, 0.05

通過jid獲取此任務的返回結果

[root@node01 ~]# salt-run jobs.lookup_jid 20200821153252781247
node02.localdomain:
     20:09:51 up  5:01,  1 user,  load average: 0.00, 0.01, 0.05