我有2个堆栈,第一个定义我的应用程序LB,另一个定义我的ECS服务。
在第一个堆栈中,我为我的多个ECS服务定义了多个目标组,并希望注册其相应的服务。
作为参考,我遵循了这个示例here to split my app up
如何将ECS服务注册到另一个堆栈中定义的目标组?
我的ECS服务...
const service = new ecs.Ec2Service(this, 'jenkinsService', {
cluster: props.cluster,
taskDefinition,
serviceName: 'Jenkins-Master',
minHealthyPercent: 0,
maxHealthyPercent: 100
});
props.targetGroup.addTarget(service) // I want to register against a specific Target Group我的自定义目标组
const jenkinsMastertargetGroup = this.targetGroup = new elbv2.ApplicationTargetGroup(this, 'Jenkins-Master', {
vpc: props.vpc,
port: 80,
targetType: elbv2.TargetType.INSTANCE,
targetGroupName: 'WDD-Jenkins-Master',
deregistrationDelay: cdk.Duration.seconds(250),
healthCheck:{
port: '8080',
path:'/log/all',
interval: cdk.Duration.minutes(3)
}
});我的侦听器上转发到TG的操作
elbHTTPlistener.addAction('JenkinsMasterResponse', {
priority: 5,
conditions: [
//ListenerCondition.hostHeaders(['sub1.test.com.au/jenkins']),
ListenerCondition.pathPatterns(['/jenkins']),
],
action: ListenerAction.forward([jenkinsMastertargetGroup]),
});发布于 2020-09-05 19:30:07
我的用例是使用1个应用程序负载均衡器(和1个监听器HTTPS),并且具有指向不同ECS部署的不同路径
例如:
而且我的配置方式和你的不一样。我不是在侦听器堆栈中设置所有目标组,而是在ECS堆栈上定义这些目标组。
StackLoadBalancer
const lb = new elbv2.ApplicationLoadBalancer(this, 'LB', {
vpc,
internetFacing: true
});
// make sure allow Application Load Balancer can accessing out to reach private service
lb.connections.securityGroups[0].addEgressRule(
ec2.Peer.ipv4(vpc.vpcCidrBlock),
ec2.Port.allTcp(),
"Allow Local Balancer to access service");
lb.logAccessLogs(logBucket)
const defaultTargetGroup = new elbv2.ApplicationTargetGroup(this, 'TargetGroup', {
vpc: vpc,
port: 80
});
const listener = lb.addListener('Listener', {
port: 443,
certificates: [certification],
defaultTargetGroups: [defaultTargetGroup],
open: true
});
new cdk.CfnOutput(this, 'listenerarn', { value: listener.listenerArn, exportName: this.stackName + "-listenerarn" })StackA
const ecsService = new ecs.Ec2Service(this, 'Service', {
cluster,
taskDefinition
});
ecsService.connections.securityGroups[0].addIngressRule(
ec2.Peer.ipv4(vpc.vpcCidrBlock),
ec2.Port.allTcp(),
"Allow CloudFormation ECS Service Update Properly"
);
const targetGroup = new elbv2.ApplicationTargetGroup(this, 'application-a-tg', {
vpc,
port: portNumber,
protocol: elbv2.ApplicationProtocol.HTTP,
healthCheck: {
interval: cdk.Duration.seconds(60),
path: "/",
timeout: cdk.Duration.seconds(5),
healthyHttpCodes: "200-499"
},
stickinessCookieDuration: cdk.Duration.hours(1),
deregistrationDelay: cdk.Duration.seconds(30),
targets: [
ecsService.loadBalancerTarget({
containerName: 'web'
})]
});
const rule1 = new elbv2.CfnListenerRule(this, "rule1", {
listenerArn: cdk.Fn.importValue("StackLoadBalancerStack-listenerarn"),
actions: [{
type: "forward",
targetGroupArn: targetGroup.targetGroupArn
}],
conditions: [{
field: "path-pattern",
pathPatternConfig: {
values: ["/application-a/*"]
},
}],
priority: 10
});StackA中最重要的部分是CfnListenerRule,在我开发的时候,我无法找到一个“原生”的CDK代码来做这件事。
发布于 2020-11-16 08:58:55
对于这个问题,我找到了一个相对简单的解决方案,即在cloudformation中使用导入/导出。
在我定义应用程序ELB的第一个堆栈中,我定义了一个空的目标组,然后导出该目标组的ARN。
确保为您的TG添加规则。
const emptytg = new elbv2.ApplicationTargetGroup(this, 'tg1', {
vpc,
port: 80,
targetGroupName: 'name',
targetType: elbv2.TargetType.INSTANCE,
});
Listener.addAction('tg1', {
priority: 2,
conditions:[
ListenerCondition.pathPatterns(['/tg1']),
],
action: ListenerAction.forward([tg1])
});
new cdk.CfnOutput(this,'tg1Export', {
value: emptytg.targetGroupArn,
exportName: 'emptytgARN'
});下面是包含ECS服务的另一个堆栈,您可以导入TG,然后使用.addTarget目标方法。
const importedGroup = elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(this, 'imported-tg',{
targetGroupArn: cdk.Fn.importValue('wddSharedResourcesjenkinsMastertargetGrouptargetGroupArn'),
});
importedGroup.addTarget(service); 这对我来说工作得很好,我希望它能在未来帮助其他人。
https://stackoverflow.com/questions/63724882
复制相似问题