aws::elasticloadbalancingv2的cdk构造库
aws-cdk.aws-elasticloadbalancingv2的Python项目详细描述
Amazon Elastic负载平衡v2构造库
@aws-cdk/aws-elasticloadbalancingv2
包为
配置应用程序和网络负载平衡器。
有关详细信息,请参见 Application Load Balancers 以及Network Load Balancers。
定义应用程序负载均衡器
通过创建
ApplicationLoadBalancer
,向负载平衡器添加侦听器
并向侦听器添加目标:
importec2=require('@aws-cdk/aws-ec2');importelbv2=require('@aws-cdk/aws-elasticloadbalancingv2');importautoscaling=require('@aws-cdk/aws-autoscaling');// ...constvpc=newec2.Vpc(...);// Create the load balancer in a VPC. 'internetFacing' is 'false'// by default, which creates an internal load balancer.constlb=newelbv2.ApplicationLoadBalancer(this,'LB',{vpc,internetFacing: true});// Add a listener and open up the load balancer's security group// to the world. 'open' is the default, set this to 'false'// and use `listener.connections` if you want to be selective// about who can access the listener.constlistener=lb.addListener('Listener',{port: 80,open: true,});// Create an AutoScaling group and add it as a load balancing// target to the listener.constasg=newautoscaling.AutoScalingGroup(...);listener.addTargets('ApplicationFleet',{port: 8080,targets:[asg]});
负载平衡器和目标的安全组是自动的 已更新以允许网络流量。
使用addFixedResponse()
方法在侦听器上添加固定的响应规则:
listener.addFixedResponse('Fixed',{pathPattern:'/ok',contentType: elbv2.ContentType.TEXT_PLAIN,messageBody:'OK',statusCode:'200'});
条件
根据来袭目标的情况,可以将交通路线定为目标
http请求。支持基于路径和主机的条件。例如,
下面将请求路由到指定的自动缩放组
仅当请求中请求的主机是example.com
:
listener.addTargets('Example.Com Fleet',{priority: 10,hostHeader:'example.com',port: 8080,targets:[asg]});
priority
是添加带条件的目标时的必需字段。最低的
赢了多少。
每个侦听器必须至少有一个无条件的目标。
定义网络负载均衡器
网络负载平衡器的定义方式与应用程序负载类似 平衡器:
importec2=require('@aws-cdk/aws-ec2');importelbv2=require('@aws-cdk/aws-elasticloadbalancingv2');importautoscaling=require('@aws-cdk/aws-autoscaling');// Create the load balancer in a VPC. 'internetFacing' is 'false'// by default, which creates an internal load balancer.constlb=newelbv2.NetworkLoadBalancer(this,'LB',{vpc,internetFacing: true});// Add a listener on a particular port.constlistener=lb.addListener('Listener',{port: 443,});// Add targets on a particular port.listener.addTargets('AppFleet',{port: 443,targets:[asg]});
要记住的一点是,网络负载平衡器没有安全性 组,并且没有为您完成自动安全组配置。你会 必须自己配置目标的安全组以允许通过 客户机和/或负载平衡器实例,具体取决于目标类型。见 Target Groups for your Network Load Balancers 和Register targets with your Target Group 欲了解更多信息。
目标和目标群体
应用程序和网络负载平衡器在目标中组织负载平衡目标
组。如果添加平衡目标(如自动缩放组、ECS
服务或单个实例)直接发送给侦听器
TargetGroup
将自动为您创建。
如果需要对创建的目标组进行更多控制,请创建
ApplicationTargetGroup
或NetworkTargetGroup
,添加所需的成员,
并通过调用addTargetGroups
而不是addTargets
将其添加到侦听器中。
addTargets()
将始终返回刚刚为您创建的目标组:
constgroup=listener.addTargets('AppFleet',{port: 443,targets:[asg1],});group.addTarget(asg2);
使用lambda目标
要使用lambda函数作为目标,请使用
@aws-cdk/aws-elasticloadbalancingv2-targets
包装:
importlambda=require('@aws-cdk/aws-lambda');importelbv2=require('@aws-cdk/aws-elasticloadbalancingv2');importtargets=require('@aws-cdk/aws-elasticloadbalancingv2-targets');constlambdaFunction=newlambda.Function(...);constloadBalancer=newelbv2.ApplicationLoadBalancer(...);constlistener=lb.addListener('Listener',{port: 80});listener.addTargets('Targets',{targets:[newtargets.LambdaTarget(lambdaFunction)]});
只能将单个lambda函数添加到单个侦听器规则。
配置运行状况检查
在创建目标组时配置运行状况检查:
listener.addTargets('AppFleet',{port: 8080,targets:[asg],healthCheck:{path:'/ping',interval: cdk.Duration.minutes(1),}});
健康检查也可以在创建后通过调用
configureHealthCheck()
在创建的对象上。
没有尝试为您的端口配置安全组 为配置运行状况检查,但如果运行状况检查位于同一端口上 您正在将流量路由到,安全组已允许该流量。 否则,您必须正确配置安全组:
listener.addTargets('AppFleet',{port: 8080,targets:[asg],healthCheck:{port: 8088,}});listener.connections.allowFrom(lb,ec2.Port.tcp(8088));
负载平衡器目标协议
要成为负载平衡器目标的结构应该实现
IApplicationLoadBalancerTarget
和/或INetworkLoadBalancerTarget
,以及
提供函数attachToXxxTargetGroup()
的实现,它可以
调用负载平衡器上的函数,并应返回有关
负载平衡目标:
publicattachToApplicationTargetGroup(targetGroup: ApplicationTargetGroup):LoadBalancerTargetProps{targetGroup.registerConnectable(...);return{targetType: TargetType.Instance|TargetType.IptargetJson:{id: ...,port: ...},};}
targetType
应该是Instance
或Ip
之一。如果目标可以
直接添加到目标组,targetJson
应该包含
目标(实例ID或IP地址取决于类型)和
可选的port
或availabilityZone
重写。
应用程序负载平衡器目标可以调用 目标组注册以添加负载平衡器的安全性 组规则。
如果负载平衡器目标要求目标组
在注册之前与loadbalancer关联(例如
ECS S外壳以服务为例),以
targetGroup.loadBalancerDependency()
如下:
// Make sure that the listener has been created, and so the TargetGroup// has been associated with the LoadBalancer, before 'resource' is created.resourced.addDependency(targetGroup.loadBalancerDependency());