腾讯云pythonSDK3.0修改服务器权重

通过腾讯云SDK3.0修改clb后端服务器的权重

pip install --user tencentcloud-sdk-python  #首尾颠倒

脚本说明:

  1. 七层clb:
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.clb.v20180317 import clb_client, models
import json, sys

qcloud_config = {
    'Region': 'bj',
    'secretId': '**************************************',
    'secretKey': '********************************',
}

params = {}

def get_InstanceId(ip):
    try:
        # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
        cred = credential.Credential(qcloud_config.get("secretId"), qcloud_config.get("secretKey"))

        client = clb_client.ClbClient(cred, "ap-beijing")

        # 实例化一个请求对象
        req = models.DescribeTargetsRequest()
        params = {
            'LoadBalancerId': 'lb-p12guaq3',
            'ListenerId': 'lbl-bfgzhz4x',
            'Protocol': 'HTTP',
            'Port': '80',
        }

        req._deserialize(params)

        # 通过client对象调用想要访问的接口,需要传入请求对象
        resp = client.DescribeTargets(req)

        IP_InstanceId = {}
        resp_json = json.loads(resp.to_json_string())
        Listeners = resp_json['Listeners'][0]
        InstanceId_IP = Listeners['Rules'][0]['Targets']
        for i in InstanceId_IP:
            IP_InstanceId[i['PrivateIpAddresses'][0]] = i['InstanceId']

        return IP_InstanceId[ip]

    except TencentCloudSDKException as err:
        print(err)

def main(ip=sys.argv[1], weight=sys.argv[2]):
    try:
        # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
        cred = credential.Credential(qcloud_config.get("secretId"), qcloud_config.get("secretKey"))

        client = clb_client.ClbClient(cred, "ap-beijing")

        # 实例化一个请求对象
        req = models.ModifyTargetWeightRequest()

        params = {
            'LoadBalancerId': 'lb-p12guaq3',
            'ListenerId': 'lbl-bfgzhz4x',
            'Targets': [{'InstanceId': get_InstanceId(ip), 'Port': 8000},],
            'LocationId': 'loc-cdywy29b',
            'Weight': weight,
        }

        req._deserialize(params)

        # 通过client对象调用想要访问的接口,需要传入请求对象
        resp = client.ModifyTargetWeight(req)

        # 输出json格式的字符串回包
        print(json.dumps(json.loads(resp.to_json_string()), indent=2))

    except TencentCloudSDKException as err:
        print(err)

if __name__ == '__main__':
    main()

    对于不同的业务,如果是七层clb,需要修改get_InstanceId函数中params中的LoadBalancerIdListenerIdProtocolPort以获取InstanceId。同时修改main函数中的LoadBalancerIdListenerIdTargetsLocationId即可。使用方法:脚本+要修改权重的IP+新的权重。

 

    2.四层clb:

from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.clb.v20180317 import clb_client, models

import json, sys

qcloud_config = {
    'Region': 'bj',
    'secretId': '**************************************',
    'secretKey': '********************************',
}

params = {}
host = 'compass.zhenai.com'
rec_file = '/tmp/clb_rs_weight'

def get_InstanceId(ip):
    try:
        # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
        cred = credential.Credential(qcloud_config.get("secretId"), qcloud_config.get("secretKey"))

        client = clb_client.ClbClient(cred, "ap-beijing")

        # 实例化一个请求对象
        req = models.DescribeTargetsRequest()
        params = {
            'LoadBalancerId': 'lb-8g6onqdn',
            'ListenerId': 'lbl-afns2299',
            'Protocol': 'TCP',
            'Port': '9001',
        }
        req._deserialize(params)

        # 通过client对象调用想要访问的接口,需要传入请求对象
        resp = client.DescribeTargets(req)

        IP_InstanceId = {}
        resp_json = json.loads(resp.to_json_string())
        Listeners = resp_json['Listeners'][0]
        Targets = Listeners['Targets']
        for i in Targets:
            IP_InstanceId[i['PrivateIpAddresses'][0]] = i['InstanceId']
            # print(i['InstanceId'], i['PrivateIpAddresses'][0]),
        return IP_InstanceId[ip]

    except TencentCloudSDKException as err:
        print(err)

def main(ip=sys.argv[1], weight=sys.argv[2]):
    try:
        # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
        cred = credential.Credential(qcloud_config.get("secretId"), qcloud_config.get("secretKey"))

        client = clb_client.ClbClient(cred, "ap-beijing")

        # 实例化一个请求对象
        req = models.ModifyTargetWeightRequest()
        params = {
            'LoadBalancerId': 'lb-8g6onqdn',
            'ListenerId': 'lbl-afns2299',
            'Targets': [{'InstanceId': get_InstanceId(ip), 'Port': 9001},],
            'Weight': weight,
        }

        req._deserialize(params)

        # 通过client对象调用想要访问的接口,需要传入请求对象
        resp = client.ModifyTargetWeight(req)

        # 输出json格式的字符串回包
        print(json.dumps(json.loads(resp.to_json_string()), indent=2))

    except TencentCloudSDKException as err:
        print(err)

if __name__ == '__main__':
    main()

    对于不同的业务,如果是四层clb,需要修改get_InstanceId函数中params中的LoadBalancerIdListenerIdProtocolPort以获取InstanceId。同时修改main函数中的LoadBalancerIdListenerIdTargets即可。使用方法:脚本+要修改权重的IP+新的权重。

你可能感兴趣的:(python学习,运维)