3

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?

The_Viper
  • 371
  • 1
  • 6
  • 14

6 Answers6

3

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.

Boyan
  • 679
  • 4
  • 8
  • +1 for english comprehension. I have read the linked article many times now, and I've obviously been blind to the sentence you point out - `You cannot manually edit any of these metadata pairs.` Thanks for this, I will have to use the startup script. Lame method of licensing in any case. – The_Viper Aug 15 '14 at 09:01
3

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!

2

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.

tacke
  • 145
  • 1
  • 5
  • getting 404 not foud for the repo url. Can you provide a updated url ? – el.severo Aug 20 '16 at 15:34
  • Have you seen [my last comment on your gits](https://gist.github.com/yuki-takeichi/3080521322f0f1d159ea6a343e2323e6#gistcomment-1876232) ? – el.severo Oct 17 '16 at 00:44
1

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)

Sirui Sun
  • 336
  • 3
  • 5
0

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

-2

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

xhk
  • 1