在2014年刚开始使用阿里云的时候python才刚入门,当时有个需求比较急就发现了几乎万能的aliyuncli。阿里云的命令行是基于对应服务的python SDK基础上的,所以aliyuncli操作SLB的时候会需要aliyun-python-sdk-slb这个包。
1. 先安装对应服务包:
pip install aliyun-python-sdk-slb==2.1.2
pip install aliyuncli==2.1.2
2.想调用API怎么样, ecs , ess, mts, rds, slb四个服务的接口使用pip安装sdk后aliyuncli既可以使用。
其它服务使用下面方法切换对应的version即可
aliyuncli ram ConfigVersion --version v20150501
v20150501则是SDK的版本,可以从你的SDK代码结构内发现。
[root@test-node1 ~]# ls /usr/lib/python2.6/site-packages/aliyunsdkram/request/
__init__.py __init__.pyc v20150501
[root@test-node1 ~]# aliyuncli slb --help
usage: aliyuncli <command> <operation> [options and parameters]
[slb] valid operations as follows:
AddBackendServers | AddListenerWhiteListItem
AddTags | AddVServerGroupBackendServers
ConfigVersion | CreateLoadBalancer
CreateLoadBalancerHTTPListener | CreateLoadBalancerHTTPSListener
CreateLoadBalancerTCPListener | CreateLoadBalancerUDPListener
CreateRules | CreateVServerGroup
DeleteCACertificate | DeleteLoadBalancer
DeleteLoadBalancerListener | DeleteRules
DeleteServerCertificate | DeleteVServerGroup
DescribeCACertificates | DescribeHealthStatus
DescribeListenerAccessControlAttribute | DescribeLoadBalancerAttribute
DescribeLoadBalancerHTTPListenerAttribute | DescribeLoadBalancerHTTPSListenerAttribute
DescribeLoadBalancerTCPListenerAttribute | DescribeLoadBalancerUDPListenerAttribute
DescribeLoadBalancers | DescribeRegions
DescribeRuleAttribute | DescribeRules
DescribeServerCertificates | DescribeTags
DescribeVServerGroupAttribute | DescribeVServerGroups
DescribeZones | ModifyLoadBalancerInternetSpec
ModifyVServerGroupBackendServers | RemoveBackendServers
RemoveListenerWhiteListItem | RemoveTags
RemoveVServerGroupBackendServers | SetBackendServers
SetCACertificateName | SetListenerAccessControlStatus
SetLoadBalancerHTTPListenerAttribute | SetLoadBalancerHTTPSListenerAttribute
SetLoadBalancerName | SetLoadBalancerStatus
SetLoadBalancerTCPListenerAttribute | SetLoadBalancerUDPListenerAttribute
SetRule | SetServerCertificateName
SetVServerGroupAttribute | ShowVersions
StartLoadBalancerListener | StopLoadBalancerListener
UploadCACertificate | UploadServerCertificate
下面展示一个操作脚本
#!/bin/bash
#自动创建负载均衡并添加后端服务器并开启监听
#author:new.nginxs.net
#date:20170718
CTIME=$(date "+%Y-%m-%d-%H-%m")
#创建负载均衡
/usr/local/bin/aliyuncli slb CreateLoadBalancer > /tmp/create.txt
SLBID=$(grep "LoadBalancerId" /tmp/create.txt | awk -F '"' '{print $4}')
VIP=$(grep "Address" /tmp/create.txt | awk -F '"' '{print $4}')
#创建http监听,监听80端口后端也是80端口,带宽-1表示不限制,,关闭粘性session,关闭健康检查
/usr/local/bin/aliyuncli slb CreateLoadBalancerHTTPListener --LoadBalancerId "$SLBID" --ListenerPort 80 --BackendServerPort 80 --Bandwidth -1 --XForwardedFor on --StickySession off --HealthCheck off
#i-u1cv2vgd9:要添加到后端ECS ID
/usr/local/bin/aliyuncli slb AddBackendServers --LoadBalancerId "$SLBID" --BackendServers [\{\"ServerId\":\"i-udsg4vgd9\",\"Weight\":\"100\"\}]
#启动监听
/usr/local/bin/aliyuncli slb StartLoadBalancerListener --LoadBalancerId "$SLBID" --ListenerPort 80
#测试通过slb是否可以访问后端服务器
sleep 10;
STATUS1=$(curl -s --head http://"$VIP"/heath.html | grep '200 OK')
if [ -n "$STATUS1" ] ;then
echo "=====SLB access OK====="
else
echo "=====SLB access error====="
exit 1;
fi
总结:
aliyuncli可以在阿里云的python SDK基础上进行调用。具体命令参数可以直接参考各个服务的 API的参数进行填写,所以特别好用,
适合不会开发的运维同学使用。