具有一个所需实例的AWS Auto-Scaling组,没有负载均衡器

问题描述 投票:0回答:1

正在运行使用CloudFormation设计的系统,该系统最近经历了一系列不幸的硬件故障。为了使该不经常使用的系统具有成本效益,它仅由一个实例支持,并且不使用负载平衡器。我想向其添加一个自动扩展组,以确保至少不需要手动干预即可对以后的类似故障做出反应。但是,由于该系统是可公开访问的,因此该实例需要具有关联的弹性IP,这就是我一直在努力的地方。我的想法是,IP将在启动后简单地与新实例重新关联。无需关心用户会话等。

据我所知,不可能将单个弹性IP配置为在自动伸缩组中重复使用(据我所知,我知道这是一个利基用例)。因此,我最终尝试在CloudFormation :: Init期间重新关联IP。有没有更优雅的解决方案?

amazon-web-services aws-auto-scaling
1个回答
0
投票

正如您所指出的,可以将用户数据脚本用于启动配置,以便在Auto Scaling启动新实例时,该脚本重新附加弹性IP地址或更新Route 53 DNS名称。

要附加弹性IP地址,脚本将需要获取其自己的实例ID,然后运行附加命令:

#!/bin/bash
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id/)

aws ec2 associate-address --allocation-id xxx --instance-id $INSTANCE_ID

或者,您可以将DNS名称与实例相关联,并在实例启动时更新DNS名称以指向实例。

来自Amazon Route 53: How to automatically update IP addresses without using Elastic IPs - DEV Community

#!/bin/bash
# Extract information about the Instance
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id/)
AZ=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone/)
MY_IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4/)

# Extract tags associated with instance
ZONE_TAG=$(aws ec2 describe-tags --region ${AZ::-1} --filters "Name=resource-id,Values=${INSTANCE_ID}" --query 'Tags[?Key==`AUTO_DNS_ZONE`].Value' --output text)
NAME_TAG=$(aws ec2 describe-tags --region ${AZ::-1} --filters "Name=resource-id,Values=${INSTANCE_ID}" --query 'Tags[?Key==`AUTO_DNS_NAME`].Value' --output text)

# Update Route 53 Record Set based on the Name tag to the current Public IP address of the Instance
aws route53 change-resource-record-sets --hosted-zone-id $ZONE_TAG --change-batch '{"Changes":[{"Action":"UPSERT","ResourceRecordSet":{"Name":"'$NAME_TAG'","Type":"A","TTL":300,"ResourceRecords":[{"Value":"'$MY_IP'"}]}}]}'

没有一种“优雅”的方法可以为Auto Scaling组自动执行这种类型的操作。

© www.soinside.com 2019 - 2024. All rights reserved.