salt-master 192.168.81.11 centOS 7.3
salt-minions1 192.168.81.12 centOS 7.3
salt-minions2 192.168.81.60 centOS 7.0
關(guān)閉防火墻和selinux
systemctl stop firewalld
setenforce 0
配置yum源
vi /etc/yum.repo.d/saltstack.repo
[saltstack-repo]
name=SaltStack repo for Red Hat Enterprise Linux $releasever
baseurl=https://repo.saltstack.com/yum/redhat/$releasever/$basearch/latest
enabled=1
gpgcheck=1
gpgkey=https://repo.saltstack.com/yum/redhat/$releasever/$basearch/latest/SALTSTACK-GPG-KEY.pub
https://repo.saltstack.com/yum/redhat/$releasever/$basearch/latest/base/RPM-GPG-KEY-CentOS-7
安裝SaltStack存儲庫和密鑰
yum install https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el7.noarch.rpm
yum clean expire-cache
yum upgrade
yum -y install epel-release
解決依賴關(guān)系
yum install zeromq3 m2crypto python-crypto python-jinja2 python-msgpack python-yaml python-zmq -y
salt-master安裝
yum install -y salt-master
修改配置文件
vim /etc/salt/master
#master消息發(fā)布端口 Default: 4505
publish_port: 4505
#工作線程數(shù)咳秉,應(yīng)答和接受minion Default: 5
worker_threads: 100
#客戶端與服務(wù)端通信的端口 Default: 4506
ret_port: 4506
#自動接受所有客戶端
auto_accept: True
#啟動salt-master
systemctl start salt-master
#檢查是否啟動成功
netstat -tnlp
tcp 0 0 0.0.0.0:4505 0.0.0.0:* LISTEN 20839/python
tcp 0 0 0.0.0.0:4506 0.0.0.0:* LISTEN 20845/python
salt-minions1 & salt-minions2 安裝
yum install -y salt-minion
#修改配置文件
vim /etc/salt/minion
# master IP或域名
master: 192.168.81.11
# 客戶端與服務(wù)端通信的端口澜建。 Default: 4506
master_port: 4506
# id minion的唯一標示。Default: hostname
id: minion_192.168.81.XX
啟動minion
systemctl start salt-minion
salt-master查看(master接受minion的minion.pub 何之,存放/etc/salt/pki/master/minions目錄幕侠,以minion的id命名)
[root@controller ~]# ls /etc/salt/pki/master/
master.pem minions minions_denied minions_rejected
master.pub minions_autosign minions_pre
salt-minion查看(minion將master的public key晤硕,存放/etc/salt/pki/minion目錄)
[root@compute01 ~]# ls /etc/salt/pki/minion
minion_master.pub minion.pem minion.pub
[root@localhost ~]# ls /etc/salt/pki/minion
minion.pem minion.pub
查看master的key列表
[root@controller ~]# salt-key -L
Accepted Keys:(接受的key)
minion_192.168.81.12
minion_192.168.81.60
Denied Keys:(否認的key)
Unaccepted Keys:(未接受的key)
Rejected Keys:(拒絕的key)
salt-key常用參數(shù)說明:
格式:salt-key 參數(shù) [minion端ID(可以是IP舞箍,也可以是主機名) [-y]
-L 列出當前所有認證,包括Accepted Keys占拍、Denied Keys捎迫、Unaccepted Keys、Rejected Keys
-a 添加某個或某些個未接受(Unaccepted Keys)認證
-A 添加所有未接受(Unaccepted Keys)認證
-d 刪除某個或某些個已接受(Accepted Keys)認證
-D 刪除所有已接受(Accepted Keys)認證
-y 使用該參數(shù)可免去證書操作的交互贝次,除非對minion端很信任蛔翅,一般不建議使用
-h 幫助
[root@controller ~]# salt '*' test.ping
minion_192.168.81.12:
True
minion_192.168.81.60:
True
查看grains分類:
salt '*' grains.ls
#查看grains所有信息:
salt '*' grains.items
#查看grains某個信息(系統(tǒng)版本):
salt '*' grains.item osrelease
[root@controller ~]# salt '*' grains.item osrelease
minion_192.168.81.12:
----------
osrelease:
7.3.1611
minion_192.168.81.60:
----------
osrelease:
7.3.1611
在minion上自定義grains信息
編輯minion配置文件/etc/salt/minion或創(chuàng)建新文件/etc/salt/grains文件
編輯后重啟salt-minion
[root@compute01 ~]# vim /etc/salt/grains
test:
- test1
[root@compute01 ~]# systemctl restart salt-minion
[root@localhost ~]# vim /etc/salt/grains
test:
- test2
[root@localhost ~]# systemctl restart salt-minion
[root@controller ~]# salt '*' grains.item test
minion_192.168.81.12:
----------
test:
- test1
minion_192.168.81.60:
----------
test:
- test2
匹配grains 某個信息
[root@controller ~]# salt -G 'os:CentOS' test.ping
minion_192.168.81.12:
True
minion_192.168.81.60:
True
自定義節(jié)點組到配置文件里,通過一個組進行操作:
$cat /etc/salt/master.d/nodegroups.conf
nodegroups:
test1: 'L@test1,test2,test3 or *.test.com'
test2: 'G@os:CentOS and *.test.com'
test3: '* and not G@os:CentOS'
用法:
salt -N 'test1' test.ping
#分批執(zhí)行位谋,每次2個
salt '*' -b 2 test.ping
#分批執(zhí)行山析,每次總共的25%
salt '*' -b 25% test.ping
[root@controller ~]# salt '*' cmd.run 'uptime'
minion_192.168.81.12:
22:33:57 up 2:55, 2 users, load average: 0.00, 0.01, 0.05
minion_192.168.81.60:
22:45:21 up 6:00, 3 users, load average: 0.00, 0.01, 0.05
cmd.run的使用
salt '*' cmd.run_all 'uptime'
#執(zhí)行命令并只返回錯誤輸出
salt '*' cmd.run_stderr 'uptime'
#執(zhí)行命令并只返回正確輸出
salt '*' cmd.run_stdout 'uptime'
#上傳本地腳本到服務(wù)器上執(zhí)行,返回所有信息
#創(chuàng)建腳本路徑:vim /srv/salt/scripts/test1.sh
salt '*' cmd.script salt://scripts/test1.sh```