You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS CDK部署ECS Service卡住,如何配置实现服务稳定?

ECS服务部署卡顿不稳定的解决方案

部署前端应用时,ECS Service创建环节持续卡住,执行cdk deploy verbose提示服务不稳定,核心原因是缺少健康探测机制——ECS无法确认容器是否正常就绪,负载均衡也无法验证后端服务可用性,最终导致部署流程超时或卡住。

需要添加的关键配置

要解决问题,需补充三类健康探测及部署优化配置:

  • 容器级健康检查:让ECS直接监控容器内部服务状态
  • 负载均衡目标组健康探测:让LB验证后端服务的可用性
  • ECS服务部署策略优化:降低部署超时概率

修改后的完整CDK代码

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
import * as iam from 'aws-cdk-lib/aws-iam';

export class WebAppStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Create VPC
    const vpc = new ec2.Vpc(this, 'WebAppVpc', {
      maxAzs: 2 // Default is all AZs in the region
    });

    // Create ECS Cluster
    const cluster = new ecs.Cluster(this, 'WebAppCluster', {
      vpc: vpc
    });

    // Add capacity to the ECS Cluster
    cluster.addCapacity('DefaultAutoScalingGroupCapacity', {
      instanceType: new ec2.InstanceType('t2.micro'),
      desiredCapacity: 2,
    });

    // Define Task Definition
    const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef');

    // Add container to Task Definition
    const container = taskDefinition.addContainer('web', {
      image: ecs.ContainerImage.fromAsset('../../'),
      memoryLimitMiB: 512,
      cpu: 256,
      // 添加容器健康检查:探测前端服务根路径
      healthCheck: {
        command: ['CMD-SHELL', 'curl -f http://localhost:80 || exit 1'],
        interval: cdk.Duration.seconds(30),
        timeout: cdk.Duration.seconds(5),
        retries: 3,
        startPeriod: cdk.Duration.seconds(60) // 给前端应用留足启动初始化时间
      }
    });

    container.addPortMappings({
      containerPort: 80,
      hostPort: 80,
      protocol: ecs.Protocol.TCP,
    });

    // Create ECS Service
    const service = new ecs.Ec2Service(this, 'Service', {
      cluster,
      taskDefinition,
      // 优化部署配置:避免因实例启动慢导致部署超时
      deploymentConfiguration: {
        maximumPercent: 200, // 允许同时运行最多2倍任务数
        minimumHealthyPercent: 50 // 确保至少50%任务保持健康
      }
    });

    // Create Load Balancer
    const lb = new elbv2.ApplicationLoadBalancer(this, 'LB', {
      vpc,
      internetFacing: true
    });

    const listener = lb.addListener('Listener', {
      port: 80,
    });

    // Attach ECS Service to Load Balancer
    listener.addTargets('ECS', {
      port: 80,
      targets: [service],
      // 添加负载均衡目标组健康探测
      healthCheck: {
        path: '/',
        port: '80',
        protocol: elbv2.Protocol.HTTP,
        interval: cdk.Duration.seconds(30),
        timeout: cdk.Duration.seconds(5),
        healthyThresholdCount: 2,
        unhealthyThresholdCount: 2
      }
    });

    // Output the Load Balancer DNS
    new cdk.CfnOutput(this, 'LoadBalancerDNS', {
      value: lb.loadBalancerDnsName,
    });
  }
}

配置说明

  1. 容器健康检查:通过curl访问容器内部80端口的根路径,确认前端服务启动完成。startPeriod设置为60秒,适配前端应用静态资源加载、初始化的耗时。
  2. 负载均衡健康探测:让LB定期检查后端服务状态,只有通过探测的任务才会被分配流量,避免将请求转发到未就绪的服务。
  3. 部署配置优化:调整maximumPercent和minimumHealthyPercent,允许部署时启动新任务的同时保留部分旧任务,既保证服务连续性,也降低因新任务启动慢导致的部署超时概率。

内容的提问来源于stack exchange,提问作者Mahmoud Saad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 22:14:58