SaltStack进阶

34 篇文章 1 订阅

1. masterless

1.1 应用场景

  • master 与 minion 网络不通或通信有延迟,即网络不稳定
  • 想在 minion 端直接执行状态

传统的 SaltStack 是需要通过 master 来执行状态控制 minion 从而实现状态的管理,但是当网络不稳定的时候,当想在minion本地执行状态的时候,当在只有一台主机的时候,想执行状态该怎么办呢?这就需要用到 masterless 了。

有了masterless,即使你只有一台主机,也能玩saltstack,而不需要你有N台主机架构。

1.2 masterless配置

1.2.1 修改配置文件minion
  • 注释master行
  • 取消注释file_client并设其值为local
  • 设置file_roots
  • 设置pillar_roots
[root@minion ~]# vim /etc/salt/minion
....此处省略N行
# resolved, then the minion will fail to start.
# master: salt      //注释此行
....此处省略N行
file_client: local  //取消此行注释并将值设为local
....此处省略N行
file_roots:         //设置file_roots的路径和环境,可有多套环境
  base:
    - /srv/salt/base
....此处省略N行
pillar_roots:		// 设置pillar_root,可使用变量
  base:
    - /srv/pillar/base
    
// 创建目录 
[root@minion ~]# mkdir -p /srv/{salt,pillar}/base
[root@minion ~]# cd /srv/
[root@minion srv]# tree
.
|-- pillar
|   `-- base
`-- salt
    `-- base
1.2.2 关闭salt-minion服务

使用 masterless 模式时是不需要启动任何服务的,包括salt-master和salt-minion。

[root@minion ~]# systemctl stop salt-minion
[root@minion ~]# systemctl disable salt-minion
Removed symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service.
1.2.3 salt-call

masterless模式执行模块或状态时需要使用salt-call命令,而不再是salt或者salt-ssh。需要注意的是要使用salt-call的–local选项。

[root@minion ~]# salt-call --local cmd.run 'date'
local:
    Mon Nov 29 10:06:12 CST 2021
 
[root@minion ~]# salt-call --local cmd.run 'ls -l /root'
local:
    total 4
    -rw-------. 1 root root 1092 Nov  2 10:34 anaconda-ks.cfg
    
[root@minion ~]# salt-call --local cmd.run 'echo "hehe" > /root/amu'
local:
[root@minion ~]# salt-call --local cmd.run 'ls -l /root'
local:
    total 8
    -rw-r--r--  1 root root    5 Nov 29 10:10 amu
    -rw-------. 1 root root 1092 Nov  2 10:34 anaconda-ks.cfg
    
[root@node1 ~]# tree /srv/salt/
/srv/salt/
`-- base
    `-- init
        |-- basepkgs
        |   `-- main.sls
        |-- chrony
        |   |-- files
        |   |   `-- chrony.conf
        |   `-- main.sls
        |-- firewalld
        |   `-- main.sls
        |-- history
        |   `-- main.sls
        |-- kernel
        |   |-- files
        |   |   |-- limits.conf
        |   |   `-- sysctl.conf
        |   `-- main.sls
        |-- main.sls
        |-- postfix
        |   `-- main.sls
        |-- salt-minion
        |   |-- files
        |   |   `-- minion.j2
        |   `-- main.sls
        |-- selinux
        |   |-- files
        |   |   `-- config
        |   `-- main.sls
        |-- sshd
        |   |-- files
        |   |   `-- sshd_config
        |   `-- main.sls
        |-- sudo
        |   `-- main.sls
        |-- timeout
        |   `-- main.sls
        `-- yum
            |-- files
            |   |-- Centos-7.repo
            |   |-- Centos-8.repo
            |   |-- epel.repo
            |   |-- salt-7.repo
            |   `-- salt-8.repo
            `-- main.sls

20 directories, 24 files

// 使用salt-call执行状态文件
[root@minion ~]# salt-call --local state.sls init.history.main
local:
----------
          ID: /etc/profile
    Function: file.line
      Result: True
     Comment: Changes were made
     Started: 18:10:15.018518
    Duration: 17.753 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,5 +1,6 @@
                   # /etc/profile
                   
                  +export HISTTIMEFORMAT="%F %T `whoami` "
                   # System wide environment and startup programs, for login setup
                   # Functions and aliases go in /etc/bashrc
                   

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  17.753 ms

2. salt-master高可用

2.1 salt-master高可用配置

我们需要用salt来管理公司的所有机器,那么salt的master就不能宕机,否则就会整个瘫痪,所以我们必须要对salt进行高可用。salt的高可用配置非常简单,只需要改一下minion配置文件,将master用列表的形式列出即可。

[root@minion ~]# vim /etc/salt/minion
....此处省略N行
master:
  - 192.168.91.135
  - 192.168.91.137
....此处省略N行

本例列出的192.168.91.135和192.168.91.137上必须安装了salt-master且保证服务都是正常状态。

2.2 salt-master高可用之数据同步

