如何为ASG实例设置基于序数的DNS名称

如何为ASG实例设置基于序数的DNS名称

本文介绍了如何为ASG实例设置基于序数的DNS名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的开发环境中,我想为ECS群集主机创建易于记忆的序号dns名称.部署时,我们从1台主机扩展到2台主机,然后耗尽/扩展.

In my dev environment I want to create easy to remember ordinal dns names for ECS cluster hosts. When we deploy we scale out from 1 to 2 hosts and then drain/scale back in.

目前,我们像这样使用userdata,因此请设置dns名称

At present we use userdata like so so set the dns name

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实例的数据,并使用启动时间来确定序数名称

有人知道更优雅的解决方案吗?

Does anyone know of a more elegant solution?

推荐答案

因此,这是我们为devTest环境管理便捷的fqdns记录的方式.当我们扩展群集以确保第二/第三台主机不尝试使用第一台主机的名称时,这一点很重要

So, here's how we manage convenience fqdns records for devTest environments. This becomes important when we scale the cluster to ensure the 2nd/3rd hosts don't attempt to take the 1st host's name

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

这篇关于如何为ASG实例设置基于序数的DNS名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 14:33