我一直试图构建一个简单的ECS Fargate基础设施,使用CDK来学习ECS及其组件。web服务器公开端口8081,服务SG入口规则允许来自ALB的所有TCP,ALB允许端口80上的连接-后来更改为所有TCP进行测试。ALB在端口80 (稍后在端口8081 )上有一个侦听器,它将流量转发给HTTP上的目标组:8081,fargate任务自动注册,健康检查正在通过。
一切似乎都是以正确的方式设置的,然而,当我去[alb-dns].com时,我什么也得不到--甚至504 -- DNS就是找不到。但是当我使用[alb-dns].com:8081时,它为我服务于when服务器上的“你好世界”。不管我的目标组是在HTTP:8081还是HTTP:80上,这都是有效的。
我尝试了一种旧的github/堆栈溢出解决方案,即将侦听器作为端口映射传递到容器,但这种方法不再起作用--类型错配。
我在这里错过了什么?
代码:
this.cluster = new Cluster(this, 'exanubes-cluster', {
vpc: props.vpc,
clusterName: 'exanubes-cluster',
containerInsights: true,
enableFargateCapacityProviders: true,
})
const albSg = new SecurityGroup(this, 'SecurityGroupLoadBalancer', {
vpc: props.vpc,
allowAllOutbound: true
})
albSg.addIngressRule(Peer.anyIpv4(), Port.allTcp())
const alb = new ApplicationLoadBalancer(this, 'alb', {
vpc: props.vpc,
loadBalancerName: 'exanubes-ecs-application-LB',
internetFacing: true,
securityGroup: albSg,
http2Enabled: false,
deletionProtection: false
})
const listener = alb.addListener('http listener', {
port: 80,
open: true
})
const targetGroup = listener.addTargets('tcp-listener-target', {
targetGroupName: 'tcp-target-ecs-service',
protocol: ApplicationProtocol.HTTP,
protocolVersion: ApplicationProtocolVersion.HTTP1,
port: CONTAINER_PORT
})
const taskDefinition = new FargateTaskDefinition(this, 'fargate-task-definition');
taskDefinition.addContainer('web-server', {
image: EcrImage.fromEcrRepository(props.repository),
}).addPortMappings({
containerPort: CONTAINER_PORT
})
const securityGroup = new SecurityGroup(this, 'http-sg', {
vpc: props.vpc,
})
securityGroup.addIngressRule(Peer.securityGroupId(albSg.securityGroupId), Port.allTcp(), 'Allow inbound connections from ALB')
const fargateService = new FargateService(this, 'fargate-service', {
cluster: this.cluster,
assignPublicIp: true,
taskDefinition,
capacityProviderStrategies: [
{
capacityProvider: "FARGATE_SPOT",
weight: 0,
},
{
capacityProvider: "FARGATE",
weight: 1
}
],
securityGroups: [securityGroup],
})
targetGroup.addTarget(fargateService)PS:我知道ApplicationLoadBalancedFargateService,但我想自己构建它。
发布于 2022-07-26 16:21:41
我认为源代码.../aws_cdk/aws_ecs/__init__.py中的这个示例应该会有所帮助。
Example::
# cluster: ecs.Cluster
# task_definition: ecs.TaskDefinition
# vpc: ec2.Vpc
service = ecs.FargateService(self, "Service", cluster=cluster, task_definition=task_definition)
lb = elbv2.ApplicationLoadBalancer(self, "LB", vpc=vpc, internet_facing=True)
listener = lb.add_listener("Listener", port=80)
service.register_load_balancer_targets(
container_name="web",
container_port=80,
new_target_group_id="ECS",
listener=ecs.ListenerConfig.application_listener(listener,
protocol=elbv2.ApplicationProtocol.HTTPS
)
)编辑:上面的内容对我没有用,但是这个对我有用。
listener.add_targets('tcp-target-group',
protocol=ApplicationProtocol.HTTP,
target_group_name="my-target",
targets=[service.load_balancer_target(
container_name=container.container_name,
container_port=CONTAINER_PORT
)],
)https://stackoverflow.com/questions/71311992
复制相似问题