Search code examples
google-cloud-platformgoogle-container-optimized-os

Adding Domain Nameserver into Google Container Optimized OS


I would like to prepend our own domain nameserver into COS. How should I do it ?

Is it just create the folowing in /etc/dhcp/dhclient.conf:

  prepend domain-name-servers <domain ip>;

I have added the above configuration but I still not able to use my domain in the COS VM instance. Is there something I've missed ?

How do I restart the network adapter in COS without reseting/rebooting ?


Solution

  • COS uses "cloud-init". If you want to add dns server as configs like this to COS, you'd use cloud-init as a way to configure your instance when it boots up. The cloud-init tool expects its configuration in the value of the user-data key of the instance metadata. For more information1

    To pass the configurations of cloud-init to the instance, you need to create your instance with the flag: --metadata-from-file user-data=[filename], or add the user-data=[filename] key value pair to the instance from the console, where the file would be stored on an external location like cloud storage, to which you'd provide the URL. It's also possible to just copy the config into the value section when setting the metadata. Example configurations to specify name servers and domains can be found in the following link.

    By replacing the yaml config value in metadata (but keeping the "user-data" key) with the following config, you can configure resolv.conf to use custom name servers and get the instance to use those name servers for address resolution.

    As an example you can create a file called cloud-config-resolv containing the following:

    #cloud-config
    
    write_files:
    - path: /etc/systemd/resolved.conf
    permissions: "0644"
    owner: root:root
    content: |
    # This is my custom resolv.conf!
    [Resolve]
    DNS= 8.8.8.8 (Use your IP)
    
    runcmd:
    - ['systemctl', 'restart', 'systemd-resolved']
    

    You can then run the following command to add [Your-IP] to the resolv.conf.

    gcloud compute instances create instance-name \
    --image-family cos-stable \
    --image-project cos-cloud \
    --metadata-from-file user-data=cloud-config-resolv \
    --zone us-central1-a
    

    I'm not confirmed that it will persist after 24hrs as dhcp lease is renewed and any changes are cleared. But the file does persist through network daemon restarts and VM restarts.