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 inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 ether 02:42:45:ad:eb:93 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.132.148 netmask 255.255.255.0 broadcast 192.168.132.255 inet6 fe80::c428:23:d920:d67 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c3:8a:0c txqueuelen 1000 (Ethernet) RX packets 20990361 bytes 4125436909 (3.8 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 21835453 bytes 6713860060 (6.2 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 11082886 bytes 5710854934 (5.3 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 11082886 bytes 5710854934 (5.3 GiB) 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