skip to Main Content

For learning purpose, I’m trying to install and setup my own Kubernetes Cluster on GCP.

I want to provision my instances on GCP with a bootstrap script.

Here is my google_compute_instance config

resource "google_compute_instance" "default" {
    name = var.vm_name
    machine_type = "f1-micro"
    zone = "europe-west1-b"

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

    network_interface {
        network = var.network
        access_config {
            // Include this section to give the VM an external IP address
        }
    }

    provisioner "remote-exec" {
        script = var.script_path
        connection {
            type        = "ssh"
            host        = var.ip_address
            user        = "root"
        }
    }

    tags = ["node"]
}

I have this issue when I do terraform apply

Error: Failed to open script ‘sudo apt-get update

sudo apt-get install
apt-transport-https
ca-certificates
curl
gnupg-agent
software-properties-common
zsh
vim

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key
add – sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs)
stable" sudo apt-get update && sudo apt-get install docker-ce
docker-ce-cli containerd.io

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo
apt-key add – cat <<EOF | sudo tee
/etc/apt/sources.list.d/kubernetes.list deb https://apt.kubernetes.io/
kubernetes-xenial main EOF sudo apt-get update sudo apt-get install -y
kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl ‘:
open sudo apt-get update

sudo apt-get install
apt-transport-https
ca-certificates
curl
gnupg-agent
software-properties-common
zsh
vim

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key
add – sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs)
stable" sudo apt-get update && sudo apt-get install docker-ce
docker-ce-cli containerd.io

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo
apt-key add – cat <<EOF | sudo tee
/etc/apt/sources.list.d/kubernetes.list deb https://apt.kubernetes.io/
kubernetes-xenial main EOF sudo apt-get update sudo apt-get install -y
kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl :
no such file or directory

All my instances are created on the cloud, It’s seems to find the bootstrap script but it is showing this error.

What did I miss? Is there a better way to do it ?

Here is the script:

#bin/bash

sudo apt-get update

sudo apt-get install 
    apt-transport-https 
    ca-certificates 
    curl 
    gnupg-agent 
    software-properties-common 
    zsh 
    vim

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository 
   "deb [arch=amd64] https://download.docker.com/linux/debian 
   $(lsb_release -cs) 
   stable"
sudo apt-get update && sudo apt-get install docker-ce docker-ce-cli containerd.io

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

2

Answers


  1. Chosen as BEST ANSWER

    For those who are interested, I have found an easier solution, without using ssh but by using the google metadata available at creation of the resource.

    metadata_startup_script = file("./scripts/bootstrap.sh")

    resource "google_compute_instance" "default" {
        name = var.vm_name
        machine_type = "e2-standard-2"
        zone = "europe-west1-b"
    
        boot_disk {
            initialize_params {
                image = "debian-cloud/debian-9"
            }
        }
    
        network_interface {
            network = var.network
            access_config {
                // Include this section to give the VM an external IP address
            }
        }
    
        metadata_startup_script = file("./scripts/bootstrap.sh")
    
        tags = ["node"]
    }
    

  2. You should provide the private_key argument in the connection block of remote-exec.

    private_key - The contents of an SSH key to use for the connection. These can be loaded from a file on disk using the file function. This takes preference over the password if provided.
    

    A sample block could be like this:

      provisioner "remote-exec" {
            script = var.script_path
            connection {
                host     = var.ip_address
                type     = "ssh"      
                user     = "root"
                private_key = fileexists("/temp/private_key") ? file("/temp/private_key") : file("C:/private_key")             
            }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search