CentOS 7 安裝 saltstack
salt介紹
salt是一個配置管理系統,能夠維護預定義狀態的遠程節點(比如,確保指定的報被安裝,指定的服務在運行)。一個分布式遠程執行系統,用來在遠程節點(可以是單個節點,也可以是任意規則挑選出來的節點)上執行命令和查詢數據。開發其的目的是為遠程執行提供最好的解決方案,並使遠程執行變得更好,更快,更簡單。
saltstack(中國用戶組www.saltstack.cn)基於Python開發,c/s架構支持多平台,比puppet輕量,在遠程執行命令時非常快捷,配置和使用比puppet容易,能實現puppet幾乎所有的功能。
saltstack的優勢:有master端和minion端,執行的信息比較穩定,不容易丟失信息,或者出現失聯主機的情況;有封裝好的http-api,可以直接啟動salt-api就可以通過http協議進行調用。不需要進行第二次的封裝。
salt安裝
1.安裝epel yum源
1 yum -y install epel-release 2 yum clean all 3 yum makecache
2.安裝 salt-master並配置
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
3.安裝 salt-minion 並配置
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
4.啟動 salt-master 和 salt-minion
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
5.測試 saltstack (接下來都在 salt-master 端操作)
1、查看 minion 列表(這時候 10.0.0.129是紅色的,表示未通過認證,10.0.0.15是綠色的,表示通過認證了)
2、認證所有 key,當然你也可以通過
salt-key -a 10.0.0.129 指定某台 minion 進行認證 key,或者salt-key -A進行所有認證key
3、接着繼續查看 minion 列表 (這時候10.0.0.129 已經變為綠色,說明 key 已被添加)
4、簡單測試(通過 salt-master 進行檢測)
可以ping到 10.0.0.129 返回值為 True,說明 salt-master 和 salt-minion 可以通訊,安裝到此結束。
saltstack的設置
1.master與minion的認證
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證書請求
2.saltstack遠程執行機器
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
3.saltstack配置文件詳解
1 saltstack占用兩個端口4505和4506: 2 確保master端的4505和4506端口開通 3 Minion的key確實別master接受 4 通過test.ping 模塊,雙方都可以ping通 5
4.saltstack的幾種模塊介紹
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
Salt-API安裝配置及使用(Python3使用saltstack和salt-api)
安裝 salt-api
1 yum install salt-api -y
salt-api配置
生成自簽名證書(用於ssl)
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
創建用於salt-api的用戶
1 useradd -M -s /sbin/nologin salt-api 2 echo "salt-api" | passwd salt-api —stdin
修改/etc/salt/master文件
1 sed -i '/#default_include/s/#default/default/g' /etc/salt/master 2 mkdir /etc/salt/master.d
新增配置文件/etc/salt/master.d/api.conf
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
新增配置文件/etc/salt/master.d/eauth.conf
1 cat /etc/salt/master.d/eauth.conf 2 external_auth: 3 pam: 4 salt-api: 5 - .* 6 - '@wheel' 7 - '@runner'
啟動salt-master and salt-api
1 systemctl start salt-master 2 systemctl start salt-api
安裝一個salt client
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
master 上接受key
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:
api使用
使用curl 獲取token
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改變
測試minion端的聯通性
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'
使用state.sls
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'
使用Targeting
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
以json格式輸出
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"}]}
使用jobs
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'