如何使用实例的临时 IP 更新 Google Cloud DNS [英] How to update Google Cloud DNS with ephemeral IP for an instance

查看:18
本文介绍了如何使用实例的临时 IP 更新 Google Cloud DNS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 GCE 上有几个实例,我真的不需要静态地址,但我仍然需要通过 dns 名称来访问它们.由于每次重启实例时临时外部 ip 地址都会更改,我认为我可以使用某种启动脚本来更新 Google Cloud DNS 中该实例的 dns 条目(有点像 dyndns).

I have couple of instances on GCE that I don't really need static addresses for, but I still need to make them accessible via dns name. Since ephemeral external ip addresses change every time an instance is restarted, I thought that I could use some sort of startup script to update dns entry for that instance in Google Cloud DNS (a bit like dyndns).

我是否遗漏了什么,有更简单的方法可以通过 gcloud 将临时外部 IP 地址映射到 dns 记录?

Did I miss something and there is an easier way to map ephemeral external ip addresses to a dns record via gcloud?

如果没有,任何有关如何编写此类脚本的指示将不胜感激!

If not, any pointers on how to write such script would be highly appreciated!

推荐答案

以下假设您将 Google Cloud DNS 用于 foo.bar.com(即 dns 名称foo.bar.com.")和区域名称foo-bar-com"与您的 VM 位于同一项目中,并且您的 VM 具有配置选项此实例具有对所有 GCP 服务的完整 API 访问权限".被选中.您的 VM 在 DNS 中将被称为my-vm.foo.bar.com".

The following assumes that you are using Google Cloud DNS for foo.bar.com (ie. dns name "foo.bar.com.") with zone name "foo-bar-com" in the same project as your VM and that your VM has configuration option "This instance has full API access to all Google Cloud services." selected. Your VM will be called "my-vm.foo.bar.com" in DNS.

我确信可以适当地修改它以在不同的项目中使用 DNS 和/或更有限的权限.

I'm sure this could be appropriately modified to work with DNS in a different project and/or more limited permissions.

可能值得注意:这假设您使用的是Google Cloud DNS"而不是(仅)Google Domains"注册商,如果您使用后者(托管您的 DNS,而不仅仅是作为注册商)那么他们使用更新机制等动态直接支持合成动态 IP 地址(但它们在许多其他方面受到更多限制).

Probably worth noting: this assumes you are using 'Google Cloud DNS' and not (just) 'Google Domains' registrar, if you're using the latter (to host your DNS, and not just as a registrar) then they have direct support for synthetic dynamic ip address with some dyndns like update mechanism (but they're more limited in a bunch of other ways).

另请注意,要使事务成功,必须有一个具有正确 IP 和正确 TTL 的记录(即,第一次运行此程序时,您可能希望通过 UI 手动删除任何条目,然后运行此程序)注释掉 dns_del 的代码).

Also note that for transaction to succeed there already has to be a record with the right IP and the right TTL (ie. the first time you run this you may want to delete any entry by hand via the UI, and run this code with dns_del commented out).

#!/bin/bash

ttlify() {
  local i
  for i in "$@"; do
    [[ "${i}" =~ ^([0-9]+)([a-z]*)$ ]] || continue
    local num="${BASH_REMATCH[1]}"
    local unit="${BASH_REMATCH[2]}"
    case "${unit}" in
                     weeks|week|wee|we|w) unit=''; num=$[num*60*60*24*7];;
                           days|day|da|d) unit=''; num=$[num*60*60*24];;
                     hours|hour|hou|ho|h) unit=''; num=$[num*60*60];;
      minutes|minute|minut|minu|min|mi|m) unit=''; num=$[num*60];;
      seconds|second|secon|seco|sec|se|s) unit=''; num=$[num];;
    esac
    echo "${num}${unit}"
  done
}

dns_start() {
  gcloud dns record-sets transaction start    -z "${ZONENAME}"
}

dns_info() {
  gcloud dns record-sets transaction describe -z "${ZONENAME}"
}

dns_abort() {
  gcloud dns record-sets transaction abort    -z "${ZONENAME}"
}

dns_commit() {
  gcloud dns record-sets transaction execute  -z "${ZONENAME}"
}

dns_add() {
  if [[ -n "$1" && "$1" != '@' ]]; then
    local -r name="$1.${ZONE}."
  else
    local -r name="${ZONE}."
  fi
  local -r ttl="$(ttlify "$2")"
  local -r type="$3"
  shift 3
  gcloud dns record-sets transaction add      -z "${ZONENAME}" --name "${name}" --ttl "${ttl}" --type "${type}" "$@"
}

dns_del() {
  if [[ -n "$1" && "$1" != '@' ]]; then
    local -r name="$1.${ZONE}."
  else
    local -r name="${ZONE}."
  fi
  local -r ttl="$(ttlify "$2")"
  local -r type="$3"
  shift 3
  gcloud dns record-sets transaction remove   -z "${ZONENAME}" --name "${name}" --ttl "${ttl}" --type "${type}" "$@"
}

lookup_dns_ip() {
  host "$1" | sed -rn 's@^.* has address @@p'
}

my_ip() {
  ip -4 addr show dev eth0 | sed -rn 's@^    inet ([0-9.]+).*@1@p'
}

doit() {
  ZONE=foo.bar.com
  ZONENAME=foo-bar-com
  dns_start
  dns_del my-vm 5min A `lookup_dns_ip "my-vm.${ZONE}."`
  dns_add my-vm 5min A `my_ip`
  dns_commit
}

这篇关于如何使用实例的临时 IP 更新 Google Cloud DNS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