salt是一個配置管理系統,可以維護預約義狀態的遠程節點(好比,確保指定的報被安裝,指定的服務在運行)。一個分佈式遠程執行系統,用來在遠程節點(能夠是單個節點,也能夠是任意規則挑選出來的節點)上執行命令和查詢數據。開發其的目的是爲遠程執行提供最好的解決方案,並使遠程執行變得更好,更快,更簡單。php
saltstack(中國用戶組www.saltstack.cn)基於Python開發,c/s架構支持多平臺,比puppet輕量,在遠程執行命令時很是快捷,配置和使用比puppet容易,能實現puppet幾乎全部的功能。node
saltstack的優點:有master端和minion端,執行的信息比較穩定,不容易丟失信息,或者出現失聯主機的狀況;有封裝好的http-api,能夠直接啓動salt-api就能夠經過http協議進行調用。不須要進行第二次的封裝。python
1 yum -y install epel-release 2 yum clean all 3 yum makecache
1 salt-master 安裝: yum -y install salt-master 2 salt-master 配置: 3 修改minion配置文件 vim /etc/salt/master 4 interface: 10.0.0.15 5 或者 6 [root@localhost salt]# sed -i 's/#interface: 0.0.0.0/interface: 10.0.0.15/g' /etc/salt/master 7 [root@localhost salt]# sed -e '/^$/d;/^#/d;' /etc/salt/master 8 interface: 10.0.0.15
1 salt-minion 安裝: yum -y install salt-minion 2 salt-minion 配置: 3 修改minion配置文件 vim /etc/salt/minion 4 master: 192.168.56.11 能夠是主機名須要解析(指定服務端的IP地址) 5 id: 惟一標識符,能夠不配,不配默認就是主機名 6 或者 7 [root@localhost salt]# sed -i 's/#master: salt/master: 10.0.0.15/g' /etc/salt/minion 8 [root@localhost salt]# sed -e '/^$/d;/^#/d;' /etc/salt/minion 9 master: 10.0.0.15
1 啓動master: 2 [root@localhost salt]# systemctl start salt-master 3 [root@localhost salt]# systemctl status salt-master 4 5 啓動minion: 6 [root@localhost salt]# systemctl start salt-minion 7 [root@localhost salt]# systemctl status salt-minion
一、查看 minion 列表(這時候 10.0.0.129是紅色的,表示未經過認證,10.0.0.15是綠色的,表示經過認證了)nginx
二、認證全部 key,固然你也能夠經過
salt-key -a 10.0.0.129 指定某臺 minion 進行認證 key,或者salt-key -A進行全部認證keyweb
三、接着繼續查看 minion 列表 (這時候10.0.0.129 已經變爲綠色,說明 key 已被添加)正則表達式
四、簡單測試(經過 salt-master 進行檢測)express
能夠ping到 10.0.0.129 返回值爲 True,說明 salt-master 和 salt-minion 能夠通信,安裝到此結束。json
1 minion在第一次啓動時,會在/etc/salt/pki/minion/(該路徑在/etc/salt/minion裏面設置)下自動生成minion.pem(private key)和 minion.pub(public key),而後將 minion.pub發送給master。 2 master在接收到minion的public key後,經過salt-key命令accept minion public key,這樣在master的/etc/salt/pki/master/minions下的將會存放以minion id命名的 public key,而後master就能對minion發送指令了。 3 經常使用指令: 4 5 salt-key -L 或者salt-key 顯示全部的key 6 salt-key -D :刪除全部認證主機id證書 7 salt-key -d keys_values -y 8 salt-key -A:接收全部id證書請求 9 salt-key -a id :接受單個id證書請求
1 (1)-E, --pcre,經過正則表達式進行匹配: 2 salt -E 'web(9|10)*' test.ping -t 1 3 (2)-L, --list, 主機id進行過濾: 4 salt -L "*app*" cmd.run "df -h" 5 (3) -G, --grain, 根據被控主機的grains信息進行過濾 6 salt -G 'role:nginx' cmd.run "ls /export" 7 (4) -I, --pillar, 根據被控主機的pillar信息進行過濾 8 salt -I 'myname:xiang' test.ping -t 5 9 (5) -S, --ipcidr, 根據minion的ip地址進行匹配 10 salt -S 192.168.1.1 test.ping 11 salt -S 192.168.1.0/24 test.ping 12 salt -S 192.168.0.0/16 test.ping 13 salt -S 192.0.0.0/8 test.ping 14 (6)檢查客戶端是否掛掉: 15 salt-run manage.status |head 16 salt-run manage.down
1 saltstack佔用兩個端口4505和4506: 2 確保master端的4505和4506端口開通 3 Minion的key確實別master接受 4 經過test.ping 模塊,雙方均可以ping通 5
1 Runner 模塊 在master端執行的 salt-run 2 Module 模塊 經過master同步到minion端, 在minion執行 3 salt-call saltutil.sync_modules 4 salt-call saltutil.sync_all:包括:beacons: 5 clouds: engines: grains: log_handlers: modules: 6 output: proxymodules: renderers: returners: sdb: 7 states: utils: 8 9 Grins 模塊 記錄minion的屬性key:value 10 Pillar模塊 記錄全部minion通用的屬性,而後同步到minion端 11 salt-call saltutil.refresh_pillar 12 salt ‘*’ saltutil.refresh_pillar 13 14 cmd模塊 salt ‘*’ cmd.run 「df -h」 15 ping模塊 salt ‘*’ test.ping –t 5 16 cp 模塊 17 file_roots: 18 base: 19 - /export/salt/root 20 salt根目錄:在master中file_roots定義的路徑,salt://test.txt至關於/export/salt/root/test.txt 21 salt 'wms5test1.app.172.17.23.176' cp.get_file salt://nscd.conf /tmp/xiang.txt 22 23 24 25 cron模塊: 26 salt '*' cron.raw_cron root (查看定時任務) 27 salt '*' cron.set_job root '*' '*' '*' '*' 1 /export/scripts/rm_log.sh 28 salt '*' cron.rm_job root /export/scripts/rm_log.sh (寫全沒效果) 29 30 31 dnsutil模塊 32 salt '*' dnsutil.hosts_append /etc/hosts 127.0.0.1 xiang.com 33 salt '*' dnsutil.hosts_remove /etc/hosts xiang.com 34 35 file模塊: 36 salt '*' file.chown /etc/passwd root root 37 salt '*' file.copy /path/to/src /path/to/dst 38 salt '*' file.file_exists /etc/hosts 39 salt '*' file.directory_exists /etc/ 40 salt '*' file.get_mod /etc/passwd 41 salt '*' file.set_mod /etc/passwd 0644 42 salt '*' file.mkdir /tmp/test 43 salt '*' file.sed /export/servers/nginx/conf/nginx.conf 'debug' 'warn' 44 salt '*' file.append /tmp/test.txt "welcome xiang" 45 salt '*' file.remove /tmp/1.txt 46 47 network模塊: 48 salt '*' network.dig www.qq.com 49 salt '*' network.ping www.qq.com 50 salt '*' network.ip_addrs 51 52 pkg包管理模塊: 53 管理yum, apt-get等 54 salt '*' pkg.install php 55 salt '*' pkg.remove php 56 salt '*' pkg.upgrade (升級全部的軟件包) 57 58 service模塊 59 salt '*' service.enable nginx 60 salt '*' service.disable nginx 61 salt '*' service.restart nginx 62 63 Grains模塊 64 自定義grians(在minion上定義的) 65 grains是在minion啓動時蒐集一些信息,如操做系統類型,網卡,內核版本,cpu架構等 66 salt "*" grains.ls 列出全部grains項目名字 67 salt "*app.*" grains.items 列出全部grains項目以及值 68 grains的信息並非動態的,並不會實時變化,它只是在minion啓動時收集到的 69 咱們能夠根據grains收集到的一些信息,作一些配置管理工做 70 在minion上:vim /etc/salt/grains 71 role: nginx 72 env: test 73 重啓service salt-minion restart 74 獲取grians: 75 salt "*" grains.item role env 76 或者: 77 salt -G "*" role:nginx cmd.run "hostname「 78 salt ‘*’grains.items 79 80 Pillar模塊 81 pillar(在master上定義)(yaml語法) 82 在配置文件中找pillar的文件路勁: 83 84 找到之後,mkdir /export/salt/pillar 85 vim top.sls 86 base: 87 "*": 88 - test 89 vim test.sls 90 conf: xiang 91 而後刷新pillar: salt '*' saltutil.refresh_pillar 92 驗證:salt '*' pillar.items conf 93 或者: salt -I 'conf:xiang' test.ping
1 yum install salt-api -y
1 cd /etc/pki/tls/certs 2 # 生成自簽名證書, 過程當中須要輸入key密碼及RDNs 3 make testcert 4 cd /etc/pki/tls/private/ 5 # 解密key文件,生成無密碼的key文件, 過程當中須要輸入key密碼,該密碼爲以前生成證書時設置的密碼 6 openssl rsa -in localhost.key -out localhost_nopass.key
1 useradd -M -s /sbin/nologin salt-api 2 echo "salt-api" | passwd salt-api —stdin
1 sed -i '/#default_include/s/#default/default/g' /etc/salt/master 2 mkdir /etc/salt/master.d
1 cat /etc/salt/master.d/api.conf 2 rest_cherrypy: 3 port: 8000 4 ssl_crt: /etc/pki/tls/certs/localhost.crt 5 ssl_key: /etc/pki/tls/private/localhost_nopass.key
1 cat /etc/salt/master.d/eauth.conf 2 external_auth: 3 pam: 4 salt-api: 5 - .* 6 - '@wheel' 7 - '@runner'
1 systemctl start salt-master 2 systemctl start salt-api
1 yum install salt-minion -y 2 修改配置 3 sed -i "/^#master: salt/c master: 192.168.104.76" /etc/salt/minion 4 啓動 client 5 systemctl start salt-minion
1 [root@node76 salt]# salt-key -L 2 Accepted Keys: 3 Denied Keys: 4 Unaccepted Keys: 5 node76 6 Rejected Keys: 7 [root@node76 salt]# salt-key -A 8 The following keys are going to be accepted: 9 Unaccepted Keys: 10 node76 11 Proceed? [n/Y] Y 12 Key for minion node76 accepted. 13 [root@node76 salt]# salt-key -L 14 Accepted Keys: 15 node76 16 Denied Keys: 17 Unaccepted Keys: 18 Rejected Keys:
1 curl -k https://192.168.104.76:8000/login -H "Accept: application/x-yaml" -d username='salt-api' -d password='salt-api' -d eauth='pam' 2 return: 3 - eauth: pam 4 expire: 1520269544.2591 5 perms: 6 - .* 7 - '@wheel' 8 - '@runner' 9 start: 1520226344.259099 10 token: 593a7224f988f28b84d58b7cda38fe5e5ea07d98 11 user: salt-api
1 --sslv3 指定sslv3版本 2 -k 忽略證書獲取https內容 3 -s 指定使用靜默(silent)方式 4 -i 指定SaltAPI收到服務器返回的結果同時顯示HTTP Header。 5 -H 指定一個特定的Header給遠端服務器,當SaltAPI 須要發送appliton-tion/json Header時。會以咱們但願的JSON格式返回結果 6 -d 想遠端服務器發送POST請求,以key=value的格式發送 ,注意key=v時,必須緊挨=號兩邊 7 8 9 獲取token後就可使用token通訊 10 注:重啓salt-api後token改變
1 下面功能相似於「salt '*' test.ping」: 2 curl -k https://192.168.104.76:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token: ded897184a942ca75683276c29d787ea71c207a9" -d client='local' -d tgt='*' -d fun='test.ping' 3 return: 4 - node76: true
1 client : 模塊,python處理salt-api的主要模塊,‘client interfaces <netapi-clients>’ 2 local : 使用‘LocalClient <salt.client.LocalClient>’ 發送命令給受控主機,等價於saltstack命令行中的'salt'命令 3 local_async : 和local不一樣之處在於,這個模塊是用於異步操做的,即在master端執行命令後返回的是一個jobid,任務放在後臺運行,經過產看jobid的結果來獲取命令的執行結果。 4 runner : 使用'RunnerClient<salt.runner.RunnerClient>' 調用salt-master上的runner模塊,等價於saltstack命令行中的'salt-run'命令 5 runner_async : 異步執行runner模塊 6 wheel : 使用'WheelClient<salt.wheel.WheelClient>', 調用salt-master上的wheel模塊,wheel模塊沒有在命令行端等價的模塊,但它一般管理主機資源,好比文件狀態,pillar文件,salt配置文件,以及關鍵模塊<salt.wheel.key>功能相似於命令行中的salt-key。 7 wheel_async : 異步執行wheel模塊 8 備註:通常狀況下local模塊,須要tgt和arg(數組),kwarg(字典),由於這些值將被髮送到minions並用於執行所請求的函數。而runner和wheel都是直接應用於master,不須要這些參數。 9 tgt : minions 10 fun : 函數 11 arg : 參數 12 expr_form : tgt的匹配規則 13 'glob' - Bash glob completion - Default 14 'pcre' - Perl style regular expression 15 'list' - Python list of hosts 16 'grain' - Match based on a grain comparison 17 'grain_pcre' - Grain comparison with a regex 18 'pillar' - Pillar data comparison 19 'nodegroup' - Match on nodegroup 20 'range' - Use a Range server for matching 21 'compound' - Pass a compound match string
1 下面功能相似於「salt '*' cmd.run ifconfig」: 2 curl -k https://192.168.104.76:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token: ded897184a942ca75683276c29d787ea71c207a9" -d client='local' -d tgt='*' -d fun='cmd.run' -d arg='uptime' 3 return: 4 - node76: ' 13:18:46 up 161 days, 2:23, 1 user, load average: 0.15, 0.09, 0.10'
1 下面功能相似於「salt '*' state.sls ifconfig」: 2 curl -k https://192.168.104.76:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token: ded897184a942ca75683276c29d787ea71c207a9" -d client='local' -d tgt='*' -d fun='state.sls' -d arg='ifconfig' 3 return: 4 - node76: 5 cmd_|-ifconfig_|-ifconfig_|-run: 6 __run_num__: 0 7 changes: 8 pid: 30954 9 retcode: 0 10 stderr: '' 11 stdout: "eth2 Link encap:Ethernet HWaddr 00:50:56:B5:5C:28 \n \ 12 \ inet addr:192.168.90.63 Bcast:192.168.90.255 Mask:255.255.255.0\n\ 13 \ inet6 addr: fe80::250:56ff:feb5:5c28/64 Scope:Link\n \ 14 \ UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1\n RX packets:825051\ 15 \ errors:0 dropped:0 overruns:0 frame:0\n TX packets:434351 errors:0\ 16 \ dropped:0 overruns:0 carrier:0\n collisions:0 txqueuelen:1000\ 17 \ \n RX bytes:60353823 (57.5 MiB) TX bytes:27062672 (25.8 MiB)\n\ 18 \nlo Link encap:Local Loopback \n inet addr:127.0.0.1 \ 19 \ Mask:255.0.0.0\n inet6 addr: ::1/128 Scope:Host\n UP\ 20 \ LOOPBACK RUNNING MTU:16436 Metric:1\n RX packets:808 errors:0\ 21 \ dropped:0 overruns:0 frame:0\n TX packets:808 errors:0 dropped:0\ 22 \ overruns:0 carrier:0\n collisions:0 txqueuelen:0 \n \ 23 \ RX bytes:59931 (58.5 KiB) TX bytes:59931 (58.5 KiB)" 24 comment: Command "ifconfig" run 25 duration: 11.991 26 name: ifconfig 27 result: true 28 start_time: '13:59:06.334112'
1 下面功能相似於"salt -L '192.168.90.61,192.168.90.63' test.ping" 2 curl -k https://192.168.104.76:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token: ded897184a942ca75683276c29d787ea71c207a9" -d client='local' -d tgt='node76' -d expr_form='list' -d fun='test.ping' 3 return: 4 - node76: true
1 curl -k https://192.168.104.76:8000 -H "Accept: application/json" -H "X-Auth-Token: ded897184a942ca75683276c29d787ea71c207a9" -d client='local' -d tgt='node76' -d fun='cmd.run' -d arg='uptime' 2 {"return": [{"node76": " 13:25:20 up 161 days, 2:30, 1 user, load average: 0.01, 0.06, 0.08"}]}
1 curl -k -s https://192.168.104.76:8000/jobs/20180306112645047796 -H "Accept: application/x-yaml" -H "X-Auth-Token: a7f8b2fab2bfb05334f8a314d97925c1bec1d14c" 2 info: 3 - Arguments: 4 - uptime 5 Function: cmd.run 6 Minions: 7 - node76 8 Result: 9 node76: 10 return: ' 11:26:45 up 162 days, 31 min, 1 user, load average: 0.12, 0.07, 11 0.08' 12 StartTime: 2018, Mar 06 11:26:45.047796 13 Target: node76 14 Target-type: glob 15 User: salt-api 16 jid: '20180306112645047796' 17 return: 18 - node76: ' 11:26:45 up 162 days, 31 min, 1 user, load average: 0.12, 0.07, 0.08' 19 #結果 20 curl -k -s https://192.168.104.76:8000/jobs/20180306112645047796 -H "Accept: application/x-yaml" -H "X-Auth-Token: a7f8b2fab2bfb05334f8a314d97925c1bec1d14c" 21 info: 22 - Arguments: 23 - uptime 24 Function: cmd.run 25 Minions: 26 - node76 27 Result: 28 node76: 29 return: ' 11:26:45 up 162 days, 31 min, 1 user, load average: 0.12, 0.07, 30 0.08' 31 StartTime: 2018, Mar 06 11:26:45.047796 32 Target: node76 33 Target-type: glob 34 User: salt-api 35 jid: '20180306112645047796' 36 return: 37 - node76: ' 11:26:45 up 162 days, 31 min, 1 user, load average: 0.12, 0.07, 0.08'