Configuring FQDN for GCE instance on startup

The_Viper picture The_Viper · Aug 14, 2014 · Viewed 7k times · Source

I am trying to start a google compute engine (GCE) instance with a pre-configured FQDN. We are intending to run an application that is licensed based on the contents of /etc/hosts.

I am starting the instances using the Google Cloud SDK utility - gcloud.

I have tried setting the "hostname" key using the metadata option like so:

gcloud compute instances create mynode (standard opts) --metadata hostname=mynode.example.com

Whenever I log into the developer console, under computer, instances, I can see hostname under "Custom metadata". This appears to be a new, custome key - it has no impact on what:

http://metadata.google.internal/computeMetadata/v1/instance/hostname

returns.

I have also tried setting "instance/hostname" like the below, which causes a parsing error when using gcloud.

--metadata instance/hostname=mynode.example.com

I have successfully used the startup scripts functionality of the metadata server to run a startup script that parses the new, internal IP address of the newly created instance, updated /etc/hosts. This appears to work but doesn't feel "like the google way".

Can I configure the FQDN (specifically, a domain name, as the instance name is always the hostname) of an instance, during instance creation, using the metaserver functionality?

Answer

Jaime Aedo Sanhueza picture Jaime Aedo Sanhueza · Dec 4, 2018

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!