有没有办法在启动时使用Route53使用Route53的AWS现场实例寄存器?

发布于 2025-01-28 11:22:47 字数 201 浏览 3 评论 0原文

我们有一些测试,DEV和CI服务器,我们已经设置了使用Route53映射到特定域的长期运行持续的点实例。这效果很好 - 我们可以节省下来,我们可以分配这些产品而不会过多关注成本,但是由于可用性,我们时不时就失去了实例。当他们回来时 - 他们带着不同的IP地址返回,这破坏了路线。

当这些实例返回在线时(通常在一两分钟之内)时,是否有一个好方法可以自动重新映射到新的IP地址?

We have some test, dev and ci servers that we have setup as long running persistent spot instances mapped to specific domains using route53. This works great - we get the savings, we can allocate these without too much concern about cost but every now and then we loose the instance due to availability. When they come back - they come back with different IP addresses which breaks the route.

Is there a good way to have these instances automatically remap to the new IP address when they come back online (usually within a minute or two)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷夜 2025-02-04 11:22:47

谨慎:我不相信这种方法毕竟是在起作用。虽然我可以按照我的期望确认所有运行,但在分配了这些机器后,新路线无法正确设置。我不确定这是否是因为该脚本运行时没有启动某些服务,或者亚马逊是否特别禁止这种行为。我很想知道别人发现的东西。

-

nb,正确的答案很可能是使用弹性IP地址,据我了解,我可以完全避免使用一个静态IP地址。我没有对此进行成本计算,但是它可能比下面提供的解决方案便宜。

我们最终提出的是一个使用AWS实例元数据和CLI进行Route53调用的脚本重新启动。这对我们的旧Ubuntu 14.04实例不起作用,但似乎在我们的较新的Ubuntu 20.04实例上。

它的工作方式如下:

  1. 我们构建了一个称为setUpRoute53.sh的小脚本,该脚本知道如何对Route53进行单个调用。
  2. 我们为Cron添加了一份工作,以在每个重启上运行此操作。
  3. (奖励)我们还创建了一个Ansible脚本,为我们本地运行的每个虚拟主机添加其他线路 - 我们使用Nginx反向代理多个服务。

我们当前在Ubuntu用户中运行此操作 - crontab看起来像这样:

# m h  dom mon dow   command
@reboot /home/ubuntu/setupRoute53.sh example.com test.example.com

setupRoute53.sh看起来像这样:

#!/bin/sh

# setupRoute53.sh

export ROOT_DOMAIN="$1"
export ROUTE53_HOSTNAME="$2"
export IP="$3"

if [ -z "$1" ]; then
    echo "Usage: $1 <route53 domain> <this hostname> ";
    echo "";
    echo "Example: $1 tokenoftrust.com test.tokenoftrust.com";
    echo;
    exit;
fi

if [ -z "$3" ]; then
    echo "IP not given...trying EC2 metadata...";
    IP=$( curl http://169.254.169.254/latest/meta-data/public-ipv4 )
fi

echo "Updating $ROUTE53_HOSTNAME to : $IP"


HOSTED_ZONE_ID=$( aws route53 list-hosted-zones-by-name | grep -B 1 -e "$ROOT_DOMAIN" | sed 's/.*hostedzone\/\([A-Za-z0-9]*\)\".*/\1/' | head -n 1 )
echo "Hosted zone being modified: $HOSTED_ZONE_ID"

INPUT_JSON=$(echo '{
  "Comment": "Update the A record set",
  "Changes": [
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "HOSTNAME",
        "Type": "A",
        "TTL": 60,
        "ResourceRecords": [
          {
            "Value": "127.0.0.1"
          }
        ]
      }
    }
  ]
}' | sed "s/127\.0\.0\.1/$IP/" | sed "s/HOSTNAME/$ROUTE53_HOSTNAME/" )


# http://docs.aws.amazon.com/cli/latest/reference/route53/change-resource-record-sets.html
# We want to use the string variable command so put the file contents (batch-changes file) in the following JSON
INPUT_JSON="{ \"ChangeBatch\": $INPUT_JSON }"

aws route53 change-resource-record-sets --hosted-zone-id "$HOSTED_ZONE_ID" --cli-input-json "$INPUT_JSON"

exit 0;

Caution: I'm not convinced this approach is working after all. While I can confirm everything runs as I expected it to - the new routes didn't get setup correctly after these machines were assigned new spot instances. I'm not sure if this is because some service is not started by the time this script runs or if Amazon specifically prohibits this behavior. I'd be curious to hear what others have found.

--

N.B. The right answer here might well be using elastic IP addresses which as I understand allow you to have a single static IP address avoiding this issue altogether. I've not done the cost calculation on this but it might well be cheaper than the solution offered below.

What we ended up coming up with is a script that uses the AWS instance metadata and cli to make a route53 call upon reboot. This did NOT work on our old Ubuntu 14.04 instances but appears to on our newer Ubuntu 20.04 instances.

Here's how it works:

  1. We built a little script called setupRoute53.sh that knows how to make a single call to route53.
  2. We added a job to cron to run this on each reboot.
  3. (Bonus) we also created an ansible script to add additional lines to the crontab for each virtual host we're running locally - we reverse proxy multiple services using nginx.

We're currently running this within the ubuntu user - the crontab looks like this:

# m h  dom mon dow   command
@reboot /home/ubuntu/setupRoute53.sh example.com test.example.com

And setupRoute53.sh looks like this:

#!/bin/sh

# setupRoute53.sh

export ROOT_DOMAIN="$1"
export ROUTE53_HOSTNAME="$2"
export IP="$3"

if [ -z "$1" ]; then
    echo "Usage: $1 <route53 domain> <this hostname> ";
    echo "";
    echo "Example: $1 tokenoftrust.com test.tokenoftrust.com";
    echo;
    exit;
fi

if [ -z "$3" ]; then
    echo "IP not given...trying EC2 metadata...";
    IP=$( curl http://169.254.169.254/latest/meta-data/public-ipv4 )
fi

echo "Updating $ROUTE53_HOSTNAME to : $IP"


HOSTED_ZONE_ID=$( aws route53 list-hosted-zones-by-name | grep -B 1 -e "$ROOT_DOMAIN" | sed 's/.*hostedzone\/\([A-Za-z0-9]*\)\".*/\1/' | head -n 1 )
echo "Hosted zone being modified: $HOSTED_ZONE_ID"

INPUT_JSON=$(echo '{
  "Comment": "Update the A record set",
  "Changes": [
    {
      "Action": "UPSERT",
      "ResourceRecordSet": {
        "Name": "HOSTNAME",
        "Type": "A",
        "TTL": 60,
        "ResourceRecords": [
          {
            "Value": "127.0.0.1"
          }
        ]
      }
    }
  ]
}' | sed "s/127\.0\.0\.1/$IP/" | sed "s/HOSTNAME/$ROUTE53_HOSTNAME/" )


# http://docs.aws.amazon.com/cli/latest/reference/route53/change-resource-record-sets.html
# We want to use the string variable command so put the file contents (batch-changes file) in the following JSON
INPUT_JSON="{ \"ChangeBatch\": $INPUT_JSON }"

aws route53 change-resource-record-sets --hosted-zone-id "$HOSTED_ZONE_ID" --cli-input-json "$INPUT_JSON"

exit 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文