繁体   English   中英

AWS websocket 容器 cdk ecs

[英]AWS websocket container cdk ecs

lass TestEcsConstruct extends core.Construct {
constructor(scope, id, props) {
    super(scope, id);

    const vpc = new ec2.Vpc(this, 'MyVpc', {
        enableDnsSupport: true,
        enableDnsHostnames: true,
        cidr: '10.0.0.0/16',
        maxAzs: 2,
        natGateways: 0,
        subnetConfiguration: [
            {
                cidrMask: 24,
                name: 'Public',
                subnetType: ec2.SubnetType.PUBLIC
            }]
    });
    const secGroup = new ec2.SecurityGroup(this, 'SecGroup',
        {
            vpc: vpc,
            description: 'Access to the ECS hosts that run containers',
            allowAllOutbound: true
        }
    );
    secGroup.connections.allowToAnyIpv4(new ec2.Port({
        protocol: ec2.Protocol.TCP,
        fromPort: 22,
        toPort: 22
    }), 'Ingress SSH from public sg');
    secGroup.connections.allowFromAnyIpv4(new ec2.Port({
        protocol: ec2.Protocol.TCP,
        fromPort: 22,
        toPort: 22
    }), 'Ingress SSH from public sg');
    secGroup.connections.allowToAnyIpv4(new ec2.Port({
        protocol: ec2.Protocol.TCP,
        fromPort: 8080,
        toPort: 8080
    }), 'Ingress Docker Port Open');
    secGroup.connections.allowFromAnyIpv4(new ec2.Port({
        protocol: ec2.Protocol.TCP,
        fromPort: 8080,
        toPort: 8080
    }), 'Ingress Docker Port Open');

    const cluster = new ecs.Cluster(this, 'Ec2Cluster', {
        vpc
    });
    const autoScaleGroup = cluster.addCapacity('DefaultAutoScalingGroup', {
        instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO),
        keyName: 'default-keypair',
        minCapacity: 1,
        maxCapacity: 2,
        desiredCapacity: 1,
        // resourceSignalTimeout: core.Duration.minutes(5),
        // rollingUpdateConfiguration: {
        //     minInstancesInService: 1,
        //     maxBatchSize: 1,
        //     pauseTime: core.Duration.minutes(5),
        //     waitOnResourceSignals: true,
        //     suspendProcesses: [
        //         'HealthCheck',
        //         'ReplaceUnhealthy',
        //         'AZRebalance',
        //         'AlarmNotification',
        //         'ScheduledActions'
        //     ]
        // }
    });
    autoScaleGroup.addSecurityGroup(secGroup);

    // create a task definition with CloudWatch Logs
    const logging = new ecs.AwsLogDriver({
        streamPrefix: "websocket-app",
        logRetention: 365
    })

    const image = ecs.ContainerImage.fromAsset(path.join(path.resolve(), 'test-docker'));
    const taskDefinition = new ecs.Ec2TaskDefinition(this, "MyTaskDefinition");
    const container = taskDefinition.addContainer("AppContainer", {
        image: image,
        cpu: 256,
        memoryLimitMiB: 1024,
        environment: [('REGION', process.env.REGION),
        ('QUEUE_URL', core.Fn.importValue(
            'NetworkStack:ApiMsgQueueUrl'
        ))],
        logging: logging
    });
    container.addPortMappings({
        containerPort: 8080,
        hostPort: 8080
    });

    // Instantiate ECS Service with just cluster and image
    new ecs.Ec2Service(this, "Ec2Service", {
        cluster,
        taskDefinition
    });
}

}

我有这段代码,似乎甚至无法在 ecs 容器的本地主机中联系这个端点。 这个例子只是为了展示一个可以从外部地址访问的开放端口,我真的很困惑我在这里遗漏了什么。 似乎在我连接的容器内部只有一个 ecs 实例 docker watcher,但没有其他 docker 实例在运行,我无法将 docker exec 放入确实存在的容器中。 请提供任何建议和/或帮助,我们将不胜感激。

这显然与内存为 1024 而不是 512 有关,在不相关的主题中我在相关代码段中配置我自己的 AutoScalingGroup 时使用了错误的图像。 因此,如果有人在这方面遇到问题,请确保您的 machineImage 是正确的类型。

//'.addCapacity' creates the ASG
const prerenderCluster = new ecs.Cluster(this, 'prerenderCluster',{
  vpc,
});

prerenderCluster.addCapacity('DefaultAutoScalingGroup', {
  instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.XLARGE),
  machineImage: new ecs.EcsOptimizedAmi(),

对于未来,这里有一个关于@thekevshow 正在谈论的关于正确 AMI 的示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM