欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Saltstack_使用指南08_远程执行-返回程序

程序员文章站 2022-05-20 17:06:41
1. 主机规划 Returner Modules文档 注意事项 修改了master或者minion的配置文件,那么必须重启对应的服务。 2. 数据库配置 2.1. 数据库安装 根据规划在 salt100 部署mariadb 数据库 2.2. 字符集设置 配置文件也要修改 2.3. 创建数据库、用户、 ......

 

1. 主机规划

Saltstack_使用指南08_远程执行-返回程序

 

salt 版本
1 [root@salt100 ~]# salt --version
2 salt 2018.3.3 (oxygen)
3 [root@salt100 ~]# salt-minion --version
4 salt-minion 2018.3.3 (oxygen)

 

returners文档
https://docs.saltstack.com/en/latest/ref/returners/index.html

 

returner modules文档

https://docs.saltstack.com/en/latest/ref/returners/all/index.html#all-salt-returners

 

注意事项

修改了master或者minion的配置文件,那么必须重启对应的服务。

 

2. 数据库配置

2.1. 数据库安装

根据规划在 salt100 部署mariadb 数据库

1 # 数据库安装
2 yum install -y mariadb mariadb-server    
3 # 启动数据库
4 systemctl start mariadb.service   # 如果有必要可以设置为开机自启动
5 # 在「其他」机器安装,用于测试远程是否可以连接
6 yum install -y mariadb  

 

2.2. 字符集设置

配置文件也要修改

 1 [root@salt100 ~]# mysql
 2 welcome to the mariadb monitor.  commands end with ; or \g.
 3 your mariadb connection id is 2
 4 server version: 5.5.60-mariadb mariadb server
 5 
 6 copyright (c) 2000, 2018, oracle, mariadb corporation ab and others.
 7 
 8 type 'help;' or '\h' for help. type '\c' to clear the current input statement.
 9 
10 mariadb [(none)]> show variables like '%char%';  
11 +--------------------------+----------------------------+
12 | variable_name            | value                      |
13 +--------------------------+----------------------------+
14 | character_set_client     | utf8                       |
15 | character_set_connection | utf8                       |
16 | character_set_database   | latin1                     |
17 | character_set_filesystem | binary                     |
18 | character_set_results    | utf8                       |
19 | character_set_server     | latin1                     |
20 | character_set_system     | utf8                       |
21 | character_sets_dir       | /usr/share/mysql/charsets/ |
22 +--------------------------+----------------------------+
23 8 rows in set (0.00 sec)
24 
25 mariadb [(none)]> set character_set_database=utf8;  
26 query ok, 0 rows affected (0.00 sec)
27 
28 mariadb [(none)]> set character_set_server=utf8;  
29 query ok, 0 rows affected (0.00 sec)
30 
31 mariadb [(none)]> show variables like '%char%';  
32 +--------------------------+----------------------------+
33 | variable_name            | value                      |
34 +--------------------------+----------------------------+
35 | character_set_client     | utf8                       |
36 | character_set_connection | utf8                       |
37 | character_set_database   | utf8                       |
38 | character_set_filesystem | binary                     |
39 | character_set_results    | utf8                       |
40 | character_set_server     | utf8                       |
41 | character_set_system     | utf8                       |
42 | character_sets_dir       | /usr/share/mysql/charsets/ |
43 +--------------------------+----------------------------+
44 8 rows in set (0.01 sec)

 

2.3. 创建数据库、用户、授权

 1 # 创建数据库
 2 mariadb [(none)]> create database  `salt`
 3          default character set utf8
 4          default collate utf8_general_ci;
 5 query ok, 1 row affected (0.00 sec)
 6 
 7 mariadb [(none)]> show create database salt;
 8 +----------+---------------------------------------------------------------+
 9 | database | create database                                               |
10 +----------+---------------------------------------------------------------+
11 | salt     | create database `salt` /*!40100 default character set utf8 */ |
12 +----------+---------------------------------------------------------------+
13 1 row in set (0.00 sec)
14 
15 # 创建用户并授权
16 mariadb [(none)]> grant all on salt.* to salt@'%' identified by 'salt';  # 用于远程访问
17 query ok, 0 rows affected (0.00 sec)
18 
19 mariadb [(none)]> grant all on salt.* to salt@'salt100' identified by 'salt';   # 用于本地访问
20 query ok, 0 rows affected (0.00 sec)
21 
22 mariadb [(none)]> flush privileges; 
23 query ok, 0 rows affected (0.00 sec)
24 
25 mariadb [(none)]> select user,host,password from mysql.user where user = 'salt';
26 +------+---------+-------------------------------------------+
27 | user | host    | password                                  |
28 +------+---------+-------------------------------------------+
29 | salt | %       | *36f75abc6d500dfa6e905046fd8be5e115812dd0 |
30 | salt | salt100 | *36f75abc6d500dfa6e905046fd8be5e115812dd0 |
31 +------+---------+-------------------------------------------+
32 2 rows in set (0.00 sec)

 

本地或者远程登录测试

1 mysql -hsalt100 -usalt -psalt
2 # 或者
3 mysql -h172.16.1.100 -usalt -psalt

 

2.4. 创建表

