python3 安裝api
# 首先安裝python3 tar zxvf Python-3.5.1.tgz cd Python-3.5.1 . ./configure make make install mv /usr/bin/python /usr/bin/python2 # 如果是軟連接,可以直接刪除 ln -s /usr/local/bin/python3.5 /usr/bin/python vim /usr/bin/yum # 修改Yum,使yum依然有效,yum依靠老版本的python #!/usr/bin/python 修改為#!/usr/bin/python2 # 修改完/usr/bin/yum 依然還有問題,可以嘗試修改/usr/libexec/urlgrabber-ext-down的文件python抬頭 # 使用Python3直接啟動salt,因為默認環境已經切換的python3, 所以直接啟動即可 systemctl start salt-master systemctl start salt-minion systemctl start salt-api #如果有問題可執行 apt-get install python3-dev
python3 -m pip install salt==2017.7.2
安裝配置api
[root@k8s_master ~]# pip install pyopenssl #安裝依賴包 [root@k8s_master ~]# yum install salt-api #安裝api服務 [root@k8s_master ~]# useradd username && echo password | passwd --stdin $username #添加用戶 [root@k8s_master ~]# salt-call --local tls.create_self_signed_cert #生成key,默認目錄/etc/pki/tls/certs [root@k8s_master ~]# vi /etc/salt/master rest_cherrypy: port: 8000 #api端口 debug: True ssl_crt: /etc/pki/tls/certs/localhost.crt ssl_key: /etc/pki/tls/certs/localhost.key external_auth: pam: #采用linux自帶的加密方式 saltapi: #之前添加的用戶名 - .* #主機組 - '@wheel' #所需要調用的模塊(如,cmd.*,file.*),這里表示所有權限 - '@runner' [root@k8s_master ~]# service salt-api start
[root@k8s_master ~]# service salt-master restart
另一種配置方法
# 添加配置文件,可以把eauth.conf和api.conf合二為一為api.conf [root@centos7 ~]# mkdir -p /etc/salt/master.d/ # 這個目錄默認不存在,需要手動創建,在/etc/salt/master主配置文件中有指定,類似include [root@centos7 ~]# vim /etc/salt/master.d/eauth.conf # 處於安全因素,一般只給特定模塊的使用權限,這里給saltapi用戶所有模塊的使用權限 external_auth: pam: saltapi: - .* - '@wheel' - '@runner' [root@k8s_master ~]# vim /etc/salt/master.d/api.conf rest_cherrypy: port: 8000 #salt-api 監聽端口 ssl_crt: /etc/pki/tls/certs/localhost.crt #ssl認證的證書 ssl_key: /etc/pki/tls/private/localhost_nopass.key
salt-api 配置文件詳解
# salt-api 配置文件詳解 port : #必須填寫,salt-api啟動的端口 host : #默認啟動於0.0.0.0,可以不填寫 debug : #默認為False,True開啟后,會輸出debug日志 log_access_file : #HTTP訪問日志的路徑,在2016.11.0版本添加的 log_error_file : #HTTP錯誤日志路徑,在2016.11.0版本添加的 ssl_crt : #SSL證書的絕對路徑 ssl_key: #SSK證書的私鑰絕對路徑 ssl_chain : #在使用PyOpenSSL時可選參數,將證書出遞給' Context.load_verify_locations ' disable_ssl : #禁用SSL標識。認證證書將會被送進clear webhook_disable_auth : False webhook_url : /hook thread_pool : 100 socket_queue_size : 30 expire_responses : True max_request_body_size : 1048576 collect_stats : False stats_disable_auth : False 更多詳細參數請見:https://github.com/saltstack/salt/blob/develop/salt/netapi/rest_cherrypy/app.py
日志查看/var/log/salt/master 或者 salt-master -l debug
測試
[root@k8s_master ~]# netstat -ntlp|grep 8080 [root@k8s_master ~]# curl -k https://192.168.132.148:8000/login -H "Accept: application/x-yaml" -d username='saltapi' -d password='saltapi' -d eauth=pam return: - eauth: pam expire: 1509451924.783881 perms: - .* - '@wheel' - '@runner' start: 1509408724.78388 token: 1887a5cdc7b675687915fa9363a54ec8224d4282 user: saltapi
每重啟一次,token就會改變
Api功能介紹
/login #登錄 /logout #登出
/minions #獲取minion信息或執行命令
/jobs #獲取job信息(get)
/run #獲取runner信息(POST)
/events #獲取events 相關信息
/stats #獲取api server相關信息
使用api
#salt "k8s_master" test.ping
[root@k8s_master ~]# curl -sSk https://192.168.132.148:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token:1887a5cdc7b675687915fa9363a54ec8224d4282" -d client=local -d tgt="k8s_master" -d fun=test.ping return: - k8s_master: true
#salt "k8s_master" cmd.run ifconfig
[root@k8s_master ~]# curl -sSk https://192.168.132.148:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token:1887a5cdc7b675687915fa9363a54ec8224d4282" -d client=local -d tgt="k8s_master" -d fun=cmd.run -d arg='ifconfig' return: - k8s_master: "docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500\n inet\ \ 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0\n ether 02:42:45:ad:eb:93\ \ txqueuelen 0 (Ethernet)\n RX packets 0 bytes 0 (0.0 B)\n RX\ \ errors 0 dropped 0 overruns 0 frame 0\n TX packets 0 bytes 0 (0.0\ \ B)\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\n\nens33:\ \ flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500\n inet 192.168.132.148\ \ netmask 255.255.255.0 broadcast 192.168.132.255\n inet6 fe80::c428:23:d920:d67\ \ prefixlen 64 scopeid 0x20<link>\n ether 00:0c:29:c3:8a:0c txqueuelen\ \ 1000 (Ethernet)\n RX packets 20990361 bytes 4125436909 (3.8 GiB)\n\ \ RX errors 0 dropped 0 overruns 0 frame 0\n TX packets 21835453\ \ bytes 6713860060 (6.2 GiB)\n TX errors 0 dropped 0 overruns 0 carrier\ \ 0 collisions 0\n\nlo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536\n inet\ \ 127.0.0.1 netmask 255.0.0.0\n inet6 ::1 prefixlen 128 scopeid 0x10<host>\n\ \ loop txqueuelen 1 (Local Loopback)\n RX packets 11082886 bytes\ \ 5710854934 (5.3 GiB)\n RX errors 0 dropped 0 overruns 0 frame 0\n\ \ TX packets 11082886 bytes 5710854934 (5.3 GiB)\n TX errors 0\ \ dropped 0 overruns 0 carrier 0 collisions 0" [root@k8s_master ~]#
#salt 'k8s_master' state.sls ifconfig,執行ifconfig.sls
[root@k8s_master ~]# curl -sSk https://192.168.132.148:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token:1887a5cdc7b675687915fa9363a54ec8224d4282" -d client=local -d tgt="k8s_master" -d fun='state.sls' -d arg='ifconfig'
#salt -L 'ip1,ip2,ip3' test.ping
[root@k8s_master ~]# curl -sSk https://192.168.132.148:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token:1887a5cdc7b675687915fa9363a54ec8224d4282" -d client=local -d tgt="ip1,ip2,ip3" -d expr_form='list' -d fun='test.ping'
#slat -N 'master1' cmd.run ifconfig
[root@k8s_master ~]# curl -sSk https://192.168.132.148:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token:1887a5cdc7b675687915fa9363a54ec8224d4282" -d client=local -d tgt="master1" -d expr_form='nodegroup' -d fun='cmd.run' -d arg='ifconfig'
#以json格式輸出
[root@k8s_master ~]# curl -sSk https://192.168.132.148:8000 -H "Accept: application/json" -H "X-Auth-Token:1887a5cdc7b675687915fa9363a54ec8224d4282" -d client=local -d tgt="k8s_master" -d fun='state.sls' -d arg='ifconfig'
參數解釋:
client : 模塊,python處理salt-api的主要模塊,‘client interfaces <netapi-clients>’ local : 使用‘LocalClient <salt.client.LocalClient>’ 發送命令給受控主機,等價於saltstack命令行中的'salt'命令 local_async : 和local不同之處在於,這個模塊是用於異步操作的,即在master端執行命令后返回的是一個jobid,任務放在后台運行,通過產看jobid的結果來獲取命令的執行結果。 runner : 使用'RunnerClient<salt.runner.RunnerClient>' 調用salt-master上的runner模塊,等價於saltstack命令行中的'salt-run'命令 runner_async : 異步執行runner模塊 wheel : 使用'WheelClient<salt.wheel.WheelClient>', 調用salt-master上的wheel模塊,wheel模塊沒有在命令行端等價的模塊,但它通常管理主機資源,比如文件狀態,pillar文件,salt配置文件,以及關鍵模塊<salt.wheel.key>功能類似於命令行中的salt-key。 wheel_async : 異步執行wheel模塊 備注:一般情況下local模塊,需要tgt和arg(數組),kwarg(字典),因為這些值將被發送到minions並用於執行所請求的函數。而runner和wheel都是直接應用於master,不需要這些參數。 tgt : minions fun : 函數 arg : 參數 expr_form : tgt的匹配規則 'glob' - Bash glob completion - Default 'pcre' - Perl style regular expression 'list' - Python list of hosts 'grain' - Match based on a grain comparison 'grain_pcre' - Grain comparison with a regex 'pillar' - Pillar data comparison 'nodegroup' - Match on nodegroup 'range' - Use a Range server for matching 'compound' - Pass a compound match string