如何解决如何为ASG实例设置基于序数的DNS名称
问题
在我的开发环境中,我想为ECS群集主机创建易于记忆的顺序dns名称。部署时,我们从1台主机扩展到2台主机,然后耗尽/扩展。
目前我们像这样使用userdata,因此请设置dns名称
INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
INSTANCE_IP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)
RECORD_CONFIG="/tmp/ecs-a-record.json"
cat >>$RECORD_CONFIG << ROUTE53
{
"Comment": "Create a friendly DNS name for the DOD ECS host","Changes": [{
"Action": "UPSERT","ResourceRecordSet": {
"Name": "dev-ecs.ourenv.dev","Type": "A","TTL": 300,"ResourceRecords": [{ "Value": "$INSTANCE_IP"}]
}
}]
}
ROUTE53
aws route53 change-resource-record-sets --hosted-zone-id ... --change-batch file://$RECORD_CONFIG
我认为我的选择是
- 探针dns名称,如果正在使用,请增加一个计数器,然后使用N + 1再次尝试
- 使用aws cli收集有关ASG实例的数据,并使用启动时间来确定序数名称
有人知道更优雅的解决方案吗?
解决方法
因此,这就是我们为devTest环境管理便捷的fqdns记录的方式。当我们扩展群集以确保第二/第三台主机不尝试使用第一台主机的名称时,这一点很重要
checkHostExists() {
host=$1
nc -z $host 22 >> /dev/null 2>&1 ; echo $?
}
findDevTestDNSName() {
base_name=$1
domain=$2
count=1
name=$(printf "%s%02d.%s" $base_name $count $domain)
while [[ "$(checkHostExists $name)" -eq "0" ]]
do
count=$((count+1))
name=$(printf "%s%02d.%s" $base_name $count $domain)
done
echo $name
}
##
# Main Userdata context
#
# Please Note: This is a Terraform template
# ${foo} refers to a foo variable passed to the template
# $${bar} refers to an actual env variable
# when terraform resolves the template then tf vars are replaced with
# concrete values in the usedata
##
# lookup the instance ID
INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
INSTANCE_IP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)
if [[ "${cluster_name}" =~ "devtest" ]]; then
# create a friendly hostname for the ECS host if this is a development test env
# install nmap/nc for host probing
yum install -y nmap
ECS_HOST_FQDNS=$(findDevTestDNSName ${cluster_name}-ecs ${dns_domain})
HOST_NAME=$(echo $ECS_HOST_FQDNS|sed 's/\..*//')
echo Setting DevTest ECS Hostname: $${ECS_HOST_FQDNS}
RECORD_CONFIG="/tmp/ecs-a-record.json"
cat >>$RECORD_CONFIG << ROUTE53
{
"Comment": "Create a friendly DNS name for the ${cluster_name} ECS host","Changes": [{
"Action": "UPSERT","ResourceRecordSet": {
"Name": "$${ECS_HOST_FQDNS}","Type": "A","TTL": 300,"ResourceRecords": [{ "Value": "$INSTANCE_IP"}]
}
}]
}
ROUTE53
aws route53 change-resource-record-sets --hosted-zone-id ${dns_zone_id} --change-batch file://$RECORD_CONFIG
fi
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。