Ver código fonte

add config port

xuekailun 4 meses atrás
pai
commit
bef4ad698c

+ 14 - 11
alb_utils.py

@@ -29,7 +29,7 @@ from alibabacloud_alb20200616 import models as alb_20200616_models
 from alibabacloud_tea_util import models as util_models
 from aliyunsdkalb.request.v20200616.ListServerGroupServersRequest import ListServerGroupServersRequest
 from aliyunsdkcore.acs_exception.exceptions import ClientException, ServerException
-
+from urllib3.connection import port_by_scheme
 
 logging.basicConfig(level=logging.INFO,
                     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
@@ -253,7 +253,7 @@ def get_instances_status(ecs_client, instance_ids):
     """
     获取实例运行状态
     :param ecs_client:
-    :param instance_ids: instance_id, type-liist
+    :param instance_ids: instance_id, type-list
     :return:
     """
     request = DescribeInstanceStatusRequest()
@@ -356,19 +356,20 @@ def send_file_to_ecs(ecs_client, instance_id_list, target_dir, name, content):
 
 
 
-def add_servers_to_server_group(alb_client, server_group_id, instance_id, weight):
+def add_servers_to_server_group(alb_client, server_group_id, instance_id, weight, port):
     """
     添加服务器到ALB服务器组
     :param alb_client: ALB客户端连接
     :param server_group_id: 服务器组ID
     :param instance_id: 实例ID
     :param weight: 权重
+    :param port: 后端服务器使用的端口
     """
     server = alb_models.AddServersToServerGroupRequestServers(
-        port=80,
         server_id=instance_id,
         server_type='ecs',
-        weight=weight
+        weight=weight,
+        port=port
     )
     request = alb_models.AddServersToServerGroupRequest(
         server_group_id=server_group_id,
@@ -383,15 +384,16 @@ def add_servers_to_server_group(alb_client, server_group_id, instance_id, weight
 
 
 
-def remove_servers_from_server_group(alb_client, server_group_id, instance_id):
+def remove_servers_from_server_group(alb_client, server_group_id, instance_id, port):
     """
     从ALB服务器组中移除服务器
     :param alb_client: ALB客户端连接
     :param server_group_id: 服务器组ID
     :param instance_id: 实例ID
+    :param port: 后端服务器使用的端口
     """
     server = alb_models.RemoveServersFromServerGroupRequestServers(
-        port=80,
+        port=port,
         server_id=instance_id,
         server_type='ecs'
     )
@@ -516,13 +518,14 @@ async def list_server_group_servers_async(alb_client, server_group_id):
         return []
 
 
-def update_server_group_servers_attribute(alb_client, server_group_id_list, instance_id_list, weight_list):
+def update_server_group_servers_attribute(alb_client, server_group_id_list, instance_id_list, weight_list, port):
     """
     更新服务器组中的服务器权重
     :param alb_client: ALB客户端
     :param server_group_id_list: 服务器组ID列表
     :param instance_id_list: 实例ID列表
     :param weight_list: 权重修改列表 type-list [(weight, sleep_time), ...]
+    :param port: 后端服务器使用的端口
     """
     for server_group_id in server_group_id_list:
         for instance_id in instance_id_list:
@@ -531,7 +534,7 @@ def update_server_group_servers_attribute(alb_client, server_group_id_list, inst
                     server_type='Ecs',
                     server_id=instance_id,
                     weight=weight,
-                    port=80
+                    port=port
                 )
                 request = alb_20200616_models.UpdateServerGroupServersAttributeRequest(
                     servers=[server],
@@ -548,7 +551,7 @@ def update_server_group_servers_attribute(alb_client, server_group_id_list, inst
                 time.sleep(sleep_time)
 
 
-async def update_server_group_servers_attribute_async(alb_client, server_group_id_list, instance_ids, weight_list):
+async def update_server_group_servers_attribute_async(alb_client, server_group_id_list, instance_ids, weight_list, port):
     """
     异步更新服务器组中的服务器属性
     :param alb_client: ALB客户端
@@ -563,7 +566,7 @@ async def update_server_group_servers_attribute_async(alb_client, server_group_i
                     server_type='Ecs',
                     server_id=instance_id,
                     weight=weight,
-                    port=80
+                    port=port
                 )
                 request = alb_20200616_models.UpdateServerGroupServersAttributeRequest(
                     servers=[server],

+ 3 - 0
gateway/gateway_config.py

@@ -10,6 +10,9 @@ apps = 'piaoquan-gateway'
 repository = 'registry-vpc.cn-hangzhou.aliyuncs.com/stuuudy/{}'
 registry = 'registry-vpc.cn-hangzhou.aliyuncs.com/stuuudy/{}:{}'
 
+# 后端服务器使用的端口
+port = "80"
+
 # 修改负载均衡权限
 alb_client_params = {
     'access_key_id': 'LTAI5tASD5yEZLeC8ffmNebY',

+ 3 - 3
gateway/gateway_update.py

@@ -42,7 +42,7 @@ def checkHealth(ipadd):
             return False  # 健康检查通过,返回
 
 
-def update(instance_id):
+def update(instance_id, port):
 
     time.sleep(10)  # 等待10秒钟
     global success_count
@@ -72,7 +72,7 @@ def update(instance_id):
 
         weight_list = [(10, 5), (20, 5), (40, 5), (60, 5), (80, 5), (100, 5)]
         for server_group_id in gateway_config.server_group_id_list:
-            alb_utils.update_server_group_servers_attribute(alb_client, [server_group_id],   [instance_id], weight_list)
+            alb_utils.update_server_group_servers_attribute(alb_client, [server_group_id],   [instance_id], weight_list, port)
 
         success_count += 1
         print("更新进度" + "%s/%s" % (success_count, total))
@@ -138,5 +138,5 @@ if __name__ == '__main__':
     for instanceID in InstanceIDs:
         for server_group_id in gateway_config.server_group_id_list:
             alb_utils.update_server_group_servers_attribute(alb_client, [server_group_id], [instanceID],
-                                                       [(0, 20)])  # 设置初始权重为0
+                                                       [(0, 20)], port=gateway_config.port)  # 设置初始权重为0
         update(instanceID)

+ 4 - 0
longvideoapi/longvideoapi_config.py

@@ -7,6 +7,10 @@ logging.basicConfig(level=logging.INFO,
 
 slb_id_list = ["lb-bp1qk9mkvjtundlzz7owm", "lb-bp1pj2v06ladvgftgxcp0", "lb-bp1y63rnrb2e64whryghz", "lb-bp17woaq4vz3gnb8ujzvh"]
 server_group_id_list = ["sgp-h79h3lkvua1xs3418y"]
+
+# 后端服务器使用的端口
+port = "80"
+
 # 修改负载均衡权限
 alb_client_params = {
     'access_key_id': 'LTAI5tASD5yEZLeC8ffmNebY',

+ 7 - 4
longvideoapi/longvideoapi_grayscale_update.py

@@ -92,13 +92,14 @@ def remove_container_image(ecs_client, instance_id, container_name_list):
         except Exception as e:
             j += 1
 
-def update_instance(ecs_client, alb_client, instance_id, version):
+def update_instance(ecs_client, alb_client, instance_id, version, port):
     """
     线上机器更新
     :param ecs_client:
     :param alb_client: alb客户端连接
     :param instance_id: instanceId
     :param version: 版本标记
+    :param port: 后端服务器使用的端口
     :return:
     """
     logging.info(f"update instance: {instance_id}")
@@ -108,7 +109,8 @@ def update_instance(ecs_client, alb_client, instance_id, version):
     alb_utils.update_server_group_servers_attribute(alb_client,
                                                longvideoapi_config.server_group_id_list,
                                                health_instance_ids,
-                                               weight_list)
+                                               weight_list,
+                                                port)
     logging.info(f"Set weight to 0 for instance: {instance_id}")
 
     # 2. 移除旧容器并删除旧镜像
@@ -148,7 +150,8 @@ def update_instance(ecs_client, alb_client, instance_id, version):
     alb_utils.update_server_group_servers_attribute(alb_client,
                                                longvideoapi_config.server_group_id_list,
                                                health_instance_ids,
-                                               add_weight_list)
+                                               add_weight_list,
+                                                port)
     logging.info(f"finished instances: {health_instances}, count: {len(health_instances)}")
 
 
@@ -176,7 +179,7 @@ def main():
         # 2. 对其中一台机器进行更新
         instance_id = online_instance_ids[0]
         logging.info(f"update instance start ...")
-        update_instance(ecs_client=ecs_client, alb_client=alb_client, instance_id=instance_id, version=version)
+        update_instance(ecs_client=ecs_client, alb_client=alb_client, instance_id=instance_id, version=version, port=longvideoapi_config.port)
         logging.info(f"update instances end!")
 
     except Exception as e:

+ 6 - 4
longvideoapi/longvideoapi_reduce_with_count.py

@@ -9,12 +9,13 @@ logging.basicConfig(level=logging.INFO,
                     datefmt='%a, %d %b %Y %H:%M:%S')
 
 
-def remove_instances(ecs_client, alb_client, instance_ids):
+def remove_instances(ecs_client, alb_client, instance_ids, port):
     """
     停止并释放机器
     :param ecs_client: 创建客户端
     :param alb_client: ALB 客户端
     :param instance_ids: instanceId 类型列表
+    :param port: 后端服务器使用的端口
     :return: None
     """
     # 1. 摘除流量
@@ -23,7 +24,8 @@ def remove_instances(ecs_client, alb_client, instance_ids):
         alb_utils.update_server_group_servers_attribute(alb_client,
                                                    longvideoapi_config.server_group_id_list,
                                                    instance_ids,
-                                                   weight_list)
+                                                   weight_list,
+                                                    port)
     except Exception as e:
         logging.error(f"Failed to set instance weight: {e}")
         sys.exit()
@@ -35,7 +37,7 @@ def remove_instances(ecs_client, alb_client, instance_ids):
     for server_group_id in longvideoapi_config.server_group_id_list:
         try:
             alb_utils.remove_servers_from_server_group(alb_client=alb_client, server_group_id=server_group_id,
-                                                   instance_id=instance_ids)
+                                                   instance_id=instance_ids, port=port)
             logging.info(f"Successfully removed instances from server group {server_group_id}.")
         except Exception as e:
             logging.error(f"Failed to remove instances from server group {server_group_id}: {e}")
@@ -105,7 +107,7 @@ def main():
         logging.info(f"reduce instances: {reduce_instance_ids}")
 
         # 停止并释放机器
-        remove_instances(ecs_client=ecs_client, alb_client=alb_client, instance_ids=reduce_instance_ids)
+        remove_instances(ecs_client=ecs_client, alb_client=alb_client, instance_ids=reduce_instance_ids, port=longvideoapi_config.port)
         logging.info(f"stop & release instances end!")
     except Exception as e:
         logging.error(e)

+ 6 - 4
longvideoapi/longvideoapi_restart.py

@@ -9,7 +9,7 @@ logging.basicConfig(level=logging.INFO,
                     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                     datefmt='%a, %d %b %Y %H:%M:%S')
 
-def server_restart(ecs_client, alb_client, instance_id, image_name):
+def server_restart(ecs_client, alb_client, instance_id, image_name, port):
     try:
         logging.info(f"Restarting instance: {instance_id}")
         # 获取ip
@@ -20,7 +20,8 @@ def server_restart(ecs_client, alb_client, instance_id, image_name):
         alb_utils.update_server_group_servers_attribute(alb_client=alb_client,
                                                    server_group_id_list=longvideoapi_config.server_group_id_list,
                                                    instance_id_list=instance_id,
-                                                   weight_list=[(0, 20)])
+                                                   weight_list=[(0, 20)],
+                                                   port=port)
         logging.info(f"Set weight to 0 for instance: {instance_id}")
 
         # 连接 Docker 客户端并重启容器
@@ -39,7 +40,8 @@ def server_restart(ecs_client, alb_client, instance_id, image_name):
         alb_utils.update_server_group_servers_attribute(alb_client,
                                                    longvideoapi_config.server_group_id_list,
                                                    instance_id,
-                                                   add_weight_list)
+                                                   add_weight_list,
+                                                   port)
         logging.info(f"Server restart finished, instance: {instance_id}/{ip_address}")
     except Exception as e:
         logging.error(f"Server restart failed, instance: {instance_id}")
@@ -68,7 +70,7 @@ def main():
         # 逐台重启
         image_name = 'longvideoapi'
         for i, instance_id in enumerate(online_instance_ids):
-            server_restart(ecs_client=ecs_client, alb_client=alb_client, instance_id=instance_id, image_name=image_name)
+            server_restart(ecs_client=ecs_client, alb_client=alb_client, instance_id=instance_id, image_name=image_name, port=longvideoapi_config.port)
             logging.info(f"Restart progress: {i + 1}/{online_instance_count}")
         logging.info("All servers restarted successfully!")
     except Exception as e:

+ 4 - 3
longvideoapi/longvideoapi_scaling_k_count.py

@@ -49,13 +49,14 @@ def longvideoapi_health_check(ecs_client, instance_id, max_wait_time=None):
             time.sleep(10)
 
 
-async def ess_instance(ecs_client, alb_client, ess_count, max_workers):
+async def ess_instance(ecs_client, alb_client, ess_count, max_workers, port):
     """
     扩容机器并运行新服务
     :param ecs_client: 购买机器客户端连接
     :param alb_client: 修改负载均衡权限
     :param ess_count: 扩容数量
     :param max_workers: 线程数
+    :param port: 后端服务器使用的端口
     :return:
     """
     # 1. 购买机器并启动
@@ -93,7 +94,7 @@ async def ess_instance(ecs_client, alb_client, ess_count, max_workers):
         time.sleep(20)
         for instance_id, ip in health_instances:
             for server_group_id in longvideoapi_config.server_group_id_list:
-                alb_utils.add_servers_to_server_group(alb_client, server_group_id, instance_id, weight=100)
+                alb_utils.add_servers_to_server_group(alb_client, server_group_id, instance_id, weight=100, port=port)
         logging.info(f"ess count: {ess_count}, "
                      f"create count: {len(ess_instance_ids)}, "
                      f"finished count: {len(health_instances)}")
@@ -122,7 +123,7 @@ def main():
         logging.info(f"ess instance count: {ess_instance_count}")
         asyncio.run(ess_instance(ecs_client=ecs_client,
                                  alb_client=alb_client,
-                                 ess_count=ess_instance_count, max_workers=2))
+                                 ess_count=ess_instance_count, max_workers=2, port=longvideoapi_config.port))
         logging.info(f"ess instances end!")
     except Exception as e:
         logging.error(e)

+ 18 - 11
longvideoapi/longvideoapi_update_k.py

@@ -41,7 +41,7 @@ def server_health_check(ecs_client, instance_id):
             time.sleep(10)
 
 
-async def ess_instance(ecs_client, alb_client, ess_count, max_workers, version):
+async def ess_instance(ecs_client, alb_client, ess_count, max_workers, version, port):
     """
     扩容机器并运行新服务
     :param ecs_client: 购买机器客户端连接
@@ -49,6 +49,7 @@ async def ess_instance(ecs_client, alb_client, ess_count, max_workers, version):
     :param ess_count: 扩容数量
     :param max_workers: 线程数
     :param version: 版本标记
+    :param port: 后端服务器使用的端口
     :return:
     """
     # 1. 购买机器并启动
@@ -84,14 +85,15 @@ async def ess_instance(ecs_client, alb_client, ess_count, max_workers, version):
         health_instance_ids = [instance_id for instance_id, _ in health_instances]
         for server_group_id in longvideoapi_config.server_group_id_list:
             for instance_id in health_instance_ids:
-                alb_utils.add_servers_to_server_group(alb_client, server_group_id, instance_id, weight=0)
+                alb_utils.add_servers_to_server_group(alb_client, server_group_id, instance_id, weight=0, port=port)
                 logging.info(f"Successfully added instance {instance_id} to server group {server_group_id}.")
 
         add_weight_list = [(10, 5), (20, 5), (40, 5), (60, 5), (80, 5), (100, 5)]
         alb_utils.update_server_group_servers_attribute(alb_client,
                                                    longvideoapi_config.server_group_id_list,
                                                    health_instance_ids,
-                                                   add_weight_list)
+                                                   add_weight_list,
+                                                   longvideoapi_config.port)
         logging.info(f"finished instances: {health_instances}, count: {len(health_instances)}")
 
 
@@ -148,7 +150,7 @@ def remove_container_image(ecs_client, instance_id, container_name_list):
             i += 1
 
 
-async def update_instance(ecs_client, alb_client, instance_ids, max_workers, version):
+async def update_instance(ecs_client, alb_client, instance_ids, max_workers, version, port):
     """
     线上机器更新
     :param ecs_client:
@@ -156,6 +158,7 @@ async def update_instance(ecs_client, alb_client, instance_ids, max_workers, ver
     :param instance_ids: instanceId type-list
     :param max_workers:
     :param version: 版本标记
+    :param port: 后端服务器使用的端口
     :return:
     """
     media_index = len(instance_ids)//2
@@ -167,7 +170,8 @@ async def update_instance(ecs_client, alb_client, instance_ids, max_workers, ver
         alb_utils.update_server_group_servers_attribute(alb_client=alb_client,
                                                     server_group_id_list=longvideoapi_config.server_group_id_list,
                                                     instance_id_list=instance_id_list,
-                                                    weight_list=[(0, 20)])
+                                                    weight_list=[(0, 20)],
+                                                    port=port)
         logging.info(f"set weight with 0 finished, instances: {instance_id_list}")
         # 2. 异步移除旧容器并删除旧镜像
         global remove_container_instances
@@ -215,7 +219,8 @@ async def update_instance(ecs_client, alb_client, instance_ids, max_workers, ver
             alb_utils.update_server_group_servers_attribute(alb_client,
                                                        longvideoapi_config.server_group_id_list,
                                                        health_instance_ids,
-                                                       add_weight_list)
+                                                       add_weight_list,
+                                                       port)
             logging.info(f"finished instances: {health_instances}, count: {len(health_instances)}")
             update_finished_count += len(health_instances)
             logging.info(f"update finished: {update_finished_count}/{len(instance_ids)}")
@@ -224,12 +229,13 @@ async def update_instance(ecs_client, alb_client, instance_ids, max_workers, ver
             sys.exit()
 
 
-def remove_instances(ecs_client, alb_client, instance_ids):
+def remove_instances(ecs_client, alb_client, instance_ids, port):
     """
     停止并释放机器
     :param ecs_client:
     :param alb_client:
     :param instance_ids: instanceId type-list
+    :param port: 后端服务器使用的端口
     :return: None
     """
     # 1. 摘流量
@@ -238,7 +244,8 @@ def remove_instances(ecs_client, alb_client, instance_ids):
         alb_utils.update_server_group_servers_attribute(alb_client,
                                                    longvideoapi_config.server_group_id_list,
                                                    instance_ids,
-                                                   weight_list)
+                                                   weight_list,
+                                                   port)
     except Exception as e:
         logging.error(f"Failed to set instance weight: {e}")
         sys.exit()
@@ -248,7 +255,7 @@ def remove_instances(ecs_client, alb_client, instance_ids):
     for server_group_id in longvideoapi_config.server_group_id_list:
         try:
             alb_utils.remove_servers_from_server_group(alb_client=alb_client, server_group_id=server_group_id,
-                                                   instance_id=instance_ids)
+                                                   instance_id=instance_ids, port=port)
             logging.info(f"Successfully removed instances from server group {server_group_id}.")
         except Exception as e:
             logging.error(f"Failed to remove instances from server group {server_group_id}: {e}")
@@ -311,13 +318,13 @@ def main():
         ess_instance_count = online_instance_count // 2
         logging.info(f"ess instance count: {ess_instance_count}")
         asyncio.run(ess_instance(ecs_client=ecs_client, alb_client=alb_client,
-                                 ess_count=ess_instance_count, max_workers=2, version=version))
+                                 ess_count=ess_instance_count, max_workers=2, version=version, port=longvideoapi_config.port))
         logging.info(f"ess instances end!")
 
         # 3. 原有机器进行更新
         logging.info(f"update online instances start ...")
         asyncio.run(update_instance(ecs_client=ecs_client, alb_client=alb_client,
-                                    instance_ids=online_instance_ids, max_workers=8, version=version))
+                                    instance_ids=online_instance_ids, max_workers=8, version=version, port=longvideoapi_config.port))
         logging.info(f"update online instances end!")
 
         # 4. 停止并释放扩容机器