elb-cmd¶
nlb and target group¶
创建nlb和tg,端口监听80
uniqstr=$(date +%Y%m%d%H%M)
port1=80
export AWS_DEFAULT_REGION=us-east-2
#VPC_ID=vpc-000a5xxxx5a67b03b
VPC_ID=$(aws ec2 describe-vpcs \
--filter Name=is-default,Values=true \
--query 'Vpcs[0].VpcId' --output text \
--region ${AWS_DEFAULT_REGION})
FIRST_SUBNET=$(aws ec2 describe-subnets \
--filters "Name=vpc-id,Values=${VPC_ID}" \
--query "Subnets[?AvailabilityZone=='"${AWS_DEFAULT_REGION}a"'].SubnetId" \
--output text \
--region ${AWS_DEFAULT_REGION})
aws elbv2 create-load-balancer \
--name nlb1-${uniqstr} \
--type network \
--scheme internet-facing \
--subnets ${FIRST_SUBNET} |tee /tmp/$$.1
nlb1_arn=$(cat /tmp/$$.1 |jq -r '.LoadBalancers[0].LoadBalancerArn')
aws elbv2 create-target-group \
--name nlb1-tg-${port1}-${uniqstr} \
--protocol TCP \
--port ${port1} \
--vpc-id ${VPC_ID} |tee /tmp/$$.2
tg1_arn=$(cat /tmp/$$.2 |jq -r '.TargetGroups[0].TargetGroupArn')
aws elbv2 create-listener --load-balancer-arn ${nlb1_arn} \
--protocol TCP --port ${port1} \
--default-actions Type=forward,TargetGroupArn=${tg1_arn}
func-alb-and-tg-¶
func-elb-and-tg | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
alb and target group (sample)¶
创建alb和tg,端口监听80
uniqstr=$(TZ=EAT-8 date +%Y%m%d-%H%M)
port1=80
export AWS_DEFAULT_REGION=us-east-2
#VPC_ID=vpc-xxx
VPC_ID=$(aws ec2 describe-vpcs \
--filter Name=is-default,Values=true \
--query 'Vpcs[0].VpcId' --output text \
--region ${AWS_DEFAULT_REGION})
FIRST_SUBNET=$(aws ec2 describe-subnets \
--filters "Name=vpc-id,Values=${VPC_ID}" \
--query "Subnets[?AvailabilityZone=='"${AWS_DEFAULT_REGION}a"'].SubnetId" \
--output text \
--region ${AWS_DEFAULT_REGION})
SECOND_SUBNET=$(aws ec2 describe-subnets \
--filters "Name=vpc-id,Values=${VPC_ID}" \
--query "Subnets[?AvailabilityZone=='"${AWS_DEFAULT_REGION}b"'].SubnetId" \
--output text \
--region ${AWS_DEFAULT_REGION})
DEFAULT_SG_ID=$(aws ec2 describe-security-groups \
--filter Name=vpc-id,Values=${VPC_ID} \
--query "SecurityGroups[?GroupName == 'default'].GroupId" \
--output text \
--region ${AWS_DEFAULT_REGION})
aws elbv2 create-load-balancer --name alb1-${uniqstr} \
--subnets ${FIRST_SUBNET} ${SECOND_SUBNET} \
--security-groups ${DEFAULT_SG_ID} |tee /tmp/$$.1
alb1_arn=$(cat /tmp/$$.1 |jq -r '.LoadBalancers[0].LoadBalancerArn')
alb1_dnsname=$(cat /tmp/$$.1 |jq -r '.LoadBalancers[0].DNSName')
aws elbv2 create-target-group \
--name alb1-tg-${port1}-${uniqstr} \
--protocol HTTP \
--port ${port1} \
--target-type ip \
--vpc-id ${VPC_ID} |tee /tmp/$$.2
tg1_arn=$(cat /tmp/$$.2 |jq -r '.TargetGroups[0].TargetGroupArn')
aws elbv2 create-listener --load-balancer-arn ${alb1_arn} \
--protocol HTTP --port ${port1} \
--default-actions Type=forward,TargetGroupArn=${tg1_arn}
example¶
create tg and listener¶
NLB_NAME=(nlb1 nlb2)
for i in ${NLB_NAME[@]}; do
aws elbv2 create-target-group \
--name ${i}-tg-80 \
--protocol TCP \
--port 80 \
--vpc-id ${VPC_ID}
aws elbv2 create-target-group \
--name ${i}-tg-81 \
--protocol TCP \
--port 81 \
--vpc-id ${VPC_ID}
done
for i in `seq 80 99`; do
aws elbv2 create-target-group \
--name tg-tcp-${i} \
--protocol TCP \
--port ${i} \
--vpc-id ${VPC_ID}
done
NLB_ARN=arn:aws:elasticloadbalancing:us-east-1:xxx:loadbalancer/net/nlb/xxx
for i in `seq 80 99`; do
TG_ARN=$(aws elbv2 describe-target-groups --query 'TargetGroups[?TargetGroupName==`tg-tcp-'"${i}"'`].TargetGroupArn' --output text)
aws elbv2 create-listener \
--load-balancer-arn ${NLB_ARN} \
--protocol TCP \
--port ${i} \
--default-actions Type=forward,TargetGroupArn=${TG_ARN}
done
create alb-type target group¶
refer¶
- https://cloudaffaire.com/network-load-balancer-target-group-health-checks/
- icon: simple/awselasticloadbalancing