gpt4 book ai didi

aws-fargate - 使用 CDK 使用正确的端口注册 Fargate 服务 ELB 目标?

转载 作者:行者123 更新时间:2023-12-05 03:56:21 27 4
gpt4 key购买 nike

我正在尝试将 Fargate 服务添加为 Application Load Balancer 目标,但它总是获取错误的容器端口。任务定义有两个容器:端口 8080 上的应用程序和端口 443 上的 nginx 反向代理。当我尝试通过 CDK 将它们连接在一起时,目标注册总是获得端口 8080。我似乎找不到方法或设置让我告诉 CDK 使用哪个容器端口的 Prop 。或者也许我是,它忽略了它?我错过了什么?

这是一个精简的示例构造:

export class CdkFargateElbStack extends cdk.Stack {

constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const vpc = new ec2.Vpc(this, 'Vpc', { maxAzs: 2 });

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

const taskDef = new FargateTaskDefinition(this, 'TaskDefinition');

const appContainer = new ContainerDefinition(this, 'AppContainer', {
image: ContainerImage.fromRegistry(APP_IMAGE),
taskDefinition: taskDef,
});
appContainer.addPortMappings({
hostPort: 8080,
containerPort: 8080
});
const proxyContainer = new ContainerDefinition(this, 'ProxyContainer', {
image: ContainerImage.fromRegistry(PROXY_IMAGE),
taskDefinition: taskDef,
})
proxyContainer.addPortMappings({
hostPort: 443,
containerPort: 443,
});

const service = new FargateService(this, 'Service', {
cluster: cluster,
taskDefinition: taskDef,
assignPublicIp: true,
desiredCount: 1,
vpcSubnets: vpc.selectSubnets({
subnetType: ec2.SubnetType.PUBLIC,
}),
});

const alb = new elb.ApplicationLoadBalancer(this, 'LoadBalancer', {
vpc: vpc,
internetFacing: true,
ipAddressType: elb.IpAddressType.IPV4,
vpcSubnets: vpc.selectSubnets({
subnetType: ec2.SubnetType.PUBLIC,
})
});

const tg = new ApplicationTargetGroup(this, 'TargetGroup', {
protocol: elb.ApplicationProtocol.HTTPS,
port: 443,
vpc: vpc,
targetType: elb.TargetType.IP,
targets: [ service ],
});

const listener = alb.addListener('Listener', {
protocol: elb.ApplicationProtocol.HTTPS,
port: 443,
certificateArns: [ CERTIFICATE_ARN ],
defaultTargetGroups: [tg]
});

const rule = new ApplicationListenerRule(this, 'rule', {
listener,
priority: 1,
pathPattern: '*',
targetGroups: [ tg ],
});
}
}

这是生成的目标注册。我需要这里的端口是 443。 target registrations screenshot

最佳答案

根据 ecs.FargateService.loadBalancerTarget 的文档,将被选为 ALB 目标的容器是任务定义中的第一个基本容器。

要使用其他容器,请像这样创建服务引用:

const sTarget = service.loadBalancerTarget({
containerName: 'MyContainer',
containerPort: 1234
}));

然后将sTarget添加到targetGroup服务。

关于aws-fargate - 使用 CDK 使用正确的端口注册 Fargate 服务 ELB 目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59397676/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com