Configuring FQDN for GCE instance on startup

蹲街弑〆低调 提交于 2019-12-01 09:18:21

According to this article 'hostname' is part of the default metadata entries that provide information about your instance and it is NOT possible to manually edit any of the default metadata pairs. You can also take a look at this video from the Google Team. Within the first few minutes it is mentioned that you cannot modify default metadata pairs. As such, it does not seem like you can specify the hostname upon instance creation other than through the use of a start-up script like you've done already. It is also worth mentioning that the hostname you've specified will get deleted and auto-synced by the metadata server upon reboot unless you're using a start-up script or something that would modify it every time.

If what you're currently doing works for what you're trying to accomplish, it might be the only workaround to your scenario.

try this:

  1. Go to your GCE >> VM instances panel.
  2. stop your gce instance.
  3. clic on the instance name.
  4. Edit your instance, adding this values on Custom metadata fields:

    • Key field: hostname / Value field: your.server.hostname

    • Key field: startup-script / Value field: sudo -s hostnamectl set-hostname your.server.hostname

    setup-example-image.png

Finally, start your instance and test with a hostnamectl command.

regards!

Here is a patch for /usr/share/google/set-hostname to set FQDN to GCE instance.

https://gist.github.com/yuki-takeichi/3080521322f0f1d159ea6a343e2323e6

Before you use this patch, you must set your desired FQDN in your instance's metadata by specifying hostname key.

Hostname is set each time instance's IP address is renewed by dhclient. set-hostname is just a hook script which dhclient executes and serves new IP address and internal hostame to, and modifies /etc/hosts. This patch changes the source of hostname by querying instance's metadata from metadata server.

The original set-hostname script is here: https://github.com/GoogleCloudPlatform/compute-image-packages/blob/master/google_config/bin/set_hostname.

Use this patch at your own risk.

When creating a VM, you can specify a custom FQDN hostname as an optional parameter. This feature is currently in Beta.

$ gcloud beta compute instances create INSTANCE_NAME --hostname example.hostname

This should work across OSes, and eliminate the need for workaround scripts. More info in the docs.

-- Sirui (Product Manager, Google Compute Engine)

I've looked throughout this site to find answered questions and found a few things that work but with a couple solutions combined. This thread seems the place to answer.

1) echo example.com > /etc/hostname

2) add -- 127.0.1.1 example.com in /etc/hosts

3) add -- hostnamectl set-hostname example.com -- command to /etc/rc.local script

4) uncomment /etc/dhcp/dhclient.conf line:

supersede domain-name "example.com";

5) profit.... Seems to stick after each reboot

(Note example.com is your domain name: fqdndomain.com - yourfqdndomain.org) Also note this is for Ubuntu or Debian. Other Unix May slightly vary. I've tested this on Ubuntu 16.04

Always on the wording NOT possible to manually edit any of the default metadata pairs, how about the instant level default metadata "/scheduling"? we could set them manually as mentioned in this article

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!