传统的 SaltStack 是需要通过 master 来执行状态控制 minion 从而实现状态的管理,但是当网络不稳定的时候,当想在minion本地执行状态的时候,当在只有一台主机的时候,想执行状态该怎么办呢?这就需要用到 masterless 了。
有了masterless,即使你只有一台主机,也能玩saltstack,而不需要你有N台主机架构。
1.2.1 修改配置文件minion
[root@node2 ~]# vim /etc/salt/minion # resolved, then the minion will fail to start. # master: salt //注释此行 file_client: local //取消此行注释并将值设为local file_roots: //设置file_roots的路径和环境,可有多套环境 base: - /srv/salt/
1.2.2 关闭salt-minion服务
使用 masterless 模式时是不需要启动任何服务的,包括salt-master和salt-minion。
[root@node2 ~]# systemctl stop salt-minion [root@node2 ~]# systemctl disable salt-minion Removed symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service. [root@node2 ~]# systemctl status salt-minion ● salt-minion.service - The Salt Minion Loaded: loaded (/usr/lib/systemd/system/salt-minion.service> Active: inactive (dead) Docs: man:salt-minion(1) file:///usr/share/doc/salt/html/contents.html https://docs.saltproject.io/en/latest/contents.html 11月 02 15:09:47 node2 salt-minion[443952]: To repair this iss> 11月 02 15:09:47 node2 salt-minion[443952]: Or restart the Sal> 11月 02 15:10:06 node2 systemd[1]: salt-minion.service: Main p> 11月 02 15:10:06 node2 systemd[1]: salt-minion.service: Failed> 11月 02 15:13:24 node2 systemd[1]: Starting The Salt Minion... 11月 02 15:13:24 node2 systemd[1]: Started The Salt Minion. 11月 29 22:35:49 node2 systemd[1]: Stopping The Salt Minion... 11月 29 22:35:49 node2 salt-minion[451713]: [WARNING ] Minion > 11月 29 22:35:50 node2 salt-minion[451713]: The Salt Minion is> 11月 29 22:35:50 node2 systemd[1]: Stopped The Salt Minion.
1.2.3 salt-call
masterless模式执行模块或状态时需要使用salt-call命令,而不再是salt或者salt-ssh。需要注意的是要使用salt-call的--local选项。
[root@node2 ~]# salt-call --local cmd.run 'df -h' local: Filesystem Size Used Avail Use% Mounted on devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 600K 1.9G 1% /dev/shm tmpfs 1.9G 9.0M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/mapper/rhel-root 17G 2.7G 15G 16% / /dev/sda1 1014M 179M 836M 18% /boot tmpfs 376M 0 376M 0% /run/user/0
1.2.3 salt-call
masterless模式执行模块或状态时需要使用salt-call命令,而不再是salt或者salt-ssh。需要注意的是要使用salt-call的--local选项
[root@node2 base]# salt-call --local state.sls test local: ---------- ID: yang_useradd Function: user.present Name: yang Result: True Comment: New user yang created Started: 23:14:36.388461 Duration: 259.236 ms Changes: ---------- fullname: gid: 1001 groups: - yang home: /home/yang homephone: name: yang other: passwd: x roomnumber: shell: /bin/bash uid: 1001 workphone: Summary for local ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1 Total run time: 259.236 ms [root@node2 base]#
涉及到高可用时,数据的同步是个永恒的话题,我们必须保证高可用的2个master间使用的数据是一致的,包括:
/etc/salt/master配置文件
/etc/salt/pki目录下的所有key
/srv/下的salt和pillar目录下的所有文件
nfs挂载
rsync同步
使用gitlab进行版本控制
安全相关:
为保证数据的同步与防止丢失,可将状态文件通过gitlab
进行版本控制管理。
环境说明:
主机名 | ip | 职责 |
---|---|---|
master | 192.168.75.128 | 主master |
node2 | 192.168.75.142 | 备master |
node1 | 192.168.75.150 | minion |
我们需要用salt来管理公司的所有机器,那么salt的master就不能宕机,否则就会整个瘫痪,所以我们必须要对salt进行高可用。salt的高可用配置非常简单,只需要改一下minion配置文件,将master用列表的形式列出即可。
在node2上安装salt-master(matser上已安装salt-master,node1上已安装salt-minion)
[root@masters ~]# rpm --import https://repo.saltproject.io/py3/redhat/8/x86_64/latest/SALTSTACK-GPG-KEY.pub [root@masters ~]# curl -fsSL https://repo.saltproject.io/py3/redhat/8/x86_64/latest.repo | tee /etc/yum.repos.d/salt.repo [root@masters ~]# yum -y install salt-master
修改node1的minion配置文件
[root@node1 ~]# vim /etc/salt/minion #master: salt master: 192.168.75.128 //指定主master [root@node1 ~]# systemctl restart salt-minion
minion生成证书并授权给master
[root@master ]# salt-key -L Accepted Keys: Denied Keys: Unaccepted Keys: node1 Rejected Keys: [root@master ]# salt-key -ya The following keys are going to be accepted: Unaccepted Keys: node1 Key for minion node1 accepted. [root@master ]# salt-key -L Accepted Keys: node1 Denied Keys: Unaccepted Keys: Rejected Keys: [root@master ]# salt 'node1' test.ping node1: True
传输证书给node2
[root@master ~]# scp /etc/salt/master 192.168.75.142:/etc/salt/master [root@master ~]# scp /etc/salt/master 192.168.75.142:/etc/salt/master [root@master ~]# scp -r /srv/salt 192.168.75.142:/srv/
修改minion的配置文件,使其与node2建立连接
[root@node1 ~]# vim /etc/salt/minion #master: salt master: 192.168.75.142 //指定主master [root@node1 ~]# systemctl restart salt-minion [root@node2 ]# salt-key -L Accepted Keys: Denied Keys: Unaccepted Keys: node1 Rejected Keys: [root@node2 ]# salt-key -ya The following keys are going to be accepted: Unaccepted Keys: node1 Key for minion node1 accepted. [root@node2 ]# salt-key -L Accepted Keys: node1 Denied Keys: Unaccepted Keys: Rejected Keys: [root@node2 ]# salt 'node1' test.ping node1: True
进行高可用设置,修改minion配置文件
[root@node1 ~]# vim /etc/salt/minion #master: salt master: - 192.168.75.128 - 192.168.75.142
人为设置故障测试
[root@node1 ~]# vim /etc/salt/minion # beacons) without a master connection master_type: failover ---------- # connection events. # master_alive_interval: 3 [root@node1 ~]# systemctl restart salt-minion
在两台master上测试
[root@master ~]# salt 'node1' test.ping node1: True [root@node2 ~]# salt 'node1' test.ping web: 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 20211129114455692315 ERROR: Minions returned with non-zero exit code
停止主master服务,在node2上ping
[root@master ~]# systemctl status salt-master ● salt-master.service - The Salt Master Server Loaded: loaded (/usr/lib/systemd/system/salt-master.service> Active: inactive (dead) since Mon 2021-11-29 23:55:12 CST; > Docs: man:salt-master(1) file:///usr/share/doc/salt/html/contents.html https://docs.saltproject.io/en/latest/contents.html Process: 1035 ExecStart=/usr/bin/salt-master (code=exited, s> Main PID: 1035 (code=exited, status=0/SUCCESS) 11月 29 23:17:54 master systemd[1]: Starting The Salt Master S> 11月 29 23:18:07 master systemd[1]: Started The Salt Master Se> 11月 29 23:55:12 master systemd[1]: Stopping The Salt Master S> 11月 29 23:55:12 master salt-master[1035]: [WARNING ] Master r> 11月 29 23:55:12 master salt-master[1035]: The salt master is > 11月 29 23:55:12 master systemd[1]: Stopped The Salt Master Se> [root@noded2 ~]# salt 'node1' test.ping node1: True