SaltStack安裝及入門
簡介
SaltStack簡稱salt,是一個配置管理工具,類似Ansible、Chef和Puppet,可以用腳本批量操作多臺機器。有如下幾個特點:
- 部署簡單、方便;
- 支持大部分UNIX/Linux及Windows環境;
- 主從集中化管理;
- 配置簡單、功能強大、擴展性強;
- 主控端(master)和被控端(minion)基於證書認證,安全可靠;
- 支持API及自定義模塊,可通過Python輕鬆擴展。
可以批量遠程執行命令、修改配置文件、部署軟件、定時執行任務…只有你想不到,沒有他做不到。
SlatStack是C/S結構[[1]],分爲服務器(master)和客戶端(minion),服務器也是一個客戶端。
安裝
通過包管理器方式安裝
sudo add-apt-repository ppa:saltstack/salt
sudo apt-get update
sudo apt-get install salt-master salt-minion salt-ssh salt-syndic
通過源碼方式安裝
curl -o install_salt.sh -L https://bootstrap.saltstack.com
sudo sh install_salt.sh git v0.16.4
基本配置及介紹
/etc/salt/master
是master的配置文件;/etc/salt/minion
是minion的配置文件;/etc/init.d/salt-xx start/stop/restart
可以執行服務的啓停;- 其採用他TCP 4505/4506兩個端口通信,4505發,4506收;
- 默認的log的地址是
/var/log/salt/minion(master)
,默認的log_level是warning,可以通過配置文件修改;
入門
備註: 由於幾乎所有的操作均需要以root用戶運行,可以直接切換到root用戶
啓動master服務
在master機器上以root權限執行/etc/init.d/salt-master start
,當然如果已經不小心啓動了,可以嘗試採用/etc/init.d/salt-master restart
;
如果要檢查,可以嘗試執行salt-call cmd.run 'ifconfig'
,如果能夠正常執行,則啓動正常(裏面的內容會不一樣):
local:
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:8046551 errors:0 dropped:0 overruns:0 frame:0
TX packets:8046551 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:861624168 (861.6 MB) TX bytes:861624168 (861.6 MB)
啓動minion服務
-
需要修改配置文件
vim /etc/salt/minion
,在其中找到master: salt
這一行,修改爲master: 192.168.8.187
, 當然,你需要保證這兩臺機器是可以互相通信的,(如果你用一臺模擬的話,那就無所謂了) -
修改minion_id
vim /etc/salt/minion_id
,刪除原來的內容,爲你的minion取一個合適的名字,比如minion1,這是minion的唯一標識,後續執行命令都會用得到; -
啓動minion
/etc/ini.d/salt-minion start
同樣可以採用restart;如果此時你去看一下log(/var/log/salt/minion
),你會發現不斷的打印The Salt Master has cached the public key for this node, this salt minion will wait for 10 seconds before attempting to re-authenticate
, 這是因爲salt採用AES加密通信,minion會將自己的證書傳遞給master,但是master並不一定同意連接。
master端接受
在服務端的機器上,如果master接收到請求,需要執行命令以接受請求。這些操作是通過salt-key完成的。
salt-key -L #列出minion的列表
# Accepted Keys:
# minion2
# Denied Keys:
# Unaccepted Keys:
# minion1
salt-key -a minion1 #接受minion1的連接
# 按照提示輸入y即可
至此minion和master的連接就成功了。
執行命令
salt '<target>' <function> [arguments]
- target指的是minion的id,’*'指全部minion
# 支持正則表達、列表以及 id/grains 混合。
# 普通 (按 minion-id 過濾)
salt '*.example.org' test.ping
# 正則表達 (regular expression)
salt -E 'virtmach[0-9]' test.ping
# 列表 (list)
salt -L 'foo,bar,baz,quo' test.ping
# 混合型 (combined)
salt -C 'G@os:Ubuntu and webser* or E@database.*' test.ping
| Letter | Match Type | Example |
| :----- | :----------------- | :---------------------------------- |
| G | Grains glob | G@os:Ubuntu |
| E | 正則匹配 Minion ID | E@web\d+\.(dev\|qa\|prod)\.loc |
| P | Grains 正則匹配 | P@os:(RedHat\|Fedora\|CentOS) |
| L | List of minions | L@minion1,minion3 or bl*.domain.com |
| I | Pillar glob | I@pdata:foobar |
| S | Subnet/IP address | [email protected]/24 or [email protected] |
| R | Range cluster | R@%foo.bar |
- function指預製在minion上的命令,包括cmd.run等; (function列表參考頁)
# 舉例
salt '*' test.ping
salt '*' cmd.run "ls -l | awk '/foo/{print \$2}'"
salt '*' state.sls xxx
- arguments是function的參數,以空格分隔,在function後面,如
-l debug
;
salt 命令參數:http://docs.saltstack.com/en/latest/ref/cli/salt.html
salt module 使用指南:http://docs.saltstack.cn/topics/tutorials/modules.html
salt builtin execution modules 列表: http://docs.saltstack.com/en/latest/ref/modules/all/
salt logging 指南:http://salt.readthedocs.org/en/latest/ref/configuration/logging/index.html