发布
社区首页 >问答首页 >识别ECS实例的硬编码DNS记录

识别ECS实例的硬编码DNS记录
EN

Stack Overflow用户
提问于 2021-07-20 20:11:12
回答 1查看 136关注 0票数 0

我有一个码头映像,我需要部署,同时保持一个一致的DNS记录。最重要的是,我希望有一个硬编码的实例数(例如,2个)。我喜欢ECS的易用性以及ECS代理维护正常运行时间,但是负载均衡器对于单个实例来说是过分的。到目前为止,我只是创建了一个ASG,其最小/max/期望为1,因此没有任何冲突的问题。

对于自动标度组中的实例编号,有一个非常强烈的论据(这里就是一个例子),这是有意义的,尽管我认为这是一个无法避免的非常可靠的用例。

我已经成功地修改了融水/地形-aws dns-处理程序,为ASG中的单个ECS实例提供了硬编码的DNS记录(比如instance-1.example.com),尽管它不是很漂亮。为了让两个实例(instance-1.example.cominstance-2.example.com)工作,我想我有两个选择:

  • 创建一个hacky解决方案来读取我的Lambda函数中现有的Route53记录,以“决定性地”决定在ASG生命周期挂钩中分配我的实例的索引(可能是一个好的开始)
  • 创建两个ECS集群,每个集群具有一个实例,并使用两个单独的Lambda函数

对我来说,这两个选项都不是很好的选择,所以我想知道是否有更好的方法来以类似ECS的方式部署一个Docker容器,我可以将DNS记录附加到上面。

对于上下文来说,“两个ECS集群”方法可以很好地工作,但是我要这样做三到四次,所以我将在其他几个集群的基础上得到8个ECS集群,这至少是不方便的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-21 15:15:37

如果您使用自动缩放组和启动Config或模板与用户数据.这是我在用户数据中添加的内容,可以在任何时候使用计数器自动保持Route53 DNS记录的最新更新。希望这能帮上忙!

代码语言:javascript
代码运行次数:0
复制
## --------------------------------------------------------------------------------------------------------------- ##
## --- AUTO SCALING LOGIC for user-data -------------------------------------------------------------------------- ##
## --------------------------------------------------------------------------------------------------------------- ##
## --- Get ip addresses of existing app DNS Entries (that point to app nodes, not load balancer) ----- ##
## --- Get this ip address --------------------------------------------------------------------------------------- ##
## --- If this IP Address matches any in existing list, do not create a new DNS record --------------------------- ##
## --- If this IP Address does not match any in existing list, create a new DNS record --------------------------- ##
## --------------------------------------------------------------------------------------------------------------- ##

region="us-east-1"
hosted_zone="ROUTE53-HOSTED-ZONE-ID"
vpc="vpc-ID"
r53_domain="example.com"
asg_desired="2"
application="app"

echo "USING EC2 INSTANCE META DATA TO OBTAIN IP ADDRESS ..."
echo "IF YOU ARE NOT USING DEFAULT AMZN INSTANCE, USE ifconfig OR ANOTHER METHOD TO OBTAIN THIS INSTANCE IP ..."

# CHOOSE THE PRIVATE (local-ipv4) OR PUBLIC IP DEPENDING ON USE CASE 
#this_ip=`curl http://169.254.169.254/latest/meta-data/local-ipv4`
this_ip=`curl http://169.254.169.254/latest/meta-data/public-ipv4`
echo $this_ip > /tmp/this-ip.txt

# FILTER BASED ON UNIQUE TAGS FOR YOUR TARGETED INSTANCE(S)
# THIS IS USING Application Tag. 
aws --region $region ec2 describe-instances --query "Reservations[*].Instances[? Tags[? (Key=='Application') && Value=='$application']].PublicIpAddress" --output text >> /tmp/existing-ec2-ips.txt

counter=1
until [ $counter -gt $asg_desired ]
do
  aws --region $region route53 list-resource-record-sets --hosted-zone-id $hosted_zone --query "ResourceRecordSets[?Name == 'instance-$counter.$r53_domain.'].ResourceRecords" --output text >> /tmp/existing-ips.txt
  aws --region $region route53 list-resource-record-sets --hosted-zone-id $hosted_zone --query "ResourceRecordSets[?Name == 'instance-$counter.$r53_domain.']" --output text >> /tmp/existing-records.txt
  ((counter++))
done

diff /tmp/existing-ips.txt /tmp/this-ip.txt

echo "Does this instance IP exist in a $application route53 record?"
this_ip_result=`grep $this_ip /tmp/existing-ips.txt | wc -l`

if [[ $this_ip_result -gt 0 ]]
then
  echo "Yes, this instance IP already exists in a $application route53 record."
  echo "Nothing left to do"
else
  echo "No, this instance IP does not exist in a $application route53 record."
  echo "Adding route53 record... "
  counter=1
    until [ $counter -gt $asg_desired ]
    do
        grep -L instance-$counter.$r53_domain /tmp/existing-records.txt > /tmp/instance-$counter.$r53_domain.txt
        if [ -s /tmp/instance-$counter.$r53_domain.txt ]
            then
                echo "instance-$counter.$r53_domain does not exist... Adding!"
                aws --region $region route53 change-resource-record-sets --hosted-zone-id $hosted_zone --change-batch '{ "Comment": "Auto Scaling Creating Record Set", "Changes": [ { "Action": "CREATE", "ResourceRecordSet": { "Name": "instance-'$counter'.'$r53_domain'", "Type": "A", "TTL": 120, "ResourceRecords": [ { "Value": "'"$this_ip"'" } ] } } ] }'
            else
                echo "Updating Record Set!"
                echo "Updating Route53 Records with the following IPs ..."
                diff /tmp/existing-ec2-ips.txt /tmp/existing-ips.txt | grep ">" | sed 's/> //g'
                ip_update=`diff /tmp/existing-ec2-ips.txt /tmp/existing-ips.txt | grep ">" | sed 's/> //g'`
                record_update=`grep -B 1 $ip_update /tmp/existing-records.txt | grep $application | awk '{print $1}' | awk 'FNR == 1 {print}'`
                aws --region $region route53 change-resource-record-sets --hosted-zone-id $hosted_zone --change-batch '{ "Comment": "Auto Scaling Updating Record Set", "Changes": [ { "Action": "UPSERT", "ResourceRecordSet": { "Name": "'"$record_update"'", "Type": "A", "TTL": 120, "ResourceRecords": [ { "Value": "'"$this_ip"'" } ] } } ] }'
            fi
        ((counter++))
    done
fi
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68460784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档