ACING THE CKAD

CKAD Tips: How to create & connect to a Google Cloud VM using SSH on Mac/Linux

Edidiong Asikpo
Ambassador Labs
Published in
8 min readJan 19, 2022

--

Welcome to the second article of my CKAD Tips series where I share my learnings while studying for the CKAD.

Every week, I will demystify Kubernetes concepts you need to know before taking the CKAD and simplify them using real-life examples to make preparing for and passing the CKAD easier for you.

Getting Started with CKAD: What did I struggle with?

In the Kubernetes for Developers course taken in preparation for the CKAD exam, the tutor only explained how to connect to a Google Cloud Compute virtual machine (VM) from a Windows machine using SSH but didn’t explain how to implement it on a Mac or Linux machine.

And as someone who wasn’t familiar with GCP, I struggled with figuring out how to replicate the same thing on a macOS machine until I figured it out. I knew someone else might encounter the same problem, so I decided to write about it.

In this article, you will learn how to create a virtual machine on the cloud using the Google Cloud Compute Engine and how to connect to that VM from your CLI using SSH on a macOS or Linux machine.

What is the Google Cloud Compute Engine?

One of the key components of the Google Cloud Platform (GCP) is the Compute Engine. The Compute Engine is a secure and customizable compute service that lets you create and run virtual machines from a standard or custom image on Google’s infrastructure.

Virtual machines (popularly called VMs) are essential in many situations as they are cost-effective, can be used to test your applications against other operating systems, and improve IT efficiency, amongst other things.

VMs can be installed on your local desktop, but they could potentially reduce the performance or speed of your computer. So, why not create one on the cloud, use it for your testing or experimentation, and shut it down when you are done.

Prerequisites

Thanks to the power of cloud computing, you don’t need a lot to set this up. Here’s what you need:

  • A macOS or Linux machine
  • A Google Cloud account
  • gcloud CLI installed on your machine

Ready? Let’s dive in.

The Google Cloud Compute Engine uses key-based SSH authentication to establish connections to Linux virtual machine instances.

After creating the VM, you’ll need to perform several configurations to connect the VM to your CLI. If you use the gcloud command-line tool to connect to your VMs, the Compute Engine will perform these configurations on your behalf, but if you want to do it manually, you’d have to perform these configurations yourself. Don’t fret! We will cover both methods in this tutorial.

Creating the VM Instance on Google Cloud Compute Engine

Of course, before we can “connect” a VM to our CLI, we need to have created that VM first, and that’s what we will be covering in this section of the tutorial. If you already have an existing VM on Google Cloud and want to delve into learning how to connect it to your CLI via SSH, you can skip this section and go to the Connecting to GCP VM from your CLI using SSH section of this article.

The first thing you need to do is create a project on the Google Cloud Platform. To do this, click on NEW PROJECT, add a name, ID, organization (if any) and click on the CREATE button.

Google Cloud Compute (GCE) requires a new VPC to be created and a firewall rule allowing all traffic to be included. So, now that you have a project running let’s set up the VPC and firewall rule.

To do this, go to the navigation bar at the top-left corner of your screen and click on VPC Network. Here you’ll be required to enable the Compute Engine API (The Compute Engine API allows you to create & run VMs on GCP) on your project following the instructions displayed on your screen.

As soon as you’ve enabled the Compute Engine API, you’ll now be able to access the default VPC network created by Google. You can choose to use the default VPC network or create yours by clicking on CREATE VPC NETWORK.

You can then add a name and description for the VPC network, select a region, & IP address range for the subnet, and click on the Create button to complete the VPC creation process.

The next thing you need to do is add your preferred firewall rules to your newly created VPC network. To do this, go to Firewall on the navbar and click on CREATE FIREWALL RULE.

Here, you’ll add a name and description for the firewall rule, and select the VPC network you just created in the Network field.

In the Targets field, select the All instances in the network option, add an IP range, click on the Allow all checkbox and finally, click on CREATE to complete the firewall rule and VPC network creation process.

Now that you’ve completely set up your VPC network with its firewall rule, you can go ahead and create the VM on Google Cloud by going to the Compute Engine on the navigation bar and clicking on the VM Instances option.

Here, you can either use the default settings or update them to meet your needs & business specification. In my case, I added a name, used Ubuntu as my OS, selected the N1 machine type, and increased the size to 20GB.

After completing the configuration steps above, click on the Create Instance button and voila — a new VM now running on Google Cloud.

Connecting to GCP VM from your CLI using SSH

Now that you’ve created a VM on GCP, you can now connect it to your CLI using SSH.

If you don’t have an existing SSH key, here’s how to generate one. Now, copy your public SSH key, go back to the GCP user interface and click on the name of your VM instance (As seen in the image above), click on Edit, scroll down to the SSH Keys section, click on the Add an Item button, and paste your public SSH key.

Before clicking on the Save button, take note of the username of your SSH key displayed right beside your public SSH key as you’ll need that later.

When you eventually click on the Save button, you’ll be redirected back to the VM instances page where you’ll see the External IP of your VM instance. Take note of it as you’ll need it soon.

With your SSH key now added, you can now connect the VM instance to your CLI using it. To do this, run the command below on your CLI:

ssh -i id_rsa edidiongasikpo@34.123.123.244

Replace:

  • id_rsa with the location of your private key
  • edidiongasikpo with the username of your public SSH key
  • 34.123.123.244 with the external IP of your VM instance

Ensure that you are currently inside the path where your SSH key is saved using cd <ssh folder>. If not, the command above will not work.

If you did everything correctly, your VM instance should now be connected to your CLI and ready for use. 😃

Connecting to GCP VM from your CLI using the gcloud command-line tool

The difference between the method explained above & using the gcloud command-line tool is that gcloud automatically creates & manages the SSH key for you.

This means you won’t need to manually generate and paste your public SSH key in the VM configuration page or even add the path of your private SSH key when you are connecting to the VM from your CLI.

To use the gcloud command-line tool, ensure Google Cloud’s SDK is installed on your computer. After confirming, run this command gcloud auth login to trigger the gcloud authentication process, which will redirect you to log in on your browser.

Once you are logged in, you have to tell gcloud which project you want to work on and you can do that by running this command:

gcloud config set project PROJECT_ID

Sometimes, the default zone on your gcloud might not be the same as your new projects and this can trigger some errors. So, it is always advisable to set the zone of your project. You can do that by running this command:

gcloud config set compute/{insert your zone here}

With everything set up correctly now, you are ready to run the final command (see it below) that will connect your VM on Google Cloud to your CLI:

glcoud compute ssh {name of your VM instance} 

A few seconds after running this command, you’ll see that you’ve successfully been able to connect to the Google Cloud Compute VM from your CLI using the cloud command-line tool. 🎉

Note: Always shut down your VM instances after using them to avoid being charged.

I covered a lot of ground in this article, and I really appreciate that you made it all the way to the end. 😃

Ace the CKAD with me!

This article is part of the CKAD Tips weekly series where I write articles that will enable anyone studying for the CKAD to excel during the examinations. Stay up to date on the latest additions to this series by following us on Medium and Twitter.

The CKAD series, so far:

--

--