Saltstack的yum部署和基礎使用(基於CentOS 7的部署)

weixin_34247155發表於2017-08-07

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

關閉防火牆和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
解決依賴關係
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訊息釋出埠 Default: 4505
publish_port: 4505
#工作執行緒數,應答和接受minion Default: 5
worker_threads: 100
#客戶端與服務端通訊的埠 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
# 客戶端與服務端通訊的埠。 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常用引數說明:

格式:salt-key 引數 [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 使用該引數可免去證照操作的互動,除非對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某個資訊(系統版本):
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或建立新檔案/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

自定義節點組到配置檔案裡,通過一個組進行操作:

$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
#分批執行,每次2個
salt '*' -b 2 test.ping
#分批執行,每次總共的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'
#執行命令並只返回錯誤輸出
salt '*' cmd.run_stderr 'uptime'
#執行命令並只返回正確輸出
salt '*' cmd.run_stdout 'uptime'
#上傳本地指令碼到伺服器上執行,返回所有資訊
#建立指令碼路徑:vim /srv/salt/scripts/test1.sh
salt '*' cmd.script salt://scripts/test1.sh```

相關文章