Loading... # 背景介绍 在一个ucloud的后端配置中,有2+1个后端,前两个为容器集群gateway的地址和端口,而另一个是haproxy反代,用于特殊情况下的限速处理。所以,如果启用2,则正常工作;如果启用1,则全部流量过haproxy并接受限速处理。 问题是什么呢? 梳理文件过程中,之前写的脚本不见了,我嘞个去。但是呢,有其他的文件。稀里糊涂的,不知道怎么搞的了。 # 处理办法 既然这样,那就根据还有依稀的记忆,进行还原吧。 # 处理过程 编写脚本 整个代码如下。 ``` #!/bin/env python3 from ucloud.core import exc from ucloud.client import Client import sys client = Client({ "region": "cn-bj2", "project_id": "xxx", "public_key": "xxx", "private_key": "xxx", }) def info(): try: resp = client.ulb().describe_ulb({ 'ULBId': 'ulb-23jeos' }) except exec.UCloudException as e: print(e) else: print(resp) def start(): try: resp = client.ulb().update_backend_attribute({ 'ULBId': 'ulb-23jeos', 'BackendId': 'backend-cobbpkhwmgl', 'Enabled': 1, }) except exec.UCloudException as e: print(e) else: print(resp) try: resp = client.ulb().update_backend_attribute({ 'ULBId': 'ulb-23jeos', 'BackendId': 'backend-dm5jju3t', 'Enabled': 0, }) except exec.UCloudException as e: print(e) else: print(resp) try: resp = client.ulb().update_backend_attribute({ 'ULBId': 'ulb-23jeos', 'BackendId': 'backend-nweidvwj', 'Enabled': 0, }) except exec.UCloudException as e: print(e) else: print(resp) def stop(): try: resp = client.ulb().update_backend_attribute({ 'ULBId': 'ulb-23jeos', 'BackendId': 'backend-cobbpkhwmgl', 'Enabled': 0, }) except exec.UCloudException as e: print(e) else: print(resp) try: resp = client.ulb().update_backend_attribute({ 'ULBId': 'ulb-23jeos', 'BackendId': 'backend-dm5jju3t', 'Enabled': 1, }) except exec.UCloudException as e: print(e) else: print(resp) try: resp = client.ulb().update_backend_attribute({ 'ULBId': 'ulb-23jeos', 'BackendId': 'backend-nweidvwj', 'Enabled': 1, }) except exec.UCloudException as e: print(e) else: print(resp) if __import__('__main__').__name__ == '__main__': if len(sys.argv) > 1: command = sys.argv[1].lower() if command == 'start': start() elif command == 'stop': stop() elif command == 'info': info() else: print('Usage: %s start|stop|info' % sys.argv[0]) else: print('No command provided. please enter start|stop|info.') ``` 代码首先定义了一段ucloud的信息,包括region project_id以及aksk信息。 然后定义了三个函数,info用于获取该ulb的描述信息,里面也有3个后端的backendid。start函数用于启用haproxy节点禁用日常节点。stop函数用于关闭haproxy节点启用日常节点 。 最后来一个main函数,用于获取用户输入并调用相应函数。 测试结果还不错: ![2024-02-22T03:16:09.png][1] 那个错误的地方不知道从哪里拷贝了一个backendid。其他两次正常测试,一次是start,一次是stop。 # 参考 https://ucloud.github.io/ucloud-sdk-python3/services.html#ulb [1]: https://www.sddts.cn/usr/uploads/2024/02/4159696894.png 最后修改:2024 年 05 月 11 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