相关文档

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

 

 1 use salt;
 2 
 3 --
 4 -- table structure for table `jids`
 5 --
 6 drop table if exists `jids`;
 7 create table `jids` (
 8   `jid` varchar(255) not null,
 9   `load` mediumtext not null,
10   unique key `jid` (`jid`)
11 ) engine=innodb default charset=utf8;
12 
13 --
14 -- table structure for table `salt_returns`
15 --
16 drop table if exists `salt_returns`;
17 create table `salt_returns` (
18   `fun` varchar(50) not null,
19   `jid` varchar(255) not null,
20   `return` mediumtext not null,
21   `id` varchar(255) not null,
22   `success` varchar(10) not null,
23   `full_ret` mediumtext not null,
24   `alter_time` timestamp default current_timestamp,
25   key `id` (`id`),
26   key `jid` (`jid`),
27   key `fun` (`fun`)
28 ) engine=innodb default charset=utf8;
29 
30 --
31 -- table structure for table `salt_events`
32 --
33 drop table if exists `salt_events`;
34 create table `salt_events` (
35 `id` bigint not null auto_increment,
36 `tag` varchar(255) not null,
37 `data` mediumtext not null,
38 `alter_time` timestamp default current_timestamp,
39 `master_id` varchar(255) not null,
40 primary key (`id`),
41 key `tag` (`tag`)
42 ) engine=innodb default charset=utf8;

 

3. 安装依赖包

1 salt '*' state.single pkg.installed name=mysql-python    
2 或者
3 salt '*' cmd.run 'yum install -y mysql-python'    

 

4. minion返回结果到mysql数据库

由 minion 端直接返回到 mysql 数据库,不需要经过 master 端。

 

相关文档

https://docs.saltstack.com/en/latest/topics/jobs/external_cache.html

 

4.1. minion端存储架构

Saltstack_使用指南08_远程执行-返回程序

 

4.2. 方式一:所有minion端都添加设置

1 [root@salt03 ~]# vim /etc/salt/minion 
2 …………
3 mysql.host: 'salt100'
4 mysql.user: 'salt'
5 mysql.pass: 'salt'
6 mysql.db: 'salt'
7 mysql.port: 3306
8 
9 [root@salt03 ~]# systemctl restart salt-minion.service  # 修改了配置,需要重启 minion

 

4.3. 方式二:在master端添加设置

 1 [root@salt100 ~]# vim /etc/salt/master 
 2 …………
 3 ext_job_cache: mysql
 4 mysql.host: 'salt100'
 5 mysql.user: 'salt'
 6 mysql.pass: 'salt'
 7 mysql.db: 'salt'
 8 mysql.port: 3306
 9 
10 [root@salt100 ~]# systemctl restart salt-master.service   # 修改了配置,需要重启 master

 

4.4. 测试执行

 1 # 说明: 带了 --return mysql ,适合上面的方式一,在minion端配置
 2 # 如果没有带  --return mysql ,适合上面的方式二,在master端配置
 3 [root@salt100 ~]# salt '*' test.ping --return mysql  
 4 salt03:
 5     true
 6 salt01:
 7     true
 8 salt02:
 9     true
10 salt100:
11     true
12 [root@salt100 ~]# salt 'salt0*' cmd.run 'df -h' --return mysql  
13 salt03:
14     filesystem      size  used avail use% mounted on
15     /dev/sda3        18g  2.0g   16g  12% /
16     devtmpfs        901m     0  901m   0% /dev
17     tmpfs           911m   12k  911m   1% /dev/shm
18     tmpfs           911m  9.6m  902m   2% /run
19     tmpfs           911m     0  911m   0% /sys/fs/cgroup
20     /dev/sda1       197m  113m   85m  58% /boot
21     tmpfs           183m     0  183m   0% /run/user/1001
22 salt01:
23     filesystem      size  used avail use% mounted on
24     /dev/sda3        18g  2.1g   16g  12% /
25     devtmpfs        901m     0  901m   0% /dev
26     tmpfs           911m   12k  911m   1% /dev/shm
27     tmpfs           911m  9.6m  902m   2% /run
28     tmpfs           911m     0  911m   0% /sys/fs/cgroup
29     /dev/sda1       197m  113m   85m  58% /boot
30     tmpfs           183m     0  183m   0% /run/user/1001
31 salt02:
32     filesystem      size  used avail use% mounted on
33     /dev/sda3        18g  2.0g   16g  12% /
34     devtmpfs        901m     0  901m   0% /dev
35     tmpfs           911m   12k  911m   1% /dev/shm
36     tmpfs           911m  9.6m  902m   2% /run
37     tmpfs           911m     0  911m   0% /sys/fs/cgroup
38     /dev/sda1       197m  113m   85m  58% /boot
39     tmpfs           183m     0  183m   0% /run/user/1001

 

4.5. 数据库表信息

Saltstack_使用指南08_远程执行-返回程序

 

5. master返回结果到mysql数据库

数据从minion端返回到master之后,再由master写入mysql

 

相关文档

https://docs.saltstack.com/en/latest/topics/jobs/external_cache.html

 

5.1. master端存储架构

Saltstack_使用指南08_远程执行-返回程序

 

5.2. master端添加如下配置

 1 [root@salt100 ~]# vim /etc/salt/master 
 2 …………
 3 master_job_cache: mysql
 4 mysql.host: 'salt100'
 5 mysql.user: 'salt'
 6 mysql.pass: 'salt'
 7 mysql.db: 'salt'
 8 mysql.port: 3306
 9 
10 [root@salt100 ~]# systemctl restart salt-master.service   # 修改了配置,需要重启 master

 

5.3. 测试执行

1 [root@salt100 ~]# salt '*' grains.items
2 [root@salt100 ~]# salt '*' cmd.run 'w' 

 

5.4. 数据库表信息

Saltstack_使用指南08_远程执行-返回程序

 

 

Saltstack_使用指南08_远程执行-返回程序