SaltStack进阶
masterless的应用
应用场景
- master 与 minion 网络不通或通信有延迟,即网络不稳定
- 想在 minion 端直接执行状态
传统的 SaltStack 是需要通过 master 来执行状态控制 minion 从而实现状态的管理,但是当网络不稳定的时候,当想在minion本地执行状态的时候,当在只有一台主机的时候,想执行状态该怎么办呢?这就需要用到 masterless 了。
masterless配置
修改配置文件minion
- 注释master行
- 取消注释file_client并设其值为local
- 设置file_roots
- 设置pillar_roots
[root@node1 ~]# vim /etc/salt/minion
# master: salt
#master: 192.168.182.141 //如果有master端就注释,注释之后不关闭salt-minion服务也一样可以使用salt-call命令来执行状态文件
file_client: local //取消注释,将remote修改为local
file_roots: //设置file_roots,这是状态文件存放位置
base:
- /srv/salt/base
pillar_roots: //设置pillar,pillar变量存放位置
base:
- /srv/pillar/base
关闭salt-minion服务
[root@node1 base]# systemctl stop salt-minion.service
salt-call命令
[root@node1 base]# tree packages/
packages/
└── main.sls
0 directories, 1 file
[root@node1 base]# cat packages/main.sls
wget:
pkg.installed
[root@node1 base]# salt-call --local state.sls packages.main
local:
----------
ID: wget
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 16:28:37.129878
Duration: 548.88 ms
Changes:
Summary for local
------------
Succeeded: 1
Failed: 0
------------
Total states run: 1
Total run time: 548.880 ms
salt-master高可用
salt-master高可用配置
环境说明
系统 | IP | 主机名 |
---|---|---|
redhat8 | 192.168.182.141 | master |
redhat8 | 192.168.182.142 | master02 |
redhat8 | 192.168.182.143 | node1 |
我们需要用salt来管理公司的所有机器,那么salt的master就不能宕机,否则就会整个瘫痪,所以我们必须要对salt进行高可用。
三台主机做同样的操作
curl -fsSL https://repo.saltproject.io/py3/redhat/8/x86_64/latest.repo | sudo tee /etc/yum.repos.d/salt.repo
// 在两台master上做下面的操作
yum -y install salt-master
[root@node1 ~]# yum -y install salt-minion
修改node1的配置文件
[root@node1 ~]# vim /etc/salt/minion
17 master: 192.168.182.141
[root@node1 ~]# systemctl start salt-minion.service //开启salt-minion服务,然后到/etc/salt/pki/minion查看是否生成两个公钥文件
[root@master ~]# salt-key -ya node1 //接受key值
The following keys are going to be accepted:
Unaccepted Keys:
node1
Key for minion node1 accepted.
[root@master ~]# salt-key -L
Accepted Keys:
node1
测试连通性
[root@master ~]# salt node1 test.ping
node1:
True
在另一台master上的操作
//将master上的公钥文件拷贝到master02上
[root@master master]# scp /etc/salt/pki/master/master.p* 192.168.182.142:/etc/salt/pki/master/
[root@node1 ~]# vim /etc/salt/minion
master: 192.168.182.142
[root@node1 ~]# systemctl restart salt-minion.service
[root@master02 minion]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
node1
Rejected Keys:
[root@master02 minion]# salt-key -yA
测试连通性
[root@master02 srv]# salt node1 test.ping
node1:
True
salt-master高可用之数据同步
涉及到高可用时,数据的同步是个永恒的话题,我们必须保证高可用的2个master间使用的数据是一致的,包括:
- /etc/salt/master配置文件
- /etc/salt/pki目录下的所有key
- /srv/下的salt和pillar目录下的所有文件
保障这些数据同步的方案有:
- nfs挂载
- rsync同步
- 使用gitlab进行版本控制
修改配置文件node1上的配置
[root@node1 ~]# vim /etc/salt/minion
master:
- 192.168.182.141
- 192.168.182.142
master_type: failover //这个表示高可用,故障转移
master_alive_interval: 10 //这个表示当主节点的master宕机10秒后去接管备的master
[root@node1 ~]# systemctl restart salt-minion.service //重启服务
测试高可用
[root@master ~]# salt node1 test.ping
node1:
True
[root@master02 ~]# salt node1 test.ping
node1:
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 20211129122202357218
// 模拟主节点故障
[root@master ~]# systemctl stop salt-master.service
[root@master02 ~]# salt node1 test.ping
node1:
True
执行状态模块
[root@master ~]# scp /etc/salt/master 192.168.182.142:/etc/salt/master
[root@master02 ~]# mkdir -p /srv/{pillar,salt}/base
[root@master ~]# scp -r /srv/salt/base/* 192.168.182.142:/srv/salt/base/
[root@master ~]# scp /srv/pillar/base/* 192.168.182.142:/srv/pillar/base
[root@master02 init]# pwd
/srv/salt/base/init
[root@master02 yum]# tree //执行此状态文件
.
├── files
│ ├── centos-7.repo
│ ├── centos-8.repo
│ ├── epel-7.repo
│ ├── epel-8.repo
│ ├── salt-7.repo
│ └── salt-8.repo
└── main.sls
[root@master02 yum]# salt node1 state.sls init.yum.main saltenv=base
node1:
----------
ID: /etc/yum.repos.d/centos-8.repo
Function: file.managed
Result: True
Comment: File /etc/yum.repos.d/centos-8.repo updated
Started: 22:00:29.543836
Duration: 173.194 ms
Changes:
----------
diff:
New file
mode:
0644
----------
ID: /etc/yum.repos.d/epel-8.repo
Function: file.managed
Result: True
Comment: File /etc/yum.repos.d/epel-8.repo updated
Started: 22:00:29.717251
Duration: 32.064 ms
Changes:
----------
diff:
New file
mode:
0644
----------
ID: /etc/yum.repos.d/salt-8.repo
Function: file.managed
Result: True
Comment: File /etc/yum.repos.d/salt-8.repo updated
Started: 22:00:29.749628
Duration: 44.853 ms
Changes:
----------
diff:
New file
mode:
0644
Summary for node1
------------
Succeeded: 3 (changed=3)
Failed: 0
------------
Total states run: 3
Total run time: 250.111 ms
salt-syndic分布式架构
salt-syndic架构图
salt-syndic的优劣势
优势:
- 可以通过syndic实现更复杂的salt架构
- 减轻master的负担
劣势:
- syndic的/srv目录下的salt和pillar目录内容要与最顶层的master下的一致,所以要进行数据同步,同步方案同salt-master高可用
- 最顶层的master不知道自己有几个syndic,它只知道自己有多少个minion,并不知道这些minion是由哪些syndic来管理的
salt-syndic部署
环境说明:
系统版本为redhat8
主机IP | 角色 | 安装的应用 |
---|---|---|
192.168.182.141 | master | salt-master |
192.168.182.142 | Syndic | salt-master salt-syndic |
192.168.182.143 | Minion | salt-minion |
192.168.182.144 | Minion | salt-minion |
安装salt-master与salt-syndic
在Syndic主机上安装salt-master和salt-syndic
先安装salt源
[root@syndic ~]# curl -fsSL https://repo.saltproject.io/py3/redhat/8/x86_64/latest.repo | sudo tee /etc/yum.repos.d/salt.repo
[root@syndic ~]# yum -y install salt-master salt-syndic
在minion端安装salt-minion
[root@Minion01 ~]# curl -fsSL https://repo.saltproject.io/py3/redhat/8/x86_64/latest.repo | sudo tee /etc/yum.repos.d/salt.repo
[root@Minion01 ~]# yum -y install salt-minion
[root@Minion02 ~]# yum -y install salt-minion
配置master
修改master的master配置文件
- 取消注释order_master
- 将order_master的值设为True
[root@master salt]# vim /etc/salt/master
order_masters: True
[root@master salt]# systemctl restart salt-master.service
配置syndic
修改syndic所在主机的master配置文件
- 取消注释syndic_master
- 将syndic_master的值设为master的IP
[root@syndic ~]# vim /etc/salt/master
syndic_master: 192.168.182.141
//将salt-master和salt-syndic设置为开机自启
[root@syndic ~]# systemctl enable --now salt-master.service
Created symlink /etc/systemd/system/multi-user.target.wants/salt-master.service → /usr/lib/systemd/system/salt-master.service.
[root@syndic ~]# systemctl enable --now salt-syndic.service
Created symlink /etc/systemd/system/multi-user.target.wants/salt-syndic.service → /usr/lib/systemd/system/salt-syndic.service.
配置minion
配置minion,将master指向syndic所在主机
[root@Minion01 ~]# vim /etc/salt/minion
master: 192.168.182.142
[root@Minion02 ~]# vim /etc/salt/minion
master: 192.168.182.142
重启服务并设置开机自启
[root@Minion01 ~]# systemctl restart salt-minion.service
[root@Minion01 ~]# systemctl enable --now salt-minion.service
[root@Minion02 ~]# systemctl restart salt-minion.service
[root@Minion02 ~]# systemctl enable --now salt-minion.service
在所有minion上做同样的操作,注意,要设置minion配置文件中的id参数,指向minion自身的ip地址或主机名,必须能够唯一标识minion本机。
在syndic上接受minion主机的key
[root@syndic ~]# salt-key -yA
在master上接受syndic主机的key
[root@master salt]# salt-key -ya syndic
在master上执行模块或状态检验有几个minion应答
[root@master salt]# salt '*' test.ping
node3:
True
Minion01:
True
[root@syndic ~]# mkdir -p /srv/salt/base
[root@master ~]# scp -r /srv/salt/base/init/* 192.168.182.1442:/srv/salt/base/
[root@master ~]# salt '*' state.sls init.history.main
node3:
----------
ID: /etc/profile
Function: file.append
Result: True
Comment: Appended 1 lines
Started: 23:45:18.572951
Duration: 73.275 ms
Changes:
----------
diff:
---
+++
@@ -83,3 +83,4 @@
. /etc/bashrc
fi
fi
+export HISTTIMEFORMAT="%F %T `whoami`"
Summary for node3
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 73.275 ms
Minion01:
----------
ID: /etc/profile
Function: file.append
Result: True
Comment: Appended 1 lines
Started: 23:45:20.060263
Duration: 51.445 ms
Changes:
----------
diff:
---
+++
@@ -83,3 +83,4 @@
. /etc/bashrc
fi
fi
+export HISTTIMEFORMAT="%F %T `whoami`"
Summary for Minion01
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 51.445 ms