1

I tried to create a load balancer with the MIG consisting of three VMs. Additionally, I created a VM that is not part of the MIG but operates within the same subnet. The reason for making it was to check the connectivity between the VM and the VMs from the MIG.

Both MIG's VMs and the additional VM have the same startup script. I'll present it using the configuration of the additional VM (var.server_port is 80):

resource "google_compute_instance" "ssh-vm" {
  name = "ssh-vm"
  machine_type = "e2-standard-2"
  project = var.pro
  tags = ["allow-ssh"]
  zone = "europe-west1-b"

  boot_disk {
    initialize_params {
      image = "ubuntu-2004-focal-v20221213"
    }
  }

  network_interface {
    subnetwork = google_compute_subnetwork.subnetwork.self_link
    access_config {
      nat_ip = google_compute_address.static.address
    }
  }

  metadata = {
    startup-script = <<-EOF
        #!/bin/bash
        sudo snap install docker
        sudo docker version > file1.txt
        sleep 5
        sudo docker run -d --rm -p ${var.server_port}:${var.server_port} \
        busybox sh -c "while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; \
        echo 'yo'; } | nc -l -p ${var.server_port}; done"
        EOF
  }
}

One can see that the idea is to spin up docker and run the server that listens on :80. When I ssh onto this instance and run sudo docker ps, I receive the info that the process is running and serving on 80. The problem is that I have the same startup script within Terraform's instance_template module, but when I ssh into any of the machines from the MIG and run the same command, it says that the docker command is not found. Here's the configuration of the module:

module "instance_template" {
  source = "terraform-google-modules/vm/google//modules/instance_template"
  version = "7.9.0"
  region = var.region
  project_id = var.pro
  network = google_compute_network.vpc-network.self_link
  subnetwork = google_compute_subnetwork.subnetwork.self_link
  service_account = {
    email = google_service_account.service-acc.email
    scopes = ["cloud-platform"]
  }

  name_prefix = "webserver"
  tags = ["template-vm", "allow-ssh"]
  machine_type = "e2-standard-2"
  startup_script = <<-EOF
  #!/bin/bash
  sudo snap install docker
  sudo docker version > docker_version.txt
  sleep 5
  sudo docker run -d --rm -p ${var.server_port}:${var.server_port} \
  busybox sh -c "while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; \
  echo 'yo'; } | nc -l -p ${var.server_port}; done"
  EOF
  source_image = "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-2004-focal-v20221213"
  disk_size_gb = 10
  disk_type = "pd-balanced"
  preemptible = true

}

I checked and the module's attribute for the startup script is indeed startup_script and not metadata_startup_script.

SkogensKonung
  • 601
  • 1
  • 9
  • 22
  • 2
    did you check serial console logs to see if startup script executed successfully or failed? – Vishal Bulbule Jan 13 '23 at 10:27
  • 1
    I've just checked the logs on the MIG's VMs and the `ssh-vm`. Everything is okay in the `ssh-vm`, but on the MIG's VMs, I can see `unable to contact snap store`. Is it possible that the traffic **from** the MIG's VMs is forbidden and the docker can't be installed? – SkogensKonung Jan 13 '23 at 10:40
  • 1
    possible , you can try adding these two command sudo apt update sudo apt install snapd – Vishal Bulbule Jan 13 '23 at 10:47
  • 1
    Adding these two command does not help. – SkogensKonung Jan 13 '23 at 13:58

0 Answers0