如何为CDK中的两个不同端口配置ELB?
How to configure ELB for two different ports in CDK?
我有一个弹性负载均衡器为 EC2 实例提供流量。我在端口 443 上有一个应用程序 运行ning,它 运行 很好。
现在我想 运行 EC2 实例上端口 444 上的另一个应用程序。我希望能够 运行 第一个应用程序通过访问端口 443 和第二个应用程序通过访问端口 444 .
不知怎么的,我无法将端口 444 添加到 CDK 中的负载平衡器。我正在做这样的事情。
const appLoadbalancer = new elbv2.ApplicationLoadBalancer(this, `${props.type}AppLoadBalancer`, {
vpc: vpc,
vpcSubnets: subnets,
internetFacing: true,
});
const httpsListener = appLoadbalancer.addListener(`${props.type}HTTPSListener`, {
port: 443,
open: true,
certificates: [props.certificate]
});
httpsListener.addTargets(`${props.type}HTTPSTarget`, {
port: 443,
targets: [autoscalingGroup],
healthCheck: {
enabled: true,
healthyHttpCodes: "200,302"
}
});
const httpsListener2 = appLoadbalancer.addListener(`${props.type}HTTPSListener2`, {
port: 444,
protocol: elbv2.ApplicationProtocol.HTTPS,
open: true,
certificates: [props.certificate]
});
httpsListener2.addTargets(`${props.type}HTTPSTarget2`, {
port: 444,
protocol: elbv2.ApplicationProtocol.HTTPS,
targets: [autoscalingGroup],
healthCheck: {
enabled: true,
healthyHttpCodes: "200,302"
}
});
如果仅针对端口 443 进行设置,则一切正常。但是当我尝试上述操作时,我得到类似的信息:
Error: Cannot add AutoScalingGroup to 2nd Target Group
我不知道这是什么意思以及如何在 cdk 中修复它...
我最终得到了这样的解决方案:
const appLoadbalancer = new elbv2.ApplicationLoadBalancer(this, `${props.type}AppLoadBalancer`, {
vpc: vpc,
vpcSubnets: subnet,
internetFacing: true,
});
const tg1 = new elbv2.ApplicationTargetGroup(this, "tg1", {
vpc: vpc,
protocol: elbv2.ApplicationProtocol.HTTPS})
const tg2 = new elbv2.ApplicationTargetGroup(this, "tg2", {vpc: vpc,
protocol: elbv2.ApplicationProtocol.HTTPS, port: 444})
const httpsListener = appLoadbalancer.addListener(`${props.type}HTTPSListener`, {
port: 443,
protocol: elbv2.ApplicationProtocol.HTTPS,
open: true,
certificates: [props.certificate]
});
httpsListener.addTargetGroups("RestTarget", {
targetGroups: [tg1]
});
const httpsListener2 = appLoadbalancer.addListener(`${props.type}HTTPSListener2`, {
port: 444,
protocol: elbv2.ApplicationProtocol.HTTPS,
open: true,
certificates: [props.certificate]
});
httpsListener2.addTargetGroups("RestTarget", {
targetGroups: [tg2]
});
const ServiceAsg = autoscalingGroup.node.defaultChild as autoscaling.CfnAutoScalingGroup
ServiceAsg.targetGroupArns = [tg1.targetGroupArn, tg2.targetGroupArn]
我有一个弹性负载均衡器为 EC2 实例提供流量。我在端口 443 上有一个应用程序 运行ning,它 运行 很好。
现在我想 运行 EC2 实例上端口 444 上的另一个应用程序。我希望能够 运行 第一个应用程序通过访问端口 443 和第二个应用程序通过访问端口 444 .
不知怎么的,我无法将端口 444 添加到 CDK 中的负载平衡器。我正在做这样的事情。
const appLoadbalancer = new elbv2.ApplicationLoadBalancer(this, `${props.type}AppLoadBalancer`, {
vpc: vpc,
vpcSubnets: subnets,
internetFacing: true,
});
const httpsListener = appLoadbalancer.addListener(`${props.type}HTTPSListener`, {
port: 443,
open: true,
certificates: [props.certificate]
});
httpsListener.addTargets(`${props.type}HTTPSTarget`, {
port: 443,
targets: [autoscalingGroup],
healthCheck: {
enabled: true,
healthyHttpCodes: "200,302"
}
});
const httpsListener2 = appLoadbalancer.addListener(`${props.type}HTTPSListener2`, {
port: 444,
protocol: elbv2.ApplicationProtocol.HTTPS,
open: true,
certificates: [props.certificate]
});
httpsListener2.addTargets(`${props.type}HTTPSTarget2`, {
port: 444,
protocol: elbv2.ApplicationProtocol.HTTPS,
targets: [autoscalingGroup],
healthCheck: {
enabled: true,
healthyHttpCodes: "200,302"
}
});
如果仅针对端口 443 进行设置,则一切正常。但是当我尝试上述操作时,我得到类似的信息:
Error: Cannot add AutoScalingGroup to 2nd Target Group
我不知道这是什么意思以及如何在 cdk 中修复它...
我最终得到了这样的解决方案:
const appLoadbalancer = new elbv2.ApplicationLoadBalancer(this, `${props.type}AppLoadBalancer`, {
vpc: vpc,
vpcSubnets: subnet,
internetFacing: true,
});
const tg1 = new elbv2.ApplicationTargetGroup(this, "tg1", {
vpc: vpc,
protocol: elbv2.ApplicationProtocol.HTTPS})
const tg2 = new elbv2.ApplicationTargetGroup(this, "tg2", {vpc: vpc,
protocol: elbv2.ApplicationProtocol.HTTPS, port: 444})
const httpsListener = appLoadbalancer.addListener(`${props.type}HTTPSListener`, {
port: 443,
protocol: elbv2.ApplicationProtocol.HTTPS,
open: true,
certificates: [props.certificate]
});
httpsListener.addTargetGroups("RestTarget", {
targetGroups: [tg1]
});
const httpsListener2 = appLoadbalancer.addListener(`${props.type}HTTPSListener2`, {
port: 444,
protocol: elbv2.ApplicationProtocol.HTTPS,
open: true,
certificates: [props.certificate]
});
httpsListener2.addTargetGroups("RestTarget", {
targetGroups: [tg2]
});
const ServiceAsg = autoscalingGroup.node.defaultChild as autoscaling.CfnAutoScalingGroup
ServiceAsg.targetGroupArns = [tg1.targetGroupArn, tg2.targetGroupArn]