涉及到高可用时,数据的同步是个永恒的话题,我们必须保证高可用的2个master间使用的数据是一致的,包括:

  • /etc/salt/master配置文件
  • /etc/salt/pki目录下的所有key
  • /srv/下的salt和pillar目录下的所有文件

保障这些数据同步的方案有:

  • nfs挂载
  • rsync同步
  • 使用gitlab进行版本控制

安全相关:
为保证数据的同步与防止丢失,可将状态文件通过gitlab进行版本控制管理。

准备工作:

主机名ip职责安装服务
master192.168.91.135主mastersalt-master
master2192.168.91.137备mastersalt-master
minion192.168.91.138minionsalt-minion

例子:

// 同步master配置文件
[root@master ~]# scp /etc/salt/master 192.168.91.137:/etc/salt/
root@192.168.91.137's password: 
master                     100%   52KB  12.9MB/s   00:00

// 创建目录
[root@master2 ~]# mkdir -p /srv/{salt/{base,test,prod,dev},pillar/{base,prod}}
[root@master2 ~]# tree /srv
/srv
|-- pillar
|   |-- base
|   `-- prod
`-- salt
    |-- base
    |-- dev
    |-- prod
    `-- test

8 directories, 0 files

// 同步所有的状态文件
[root@master ~]# scp -r /srv/* 192.168.91.137:/srv/


// 同步pki目录
[root@master ~]# scp -r /etc/salt/pki/* 192.168.91.137:/etc/salt/pki/

// 重启
[root@master ~]# systemctl restart salt-master
[root@master2 ~]# systemctl restart salt-master

// 修改配置文件,重启salt-minion服务
[root@minion ~]# vim /etc/salt/minion
....此处省略N行
master: 
  - 192.168.91.135 // 指定主master IP
  - 192.168.91.137 // 指定备master IP
[root@minion minion]# systemctl restart salt-minion

实际操作:

等待证书生成,授权证书之后开始ping通验证

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

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

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

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

当主master与minion端ping通后,再将master主机上的 /etc/salt/pki/master 目录中的公钥与私钥master.pem、master.pub 传输到备机 master 主机的 /etc/salt/pki/master 目录中。

[root@master ~]# cd /etc/salt/pki/master/
[root@master master]# ls
master.pem  master.pub  minions  minions_autosign  minions_denied  minions_pre  minions_rejected

[root@master master]# scp /etc/salt/pki/master/master.pem 192.168.91.137:/etc/salt/pki/master
root@192.168.91.137's password: 

[root@master master]# scp /etc/salt/pki/master/master.pub  192.168.91.137:/etc/salt/pki/master
root@192.168.91.137's password: 

传输完成,再去minion主机上修改配置文件

[root@minion minion]# vim /etc/salt/minion
#master: salt
master: 192.168.58.30    //指定备masterip

// 修改完成重启配置文件
[root@minion minion]# systemctl restart salt-minion

等待证书生成后,授权证书,去master2主机进行test.ping检测

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

[root@master2 ~]# salt-key -ya192.168.91.138
The following keys are going to be accepted:
Unaccepted Keys:
192.168.91.138
Key for minion 192.168.91.138 accepted.

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

[root@master2 ~]# salt '192.168.91.138' test.ping
192.168.91.138:
    True

主、备master都ping通之后,进行高可用配置

[root@minion minion]# vim /etc/salt/minion
#master: salt
master: 
  - 192.168.91.135  // 指定主master IP
  - 192.168.91.137	// 指定备master IP

开始故障转移配置

[root@minion minion]# vim /etc/salt/minion
master_type: failover    // 高可用(故障转移)
----------
# connection events.
#
master_alive_interval: 15       // 主机等待的时间间隔

// 配置完成之后重启salt-minion服务
[root@minion minion]# systemctl restart salt-minion

查看两台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                 *:80                *:*                                                                 
LISTEN 0      128              [::]:22             [::]:*  

// 备
[root@master2 ~]# 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             [::]:*  

开始实验:

  • 主master连接minion的时候,备master2能否连接minion
// 主
[root@master ~]# salt '192.168.91.138' test.ping
192.168.91.138:
    True
    
// 备
[root@masters master]# salt '192.168.91.138' test.ping
192.168.91.138:
    Minion did not return. [No response]
    The minions may not have all finished running and any remaining minions will return upon completion. To look up the return data for this job later, run the following command:
    
    salt-run jobs.lookup_jid 20211129105915662209
ERROR: Minions returned with non-zero exit code 	// 这里的备机master2无法正常连接minion,因为主master还在连接minion

// 查看salt-minion状态
[root@minion minion]# systemctl status salt-minion
● salt-minion.service - The Salt Minion
   Loaded: loaded (/usr/lib/systemd/system/salt-minion.servi>
   Active: active (running) since Mon 2021-11-29 19:58:53 CS>
     Docs: man:salt-minion(1)
           file:///usr/share/doc/salt/html/contents.html
           https://docs.saltproject.io/en/latest/contents.ht>
 Main PID: 409603 (salt-minion)
    Tasks: 15 (limit: 11201)
   Memory: 81.8M
   CGroup: /system.slice/salt-minion.service
           ├─409603 /usr/bin/python3.6 /usr/bin/salt-minion
           ├─409646 /usr/bin/python3.6 /usr/bin/salt-minion
           └─409648 /usr/bin/python3.6 /usr/bin/salt-minion

Nov 29 19:58:53 minion systemd[1]: Starting The Salt Minion.>
Nov 29 19:58:53 minion systemd[1]: Started The Salt Minion.
Nov 29 19:58:53 minion salt-minion[181440]: [CRITICAL] 'master_type' set to 'failover' but 'retry_dns' is 
  • 当主master连接minion断开,备master2进行ping通测试
// 主
[root@master master]# systemctl stop 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                 *:80                *:*    
                                                             
LISTEN 0      128              [::]:22             [::]:*    
                                                            
                                                            
// 备
[root@masters master]# salt '192.168.91.138' test.ping
192.168.91.138:
    True	// 当主master断开连接,备master2主机可以连接上minion
    
// 查看salt-minion状态
[root@minion minion]# systemctl status salt-minion
● salt-minion.service - The Salt Minion
   Loaded: loaded (/usr/lib/systemd/system/salt-minion.servi>
   Active: active (running) since Mon 2021-11-29 19:58:53 CS>
     Docs: man:salt-minion(1)
           file:///usr/share/doc/salt/html/contents.html
           https://docs.saltproject.io/en/latest/contents.ht>
 Main PID: 409603 (salt-minion)
    Tasks: 15 (limit: 11201)
   Memory: 81.8M
   CGroup: /system.slice/salt-minion.service
           ├─409603 /usr/bin/python3.6 /usr/bin/salt-minion
           ├─409646 /usr/bin/python3.6 /usr/bin/salt-minion
           └─409648 /usr/bin/python3.6 /usr/bin/salt-minion

Nov 29 19:58:53 minion systemd[1]: Starting The Salt Minion.>
Nov 29 19:58:53 minion systemd[1]: Started The Salt Minion.
Nov 29 19:58:53 minion salt-minion[181440]: [CRITICAL] 'master_type' set to 'failover' but 'retry_dns' is 
Nov 29 19:58:53 minion salt-minion[181440]: [WARNING ] Master ip address changed from 192.168.91.135 to 192.168.91.137>
Nov 29 19:58:53 minion salt-minion[181440]: [WARNING ] Master ip address changed from 192.168.91.135 to 192.168.91.137>
  • 最后为保证备服务器的业务正常,将主 master 的 /srv/目录 copy到 备master 服务上,实验就结束了
[root@master ~]# scp -r /srv/ 192.168.91.137:/srv/

3. salt-syndic分布式架构

3.1 salt-syndic架构图

img

3.2 salt-syndic的优劣势

优势:

  • 可以通过syndic实现更复杂的salt架构
  • 减轻master的负担

劣势:

  • syndic的/srv目录下的salt和pillar目录内容要与最顶层的master下的一致,所以要进行数据同步,同步方案同salt-master高可用
  • 最顶层的master不知道自己有几个syndic,它只知道自己有多少个minion,并不知道这些minion是由哪些syndic来管理的

3.3 salt-syndic部署

环境说明

主机名IP服务
master192.168.91.135salt-master
syndic192.168.91.137salt-master、salt-syndic
minion192.168.91.138salt-minion

安装salt-master、salt-syndic

[root@syndic ~]# yum -y install salt-master salt-syndic

修改master主机 /etc/salt/master 配置文件

[root@master ~]# vim /etc/salt/master
order_masters: True  // 取消该行注释,修改这里的值变为True

// 重启服务
[root@master ~]# systemctl restart salt-master

修改syndic所在主机的 /etc/saltmaster 配置文件

[root@syndic ~]# vim /etc/salt/master
syndic_master: 192.168.91.135	// 取消该行注释,修改这里的值变为master主机的 IP 地址

// 重启服务
[root@syndic ~]# systemctl restart salt-master
[root@syndic ~]# systemctl restart salt-syndic

配置minion,将master指向syndic所在主机

[root@minion ~]# vim /etc/salt/minion
master: 192.168.91.137

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

在所有minion上做同样的操作
注意,要设置minion配置文件中的id参数,指向minion自身的ip地址或主机名,必须能够唯一标识minion本机。

先在syndic主机上接受minion主机的key

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

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

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

在master上接受syndic主机的key

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

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

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

最后在master上验证与minion的连通性

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

执行状态文件进行测试

// 先同步master主机和syndic主机的目录,还有file_roots、pillar_roots文件
[root@master ~]# scp -r /srv/ 192.168.91.137:/srv/

[root@master ~]# salt 'minion' state.sls init.firewalld.main
minion:
----------
          ID: /etc/profile
    Function: file.line
      Result: True
     Comment: Changes were made
     Started: 20:50:16.018518
    Duration: 19.753 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,5 +1,6 @@
                   # /etc/profile
                   
                  +export HISTTIMEFORMAT="%F %T `whoami` "
                   # System wide environment and startup programs, for login setup
                   # Functions and aliases go in /etc/bashrc
                   

Summary for minion
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  22.753 ms
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值