如果您选择使用AWS Network Load Balancer(NLB)作为负载均衡器,并且您的负载均衡器似乎无法正常工作,您可以采取以下措施:
确保您的EC2实例安全组已配置以允许流量通过从NLB到实例的端口。您可以参考以下的安全组规则,假设您的实例在私有网络中:
确保您的NLB目标组所设置的协议与实际运行的协议一致。例如,如果您的目标组协议为TCP,则您的后端EC2实例必须在该端口上运行TCP服务。
在NLB目标组中,确保'注册目标”操作设置了正确的协议和端口。如果您的实例在后端使用了代理协议,例如HTTP,则此设置也需要正确地将请求转发到实例的协议和端口。
以下是一个Python Boto3示例,用于创建NLB和目标组:
import boto3
region_name = 'us-west-2'
# Create a client to interact with the AWS NLB service
client = boto3.client('elbv2', region_name=region_name)
# Create the NLB with required listener configs
response = client.create_load_balancer(
Name='my-nlb',
Subnets=[
'subnet-12345678',
'subnet-87654321'
],
Scheme='internet-facing',
Type='network',
IpAddressType='ipv4',
LoadBalancerAttributes={
'deletion_protection.enabled': 'false' # set to true if you want deletion protection
}
)
load_balancer_arn = response['LoadBalancers'][0]['LoadBalancerArn']
# Create the target group with required backend configs
response = client.create_target_group(
Name='my-nlb-targets',
Protocol='TCP',
Port=80,
VpcId='vpc-12345678',
TargetType='instance'