saltstack API(一) 安裝並測試

python3 安裝apinode

 

# 首先安裝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

另外一種配置方法python

# 添加配置文件,能夠把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 配置文件詳解 linux

# 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 debuggit

 

測試github

[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就會改變web

 

Api功能介紹docker

/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'

 參數解釋:express

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
相關文章
相關標籤/搜索