Skip to content

Instantly share code, notes, and snippets.

@will-fong
Forked from ziritrion/dezoomcamp-vm.md
Last active February 7, 2022 03:58
Show Gist options
  • Save will-fong/6304faba8f42bbfff97f25b80f4d9e41 to your computer and use it in GitHub Desktop.
Save will-fong/6304faba8f42bbfff97f25b80f4d9e41 to your computer and use it in GitHub Desktop.
Quick guide to easily create a VM instance in GCP and set up SSH access with Gcloud SDK

Install and setup Gcloud SDK

  1. Download Gcloud SDK from this link and install it according to the instructions for your OS.
  2. Initialize the SDK following these instructions.
    1. Run gcloud init from a terminal and follow the instructions.
    2. Make sure that your project is selected with the command gcloud config list

Creating a VM instance

  1. From your project's dashboard, go to Cloud Compute > VM instance
  2. Create a new instance with this config:
    • Any name of your choosing
    • Pick your favourite region. You can check out the regions iin this link.
    • Pick a E2 series instance. A e2-standard-4 instance is recommended (4 vCPUs, 16GB RAM)
    • Change the boot disk to Ubuntu. The Ubuntu 20.04 LTS version is recommended. Also pick at least 30GB of storage.
    • Leave all other settings on their default value and click on Create.
  3. When you create an instance, it will be started automatically. You can skip to step 3 of the next section.

Set up SSH access

  1. Start your instance from the VM instances dashboard.
  2. In your local terminal, make sure that gcloud SDK is configured for your project. Use gcloud config list to list your current config's details.
    1. If you have multiple google accounts but the current config does not match the account you want:
      1. Use gcloud config configurations list to see all of the available configs and their associated accounts.
      2. Change to the config you want with gcloud config configurations activate [name of project]
    2. If the config matches your account but points to a different project:
      1. Use gcloud projects list to list the projects available to your account (it can take a while to load).
      2. use gcloud config set project [name of project] to change your current config to your project.
  3. Set up the SSH connection to your VM instances with gcloud compute config-ssh
    • Inside ~/ssh/ a new config file should appear with the necessary info to connect.
    • If you did not have a SSH key, a pair of public and private SSH keys will be generated for you.
    • The output of this command will give you the host name of your instance in this format: instance.zone.project ; write it down.
    • If you experience the following permission issue Required 'compute.projects.setCommonInstanceMetadata' permission, you may need to add the following IAM Roles to your service account to set up the SSH keys.
      • Compute Admin
      • Service Account User
    • In total you will have 6 IAM roles configured.
  4. You should now be able to open a terminal and SSH to your VM instance like this:
    • ssh instance.zone.project
  5. In VSCode, with the Remote SSH extension, if you run the command palette and look for Remote-SSH: Connect to Host (or alternatively you click on the Remote SSH icon on the bottom left corner and click on Connect to Host), your instance should now be listed. Select it to connect to it and work remotely.
    • VSCode may return an error for Permission denied (publickey).. You may resolve this by ensuring that the SSH config file correctly lists the Hostname and User which may be your Windows AD login.
    • Alternatively, continue by launching the VM in your browser from the GCP portal.
    ...
    [22:19:19.119] > ca\\dw814qu@34.105.117.37: Permission denied (publickey).
    [22:19:19.143] > The process tried to write to a nonexistent pipe.
    ...
    

Installation

Upgrade VM

  1. Run this first in your SSH session: sudo apt update && sudo apt -y upgrade
    • It's a good idea to run this command often, once per day or every few days, to keep your VM up to date.

Install Anaconda

  1. In your local browser, go to the Anaconda download page, scroll to the bottom, right click on the 64 bit x86 installer link under Linux and copy the URL.
  2. In your SSH session, type wget <anaconda_url> to download the installer.
    • For example: wget https://repo.anaconda.com/archive/Anaconda3-2021.11-Linux-x86_64.sh
  3. Find the filename of the installer with ls
  4. Run the installer with bash <filename> (you can start typing the name and then press the Tab key to autocomplete)
  5. Follow the on-screen instructions. Answer yes to all yes/no questions and leave all other default values.
  6. Log out of your current SSH session with exit and log back in.
    • You should now see a (base) at the beginning of your command prompt.
  7. You may now remove the Anaconda installer with rm <filename>.
    • For example: rm Anaconda3-2021.11-Linux-x86_64.sh

Install Docker

  1. Run sudo apt install docker.io to install it.
  2. Change your settings so that you can run Docker without sudo:
    1. Run sudo groupadd docker
      1. Run sudo gpasswd -a $USER docker
      2. Log out of your SSH session with exit and log back in.
      3. Run sudo service docker restart
      4. Test that Docker can run successfully with docker run hello-world
  3. Now to install Docker Compose:
    1. Go to https://github.com/docker/compose/releases and copy the URL for the docker-compose-linux-x86_64 binary for its latest version.
    2. Create a folder for binary files for your Linux user:
      1. Create a subfolder bin in your home account with mkdir ~/bin
      2. Go to the folder with cd ~/bin
      3. Download the binary file with wget <compose_url> -O docker-compose
        • If you forget to add the -O option, you can rename the file with mv <long_filename> docker-compose
        • For example: wget https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-x86_64 -O docker-compose
      4. Make sure that the docker-compose file is in the folder with ls
      5. Make the binary executable with chmod +x docker-compose
      6. Check the file with ls again; it should now be colored green. You should now be able to run it with ./docker-compose version
      7. Go back to the home folder with cd ~
      8. Run nano .bashrc to modify your path environment variable:
        1. Scroll to the end of the file
          • You can use the down arrow to scroll manually to the bottom and you may need to alt tab back to the VM window to confirm your cursor position
        2. Add this line at the end:
          export PATH="${HOME}/bin:${PATH}"
        3. Press CTRL + o in your keyboard to save the file.
        4. Press Enter.
        5. Press CTRL + x in your keyboard to exit the Nano editor.
      9. Reload the path environment variable with source .bashrc
      10. You should now be able to run Docker compose from anywhere; test it with docker-compose version

Install Terraform

  1. Run curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
  2. Run sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
  3. Run sudo apt-get update && sudo apt-get install terraform
  4. Check the installation with terraform version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment