1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 调用阿里云SLB API设置SLB权重

调用阿里云SLB API设置SLB权重

时间:2022-01-27 08:28:00

相关推荐

调用阿里云SLB API设置SLB权重

背景:服务部署在阿里云ECS上,使用了阿里云SLB做负载均衡。使用jenkins做金丝雀发时,希望可以自动修改SLB的权重,实现服务器的上下线。

安装阿里云Python SDK

完成以下操作安装Python SDK:

安装SDK核心库。

推荐使用 pip 命令,安装阿里云SDK核心库:

pip install aliyun-python-sdk-core

安装云产品SLB的SDK。

pip install aliyun-python-sdk-slb

如果您在使用 pip 时,遇到网络导致的安装问题时,请尝试配置镜像来加速安装过程。PyPI 镜像设置参见:/mirror/pypi。

您也可以在GitHub下载各产品的SDK。

代码实现

set_aliyun_slb_weight.py

#!/usr/bin/env python#coding=utf-8#version 3.9.1#AK,SK,regionID,SLBID,ECSID都需要替换import sysfrom aliyunsdkcore.client import AcsClientfrom aliyunsdkcore.acs_exception.exceptions import ClientExceptionfrom aliyunsdkcore.acs_exception.exceptions import ServerExceptionfrom aliyunsdkslb.request.v0515.SetBackendServersRequest import SetBackendServersRequestweight1=sys.argv[1]weight2=sys.argv[2]client = AcsClient('AK', 'SK', 'regionID')request1 = SetBackendServersRequest()request1.set_accept_format('json')request1.set_LoadBalancerId("SLBID")request1.set_BackendServers("[{ \"ServerId\": \"ECS1ID\", \"Weight\": %s, \"Type\": \"ecs\", \"Port\":\"80\",\"Description\":\"ECS1\" }]" %weight1)response = client.do_action_with_exception(request1)#print(str(response, encoding='utf-8'))request2 = SetBackendServersRequest()request2.set_accept_format('json')request2.set_LoadBalancerId("SLBID")request2.set_BackendServers("[{ \"ServerId\": \"ECS2ID\", \"Weight\": %s, \"Type\": \"ecs\", \"Port\":\"80\",\"Description\":\"ECS2\" }]" %weight2)response = client.do_action_with_exception(request2)print(str(response, encoding='utf-8'))

调用脚本实现先发布到ECS1:set_aliyun_slb_weight 0 100,待测试通过后,再发布到ECS2

set_aliyun_slb_weight 100 0,最后设置set_aliyun_slb_weight 100 100

参考1:/document_detail/53090.html?spm=a2c1g.8271268.10000.158.2e90df25MByzQI

参考2:/document_detail/27566.html?spm=a2c4g.11186623.6.809.20005795sk2FT7

参考3:/#/?product=Slb&version=-05-15&api=DescribeLoadBalancerAttribute&params={%22RegionId%22:%22cn-shenzhen%22,%22LoadBalancerId%22:%22lb-wz9az39295kx3dhyq3zfr%22}&tab=DEMO&lang=PYTHON

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。