Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use cloud-init with a debian-based image on Google Cloud?

Following terraform best practice for bootstrapping instances, I'm working on a cloud-init config in order to bootstrap my instance. My only need is to install a specific package.

My terraform config looks like this:

resource "google_compute_instance" "bastion" {
  name         = "my-first-instance"
  machine_type = "n1-standard-1"
  zone         = "europe-west1-b"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }

  network_interface {
    network = "default"

    access_config {
      // Ephemeral IP
    }
  }

  metadata = {
    ssh-keys = "eugene:${file("/Users/eugene/.ssh/id_rsa.pub")}"
    user-data = file("my_cloud_init.conf")
  }
}

Following example for installing packages from cloud-init docs, here's the contents of my_cloud_init.conf:

#cloud-config

packages:
 - kubectl

After running terraform plan -out myplan and terraform apply myplan, I ssh onto the node only to find kubectl not available. Moreover, there's no evidence that cloud-init was run or that it exists on the node:

$ which -a cloud-init
$ cat /var/log/cloud-init
cat: /var/log/cloud-init: No such file or directory

Looking for clues about usage of cloud-init with Google Cloud Compute instances wasn't fruitful:

  • "Google Cloud Engine" page from cloud-init docs suggests settings user-data to a cloud-init config should be enough,
  • I see a cloud-init tutorial, but it's for Container Optimized OS,
  • there are some clues about cloud-init on other images, but nothing indicates cloud-init is available on debian-cloud/debian-9,
  • there's "Running startup scripts", but it has no mention of cloud-init.

I don't mind using another image, as long as it's Debian or Ubuntu and I don't have to make an image template myself.

How to use cloud-init with a debian-based image on Google Cloud? What am I missing?

like image 265
gmile Avatar asked Oct 05 '19 12:10

gmile


People also ask

Which of the images can be installed in the GCP virtual machines?

You can use one of the following image types: Public images are provided and maintained by Google, open source communities, and third-party vendors. By default, all Google Cloud projects have access to these images and can use them to create instances. Custom images are available only to your Cloud project.


1 Answers

To complement answer from @norbjd, posting this for completeness:

  • in order to install kubectl package that I planned initially, I had to write this cloud-init config:

    #cloud-config
    
    bootcmd:
     - curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
    
    apt:
      sources:
        kubernetes.list:
          source: "deb http://apt.kubernetes.io/ kubernetes-xenial main"
    
    packages:
     - kubectl
    
  • if you need to know which images contain which packages, these resources may be helpful:

    • Ubuntu:

      • Ubuntu Cloud Images (RELEASED)

      • Actual contents (example):

        • https://cloud-images.ubuntu.com/releases/releases/18.04/release-20191003/ubuntu-18.04-server-cloudimg-amd64.manifest
    • Debian:

      • List of differences between standard and cloud systems

      • Actual contents:

        • https://salsa.debian.org/cloud-team/debian-cloud-images/blob/master/config_space/package_config/EXTRAS
        • https://salsa.debian.org/cloud-team/debian-cloud-images/blob/master/config_space/package_config/CLOUD
like image 139
gmile Avatar answered Oct 21 '22 07:10

gmile