文章目录
masterless
应用场景
- master 与 minion 网络不通或通信有延迟,即网络不稳定
- 想在 minion 端直接执行状态
- 仅有一台主机
masterless配置
masterless配置步骤:
- 配置文件注释master行
- 取消注释file_client并设其值为local
- 设置file_roots
- 设置pillar_roots
- 关闭salt-minion服务
准备一台安装好salt-minion的主机:
- 修改配置文件:
[root@node ~]# vim /etc/salt/minion
#master: salt //注释此行
file_client: local //设置file_client
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服务
[root@node ~]# systemctl stop salt-minion
[root@node ~]# systemctl status salt-minion
● salt-minion.service - The Salt Minion
Loaded: loaded (/usr/lib/systemd/system/salt-minion.servi>
Active: inactive (dead) since Mon 2021-11-29 18:22:38 CST>
Docs: man:salt-minion(1)
file:///usr/share/doc/salt/html/contents.html
https://docs.saltproject.io/en/latest/contents.ht>
Process: 1047 ExecStart=/usr/bin/salt-minion (code=exited,>
Main PID: 1047 (code=exited, status=0/SUCCESS)
Nov 29 18:07:31 node salt-minion[1047]: [ERROR ] Error whi>
Nov 29 18:08:23 node salt-minion[1047]: [ERROR ] Error whi>
Nov 29 18:09:13 node salt-minion[1047]: [ERROR ] Error whi>
Nov 29 18:10:03 node salt-minion[1047]: [ERROR ] Error whi>
Nov 29 18:10:53 node salt-minion[1047]: [ERROR ] Error whi>
Nov 29 18:22:37 node systemd[1]: Stopping The Salt Minion...
Nov 29 18:22:37 node salt-minion[1047]: [WARNING ] Minion re>
Nov 29 18:22:38 node salt-minion[1047]: The Salt Minion is s>
Nov 29 18:22:38 node systemd[1]: salt-minion.service: Succee>
Nov 29 18:22:38 node systemd[1]: Stopped The Salt Minion.
lines 1-19/19 (END)
- salt-call
masterless模式执行模块或状态时需要使用salt-call命令,而不再是salt或者salt-ssh。需要注意的是要使用salt-call的–local选项。
//测试
[root@node ~]# salt-call --local cmd.run 'date'
local:
Mon Nov 29 18:28:06 CST 2021
//测试是否可以执行状态文件,base环境中有命令历史的状态文件
[root@node base]# tree /srv
/srv
├── pillar
│ ├── base
│ └── prod
└── salt
├── base
│ └── history
│ └── main.sls
└── prod
7 directories, 1 file
[root@node history]# salt-call --local state.sls history.main
local:
----------
ID: /etc/profile
Function: file.append
Result: True
Comment: Appended 1 lines
Started: 18:45:03.944108
Duration: 15.358 ms
Changes:
----------
diff:
---
+++
@@ -83,3 +83,4 @@
. /etc/bashrc
fi
fi
+export HISTTIMEFORMAT="%F %T `whoami` "
Summary for local
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 15.358 ms
//执行状态文件成功,masterless部署完成
salt-master高可用
用salt来管理公司的所有机器,那么salt的master就不能宕机,否则就会整个瘫痪,所以我们必须要对salt进行高可用。
salt-master高可用之数据同步
涉及到高可用时,数据的同步是个永恒的话题,我们必须保证高可用的2个master间使用的数据是一致的,包括:
- /etc/salt/master配置文件
- /etc/salt/pki目录下的所有key
- /srv/下的salt和pillar目录下的所有文件
保障这些数据同步的方案有:
- nfs挂载
- rsync同步
- 使用gitlab进行版本控制
salt-master高可用配置
- 环境:
主机IP | 角色 | 需安装的应用 |
---|---|---|
192.168.218.132 | 主master | salt-master |
192.168.218.133 | 备master | salt-master |
192.168.218.144 | minion | salt-minion |
- 安装配置salt-minion
//提前准备好salt源
[root@minion ~]# ls /etc/yum.repos.d/
......
salt-8.repo
//安装salt-minion
[root@minion ~]# yum -y install salt-minion
//修改配置文件
[root@minion ~]# vim /etc/salt/minion
......
master: 192.168.218.132
......
//启动salt-minion
[root@minion ~]# systemctl enable --now salt-minion
//主master上查看minion的key
[root@master ~]# salt-key
Accepted Keys:
Denied Keys:
Unaccepted Keys:
master
minion //已有minion的key
Rejected Keys:
//接受key
[root@master ~]# salt-key -ya minion
The following keys are going to be accepted:
Unaccepted Keys:
minion
Key for minion minion accepted.
//测试能否ping通
[root@master ~]# salt 'minion' test.ping
minion:
True
- 安装配置备master-bak机
//提前准备好salt源
[root@master-bak ~]# ls /etc/yum.repos.d/
......
salt-8.repo
//安装salt-master
[root@master-bak ~]# yum -y install salt-master
//把主master /etc/salt/pki/master/下的密钥复制到备机/etc/salt/pki/master/下
[root@master ~]# scp -r /etc/salt/pki/master/ 192.168.218.133:/etc/salt/pki/master/
root@192.168.218.133's password:
minion 100% 451 599.3KB/s 00:00
master 100% 451 453.5KB/s 00:00
master.pem 100% 1675 2.1MB/s 00:00
master.pub 100% 451 902.9KB/s 00:00
salt-ssh.rsa 100% 2590 1.9MB/s 00:00
salt-ssh.rsa.pub 100% 565 729.7KB/s 00:00
//开启salt-master
[root@master-bak ~]# systemctl enable --now salt-master
//修改minion配置文件master ip为备机ip,测试能否ping通
[root@minion ~]# vim /etc/salt/minion
......
master: 192.168.218.133
......
//重启minion
[root@minion ~]# systemctl restart salt-minion
//备机查看key
[root@master-bak ~]# salt-key
Accepted Keys:
Denied Keys:
Unaccepted Keys:
minion //已出现minion的key
Rejected Keys:
//接受minion的key
[root@master-bak ~]# salt-key -ya minion
The following keys are going to be accepted:
Unaccepted Keys:
minion
Key for minion minion accepted.
//测试备机能否ping通minion
[root@master-bak ~]# salt 'minion' test.ping
minion:
True
- 修改minion配置文件为高可用
[root@minion ~]# vim /etc/salt/minion
//修改一下参数
master:
- 192.168.218.132
- 192.168.218.144
master_type: failover
master_alive_interval: 3
//重启salt-minion
[root@minion ~]# systemctl restart salt-minion
//主master测试能否ping通minion
[root@master ~]# salt 'minion' test.ping
minion:
True
//关闭主master的salt-master,测试备master是否接管master,能否ping通minion
[root@master-bak ~]# salt 'minion' test.ping
minion:
True
//同步主master数据到备master
[root@master ~]# scp -r /srv/* 192.168.218.133:/srv/
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.218.132 | master | salt-master |
192.168.218.133 | syndic | salt-master salt-sybdic |
192.168.218.130 | minion | salt-minion |
192.168.218.144 | minion | salt-minion |
- 安装salt-master与salt-syndic
在192.168.218.133上安装salt-master与salt-syndic,安装前请自行配置yum源
//源
[root@syndic ~]# ls /etc/yum.repos.d/
......
salt-8.repo
//安装salt-master、salt-syndic
[root@syndic ~]# yum -y install salt-master salt-syndic
- 修改顶层master的配置文件
//取消注释order_master
//将order_master的值设为True
[root@master ~]# vim /etc/salt/master
......
order_masters: True
//重启master
[root@master ~]# systemctl restart salt-master
- 配置syndic
修改syndic所在主机的master配置文件:- 取消注释syndic_master
- 将syndic_master的值设为master的IP
[root@syndic ~]# vim /etc/salt/master
......
syndic_master: 192.168.218.132
//启动服务
[root@syndic ~]# systemctl enable --now salt-master
[root@syndic ~]# systemctl enable --now salt-syndic
- 配置minion
配置minion,将master指向syndic所在主机
//在所有minion上做同样的操作
[root@node1 ~]# vim /etc/salt/minion
......
master: 192.168.218.133
[root@node2 ~]# vim /etc/salt/minion
......
master: 192.168.218.133
//启动salt-minion
[root@node1 ~]# systemctl enable --now salt-minion
[root@node2 ~]# systemctl enable --now salt-minion
- 在syndic上接受minion主机的key
//查看key
[root@syndic ~]# salt-key
Accepted Keys:
Denied Keys:
Unaccepted Keys:
node1
node2
Rejected Keys:
//接受所有minion的key
[root@syndic ~]# 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.
[root@syndic ~]# salt-key -L
Accepted Keys:
node1
node2
Denied Keys:
Unaccepted Keys:
Rejected Keys:
- 在master上接受syndic主机的key
//查看key
[root@master ~]# salt-key
Accepted Keys:
Denied Keys:
Unaccepted Keys:
syndic
Rejected Keys:
//接受syndic的key
[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:
Rejected Keys:
- 在master上执行模块或状态检验有几个minion应答
[root@master ~]# salt '*' test.ping
node1:
True
node2:
True
- 同步master上的数据到syndic上
//同步之前把syndic上的配置文件中的file_roots和pillar_roots编辑好
//同步数据
[root@master ~]# scp -r /srv/* 192.168.218.133:/srv/