saltstack进阶

masterless

应用场景

  • master 与 minion 网络不通或通信有延迟,即网络不稳定
  • 想在 minion 端直接执行状态
  • 传统的 SaltStack 是需要通过 master 来执行状态控制 minion 从而实现状态的管理,但是当网络不稳定的时候,当想在minion本地执行状态的时候,当在只有一台主机的时候,想执行状态该怎么办呢?这就需要用到 masterless 了。
  • 有了masterless,即使你只有一台主机,也能玩saltstack,而不需要你有N台主机架构。

masterless配置

修改配置文件minion

  • 注释master行
  • 取消注释file_client并设其值为local
  • 设置file_roots
  • 设置pillar_roots
[root@master ~]# vim /etc/salt/minion
......
#master: salt				#注释此行
file_client: local			#取消此行注释并将值设为local
file_roots:					#设置file_roots的路径和环境,可有多套环境
  base:
    - /srv/salt/base
  prod:
    - /srv/salt/prod

pillar_roots:				#设置pillar_roots的路径和环境,可有多套环境
  base:
    - /srv/pillar/base
  prod:
    - /srv/pillar/prod

关闭salt-minion服务
使用 masterless 模式时不需要启动salt任何服务

[root@master ~]# systemctl status salt-minion
● salt-minion.service - The Salt Minion
   Loaded: loaded (/usr/lib/systemd/system/salt-minion.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:salt-minion(1)
           file:///usr/share/doc/salt/html/contents.html
           https://docs.saltproject.io/en/latest/contents.html

salt-master高可用

salt-master高可用配置

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

[root@minion ~]# vim /etc/salt/minion
....
master:
  - 192.168.47.115
  - 192.168.47.120
....

salt-master高可用之数据同步

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

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

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

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

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

环境说明

主机IP角色安装的应用
192.168.47.115mastersalt-master
192.168.47.120master-2salt-master
192.168.47.121minionsalt-minion
  • minion端

minion端安装salt-minion

[root@minion ~]# ls /etc/yum.repos.d/
centos-8.repo  epel-8.repo  redhat.repo  salt-8.repo

[root@minion ~]# yum -y install salt-minion

修改minion端的配置文件,将master设为主控端的IP

[root@minion ~]# sed -i '/^#master:/a master: 192.168.47.115' /etc/salt/minion		#master的ip

启动

[root@minion ~]# systemctl enable --now salt-minion
Created symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service → /usr/lib/systemd/system/salt-minion.service.

[root@minion ~]# tree /etc/salt/pki
/etc/salt/pki
├── master
└── minion
    ├── minion.pem
    └── minion.pub

在master端接收minion端key(注意防火墙)

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

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

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

测试master端指定minion主机是否存活

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

master-2端安装salt-master

[root@master-2 ~]# ls /etc/yum.repos.d/
centos-8.repo  epel-8.repo  redhat.repo  salt-8.repo

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

将master上的公钥与私钥拷贝到master-2上去

[root@master ~]# tree /etc/salt/pki/master/
/etc/salt/pki/master/
├── master.pem
├── master.pub
├── minions
│   └── minion
├── minions_autosign
├── minions_denied
├── minions_pre
├── minions_rejected
└── ssh
    ├── salt-ssh.rsa
    └── salt-ssh.rsa.pub

[root@master ~]# scp /etc/salt/pki/master/master.p* 192.168.47.120:/etc/salt/pki/master/
root@192.168.47.120's password: 

//启动salt-master
[root@master-2 ~]# systemctl enable --now salt-master
Created symlink /etc/systemd/system/multi-user.target.wants/salt-master.service → /usr/lib/systemd/system/salt-master.service.

[root@master-2 ~]# tree /etc/salt/pki/master/
/etc/salt/pki/master/
├── master.pem
├── master.pub
├── minions
├── minions_autosign
├── minions_denied
├── minions_pre
└── minions_rejected

5 directories, 2 files

测试master-2端指定minion主机是否存活

[root@minion ~]# vim /etc/salt/minion
......
# master: salt
master: 192.168.47.120		#master-2端的ip

# Set http proxy information for the minion when doing requests
......

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

在master-2端接收minion主机的key(注意防火墙)

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

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

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

测试master-2端指定minion主机是否存活

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

同步配置和数据

[root@master ~]# scp /etc/salt/master 192.168.47.120:/etc/salt/master
[root@master ~]# scp -r /etc/salt/pki 192.168.47.120:/etc/salt/
[root@master ~]# scp -r /srv/salt 192.168.47.120:/srv/

当两台master都能ping通之后,最后再进行高可用设置并重启服务

[root@minion ~]# vim /etc/salt/minion
......
# master: salt
master: 
  - 192.168.47.115		#主master的ip
  - 192.168.47.120		#备master的ip
# Set http proxy information for the minion when doing requests
......
# beacons) without a master connection
master_type: failover		#取消注释并把str修改为failover(故障转移)

# Poll interval in seconds for checking if the master is still there.  Only
......
# of TCP connections, such as load balancers.)
master_alive_interval: 3		#取消注释并把30修改为3(当主节点挂了,3秒后接管)

# If the minion is in multi-master mode and the master_type configuration option
......

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

此时两台salt-master都为开启状态

[root@master ~]# ss -anlt
State        Recv-Q       Send-Q             Local Address:Port              Peer Address:Port       
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                        [::]:* 

[root@master-2 ~]# ss -anlt
State        Recv-Q       Send-Q             Local Address:Port              Peer Address:Port       
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可以ping通minion端,master-2就ping不通

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

[root@master-2 ~]# salt '*' test.ping		
minion:
    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     

当master与master-2都在运行状态时,minion只能连接主master上

[root@minion ~]# systemctl status salt-minion
● salt-minion.service - The Salt Minion
   Loaded: loaded (/usr/lib/systemd/system/salt-minion.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2021-11-30 02:23:06 CST; 10min ago
     Docs: man:salt-minion(1)
           file:///usr/share/doc/salt/html/contents.html
           https://docs.saltproject.io/en/latest/contents.html
 Main PID: 142364 (salt-minion)
    Tasks: 6 (limit: 12400)
   Memory: 91.3M
   CGroup: /system.slice/salt-minion.service
           ├─142373 /usr/bin/python3.6 /usr/bin/salt-minion
           ├─142374 /usr/bin/python3.6 /usr/bin/salt-minion
           └─142376 /usr/bin/python3.6 /usr/bin/salt-minion

1130 02:23:06 minion systemd[1]: Stopped The Salt Minion.
1130 02:23:06 minion systemd[1]: Starting The Salt Minion...
1130 02:23:06 minion systemd[1]: Started The Salt Minion.
1130 02:23:18 minion salt-minion[142364]: [CRITICAL] 'master_type' set to 'failover' but 'retry_dns' >

停止master的服务,master-2就能ping通

[root@master ~]# systemctl stop salt-master
[root@master ~]# ss -anlt
State        Recv-Q       Send-Q             Local Address:Port              Peer Address:Port       
LISTEN       0            128                      0.0.0.0:22                     0.0.0.0:*          
LISTEN       0            128                         [::]:22                        [::]:*  

在master-2主机上进行test.ping测试

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

此时minion端的状态文件是

[root@minion ~]# systemctl status salt-minion
● salt-minion.service - The Salt Minion
   Loaded: loaded (/usr/lib/systemd/system/salt-minion.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2021-11-30 02:23:06 CST; 15min ago
     Docs: man:salt-minion(1)
           file:///usr/share/doc/salt/html/contents.html
           https://docs.saltproject.io/en/latest/contents.html
 Main PID: 142364 (salt-minion)
    Tasks: 6 (limit: 12400)
   Memory: 92.4M
   CGroup: /system.slice/salt-minion.service
           ├─142373 /usr/bin/python3.6 /usr/bin/salt-minion
           ├─142374 /usr/bin/python3.6 /usr/bin/salt-minion
           └─142376 /usr/bin/python3.6 /usr/bin/salt-minion

1130 02:23:06 minion systemd[1]: Stopped The Salt Minion.
1130 02:23:06 minion systemd[1]: Starting The Salt Minion...
1130 02:23:06 minion systemd[1]: Started The Salt Minion.
1130 02:23:18 minion salt-minion[142364]: [CRITICAL] 'master_type' set to 'failover' but 'retry_dns' >
1130 02:23:36 minion salt-minion[142364]: [WARNING ] Master ip address changed from 192.168.47.115 t>
1130 02:23:36 minion salt-minion[142364]: [WARNING ] Master ip address changed from 192.168.47.115 t>

salt-syndic

salt-syndic分布式架构图

在这里插入图片描述

salt-syndic的特点

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

salt-syndic部署

环境说明

主机ip角色主机名安装应用
192.168.47.115Mastermastersalt-master
192.168.47.120Syndicsyndicsalt-master salt-syndic
192.168.47.121Minionnode1salt-minion
192.168.47.128Minion2node2salt-minion

安装软件

//在master上安装salt-master
[root@master ~]# yum -y install salt-master
[root@master ~]# rpm -qa | grep salt
salt-master-3004-1.el8.noarch
salt-3004-1.el8.noarch

//在syndic上安装salt-master和salt-syndic
[root@syndic ~]# yum -y install salt-master salt-syndic
[root@sybdic ~]# rpm -qa | grep salt
salt-3004-1.el8.noarch
salt-syndic-3004-1.el8.noarch
salt-master-3004-1.el8.noarch

在node1上安装salt-minion
[root@node1 ~]# yum -y install salt-minion
[root@node1 yum.repos.d]# rpm -qa | grep salt
salt-3004-1.el8.noarch
salt-minion-3004-1.el8.noarch

在node2上安装salt-minion
[root@node2 ~]# yum -y install salt-minion
[root@node2 yum.repos.d]# rpm -qa | grep salt
salt-minion-3004-1.el8.noarch
salt-3004-1.el8.noarch

配置master端
在master的配置文件将order_master的值设为True

[root@master ~]# vim /etc/salt/master
.....
# masters' syndic interfaces.
order_masters: True

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

配置syndic端
在syndic的配置文件上将syndic_master的值设为master的IP

[root@syndic ~]# vim /etc/salt/master
.....此处省略N行
syndic_master: 192.168.47.115

[root@sybdic ~]# systemctl enable --now salt-master
Created symlink /etc/systemd/system/multi-user.target.wants/salt-master.service → /usr/lib/systemd/system/salt-master.service.
[root@sybdic ~]# systemctl enable --now salt-syndic
Created symlink /etc/systemd/system/multi-user.target.wants/salt-syndic.service → /usr/lib/systemd/system/salt-syndic.service.

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


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

[root@node1 ~]# vim /etc/salt/minion
......
# master: salt
master: 192.168.47.120
# Set http proxy information for the minion when doing requests
......

[root@node1 ~]# systemctl enable --now salt-minion
Created symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service → /usr/lib/systemd/system/salt-minion.service.


[root@node2 ~]# vim /etc/salt/minion
......
# master: salt
master: 192.168.47.120
# Set http proxy information for the minion when doing requests
......

[root@node2 ~]# systemctl enable --now salt-minion
Created symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service → /usr/lib/systemd/system/salt-minion.service.

[root@node1 ~]# tree /etc/salt/pki
/etc/salt/pki
├── master
└── minion
    ├── minion.pem
    └── minion.pub

[root@node2 ~]# tree /etc/salt/pki
/etc/salt/pki
├── master
└── minion
    ├── minion.pem
    └── minion.pub

在syndic上接受minion主机的key

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

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

master上接受syndic主机的key

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

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

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

在master上执行模块或状态检验有几个minion应答

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

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:创作都市 设计师:CSDN官方博客 返回首页

打赏作者

小、阿杰

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值