在AWS中,可以使用健康检查来排除目标组中不健康的注册目标。下面是一个使用AWS CLI和Python SDK的解决方法,包含了代码示例。
解决方法1:使用AWS CLI
aws elbv2 create-target-group --name my-target-group --protocol HTTP --port 80 --target-type instance
aws elbv2 create-health-check --cli-input-json file://healthcheck.json
其中,healthcheck.json文件的内容如下:
{
"HealthCheckProtocol": "HTTP",
"HealthCheckPort": "80",
"HealthCheckPath": "/",
"HealthyThresholdCount": 3,
"UnhealthyThresholdCount": 3,
"IntervalSeconds": 30,
"TimeoutSeconds": 5
}
aws elbv2 register-targets --target-group-arn --targets Id=
aws elbv2 describe-target-health --target-group-arn
解决方法2:使用Python SDK(boto3)
import boto3
elbv2_client = boto3.client('elbv2')
# 创建健康检查
response = elbv2_client.create_target_group(
Name='my-target-group',
Protocol='HTTP',
Port=80,
TargetType='instance'
)
response = elbv2_client.create_health_check(
HealthCheckProtocol='HTTP',
HealthCheckPort='80',
HealthCheckPath='/',
HealthyThresholdCount=3,
UnhealthyThresholdCount=3,
IntervalSeconds=30,
TimeoutSeconds=5,
TargetGroupArn=
)
# 注册目标
response = elbv2_client.register_targets(
TargetGroupArn=,
Targets=[
{
'Id': ,
},
]
)
# 检查目标状态
response = elbv2_client.describe_target_health(
TargetGroupArn=
)
以上是一个基本的解决方法,可以根据实际需求进行调整和扩展。