Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Starting Your lab
3.
Activate Cloud Shell
4.
Task 1. Configure your environment
4.1.
Understanding regions and zones
4.2.
Finding project information
4.3.
Setting environment variables
4.4.
Creating a virtual machine with the gcloud tool
4.5.
Explore gcloud commands
5.
Task 2. Filtering command line output
6.
Task 3. Connecting to your VM instance
7.
Task 4. Updating the Firewall
8.
Task 5. Viewing the system logs
9.
Frequently Asked Questions
9.1.
What is a Cloud shell?
9.2.
What is an instance in a VM?
9.3.
Which command is used to list the available compute instances for the project?
10.
Conclusion
Last Updated: Mar 27, 2024

Getting Started with Cloud Shell and gcloud

Author Nagendra
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Google Shell provides command-line access to Google Cloud-hosted computing resources. You can manage your Google Cloud projects and resources with ease using Cloud Shell, a Debian-based virtual system with a permanent 5-GB home directory. You can get started immediately with Cloud Shell because it comes pre-installed with the gcloud command-line tool and other necessary utilities.

This blog describes how to use the gcloud tool and Cloud Shell to connect to computational resources housed on Google Cloud, as well as the specifics of utilising Cloud Shell and gcloud.

Without further ado, lets get started.

Starting Your lab

  • Select "Start Lab" from the menu. In the Lab Details panel, which is on the left, are the following:
    • The button to access Google Console
       
    • Time Remaining
       
    • The temporary login information required for this lab
       
    • Should it be necessary, further details to complete this lab
       
  • Open Google Console. The lab starts spinning up resources before opening a new tab on the Sign in page.
     
  • In the Sign in dialogue, copy the Username from the Lab Details panel if necessary. Choose Next.
     
  • From the Lab Details panel, copy the password, and then paste it into the Welcome dialogue. Select Next
     
  • Go through the following pages by clicking:
    • The terms and conditions are accepted.
       
    • Do not include two-factor authentication or recovery alternatives (because this is a temporary account).
       
    • Avoid registering for free trials.
       
  • A few while later, the Cloud Console launches.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Activate Cloud Shell

A virtual machine called Cloud Shell is full of programming tools. It uses the Google Cloud and provides a persistent 5GB home directory. Your Google Cloud resources can be accessed via the command line using Cloud Shell.

  • Click the Activate Cloud Shell button in the top right toolbar of the Cloud Console.
     
  • Then click Continue.
     

Provisioning and connecting to the environment take some time. You have already authenticated when you connect, and your PROJECT_ID is configured for the project.

Let's dive into the details of the tasks performed.

Task 1. Configure your environment

You will learn about the elements of the development environment that you can modify in this section.

Understanding regions and zones

Zones or regions are home to specific Google Compute Engine resources. A region is a designated geographic area where your resources can be managed. One or more zones exist in each region. In the Central United States, for instance, the us-central1 area designates a region with the zones us-central1-aus-central1-bus-central1-c, and us-central1-f. Zones in each region are shown in the following table:
Zonal resources are those that inhabit a certain zone. Instances of virtual machines and persistent disks exist in a zone. Both resources need to be in the same zone if you wish to attach a persistent disc to a virtual machine instance. The instance must also be located in the same area as the static IP address if you want to give it a static IP address.

  • Set the region to us-east1:

Command:

gcloud config set compute/region us-east1
  • Enter the following command to view the project region configuration.

Command:

gcloud config get-value compute/region
  • Decide on us-east1-c as the zone:

Command:

gcloud config set compute/zone us-east1-c
  • Run the next command to view the project zone setting:

Command:

gcloud config get-value compute/zone

Finding project information

  • Copy the project ID to the text editor or clipboard. Two places are identified with the project ID.
     
  • You will find it under Project info on the Dashboard of the Cloud Console. Click Cloud overview > Dashboard from the Navigation menu (Navigation menu icon) to access the Dashboard.
     
  • Run the following command in Cloud Shell to view the project id for your project:

Command:

gcloud config get-value project
  • Run the gcloud command listed below in Cloud Shell to view the project's specifics:

Command:

gcloud compute project-info describe --project $(gcloud config get-value project)

In the output, look for the zone and region information values. Later in this experiment, you'll use the zone (google-compute-default-zone) from the result.

Setting environment variables

When you develop scripts that contain executables or APIs, environment variables describe your environment and help you write them faster.

  • Using the value for name from the gcloud compute project-info describe command you previously executed, create an environment variable to keep <your_project_ID>:

Command: 

export PROJECT_ID=$(gcloud config get-value project)
  • In order to store your Zone, create an environment variable and replace <your_zone> with the zone value from the gcloud compute project-info describe the command you previously executed:

Command:

export ZONE=$(gcloud config get-value compute/zone)
  • Run the following commands to ensure that your variables were set correctly:

Command:

echo -e "PROJECT ID: $PROJECT_ID\nZONE: $ZONE"

Your Project ID and Zone will be output by the echo commands if the variables were set properly.

Creating a virtual machine with the gcloud tool

To build a new virtual machine (VM) instance, use the gcloud utility.

  • Run the following command to build your VM:

Command:

gcloud compute instances create gcelab2 --machine-type e2-medium --zone $ZONE

Output:

NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
gcelab2 e2-medium 10.128.0.2 34.67.152.90 RUNNING

Command details:

  • You may manage your Compute Engine resources with gcloud compute in a way that is more user-friendly than the Compute Engine API.
     
  • New instances are created using instances create.
     
  • The VM's name is gcelab2.
     
  • The machine type is specified as e2-medium via the —machine-type flag.
     
  • The —zone flag indicates the location of the VM's creation.
     
  • The gcloud programme can determine your intended zone based on your default parameters if you don't include the —zone flag. If not supplied in the create command, other necessary instance settings, such as machine type and image, are set to default values.
     

Explore gcloud commands

Simple usage instructions for the gcloud tool are accessible by including the -h flag (for help) at the end of any gcloud command.

  • Run the command line:

Command:

gcloud -h

By adding the —help flag to a command or using the gcloud help command, you can get more detailed help.

  • Run the command line:

Command:

gcloud config --help
  • Run the command line:

Command:

gcloud help config

The output of the commands gcloud config —help and gcloud help config is identical. Both deliver thorough assistance.

Per-invocation command behaviour is controlled by gcloud Global Flags. Any values defined in SDK properties are overridden by flags.

  • View the environment's configuration list:

Command:

gcloud config list
  • To view each property's settings:

Command:

gcloud config list --all
  • Give a list of your components:

Command:

gcloud components list

The gcloud components that are available for use in this lab are displayed by this command.

Let's look at the details of filtering the command line output.

Task 2. Filtering command line output

A strong tool for command-line work is the gcloud CLI. You could want a certain piece of information presented.

  • List the available compute instances for the project:

Command:

gcloud compute instances list

Output:

NAME: gcelab2
ZONE: 
MACHINE_TYPE: e2-medium
PREEMPTIBLE:
INTERNAL_IP: 10.142.0.2
EXTERNAL_IP: 35.237.43.111
STATUS: RUNNING
  • List the virtual machine for gcelab2:

Command:

gcloud compute instances list --filter="name=('gcelab2')"

Output:

NAME: gcelab2
ZONE: 
MACHINE_TYPE: e2-medium
PREEMPTIBLE:
INTERNAL_IP: 10.142.0.2
EXTERNAL_IP: 35.237.43.111
STATUS: RUNNING

In the command above, we told Gcloud to only display data that matched the requirements, such as the name of a virtual instance.

  • List the project's firewall rules:

Command:

gcloud compute firewall-rules list
  • List the firewall rule for the default network:

Command:

gcloud compute firewall-rules list --filter="network='default'"
  • Display the firewall rules for the default network that match an ICMP rule when the allow rule is applied:

Command:

gcloud compute firewall-rules list --filter="NETWORK:'default' AND ALLOW:'icmp'"

Let's look at the details of connecting to the VM instance.

Task 3. Connecting to your VM instance

It's simple to connect to your instances with gcloud compute. A wrapper for SSH is provided via the gcloud compute ssh command, which handles authentication and the translation of instance names to IP addresses.

  • Run the following command to establish an SSH connection to your VM:

Command:

gcloud compute ssh gcelab2 --zone $ZONE
  • Type Y to proceed.
     
  • Enter twice to leave the passphrase blank.
     
  • The virtual machine's nginx web server installed:

Command:

sudo apt install -y nginx
  • Run the following command to end SSH and the remote shell because you don't need to do anything else:

Command:

exit

You should now be back at the command prompt for your project.

Let's look at the details of updating the firewall.

Task 4. Updating the Firewall

It's crucial to comprehend the relevant firewall regulations while employing compute resources like virtual machines.

  • List the project's firewall policies:

Command:

gcloud compute firewall-rules list

We can see that there are two networks available from the above. The virtual machine gcelab2 is situated on the default network.

  • Visit the nginx service that is active on the gcelab2 virtual machine.
     
  • Give the virtual machine a tag:

Command:

gcloud compute instances add-tags gcelab2 --tags http-server,https-server
  • A firewall rule update to allow:

Command:

gcloud compute firewall-rules create default-allow-http --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=tcp:80 --source-ranges=0.0.0.0/0 --target-tags=http-server
  • List the project's firewall rules:

Command:

gcloud compute firewall-rules list --filter=ALLOW:'80'
  • Check that http connectivity with the virtual machine is possible.

Command:

curl http://$(gcloud compute instances list --filter=name:gcelab2 --format='value(EXTERNAL_IP)')

You will observe the nginx output by default.

Let's look at the commands used for viewing the system logs.

Task 5. Viewing the system logs

Viewing logs is crucial to comprehending how your project is operating. To access the various logs offered by Google Cloud, use gcloud.

  • View the system's accessible logs:

Command:

gcloud logging logs list 
  • View the logs pertaining to the computing resources:

Command:

gcloud logging logs list --filter="compute" 
  • Check the logs for the gce_instance resource type:

Command:

gcloud logging read "resource.type=gce_instance" --limit 5
  • Check out the virtual machine's logs:

Command:

gcloud logging read "resource.type=gce_instance AND labels.instance_name='gcelab2'" --limit 5

Frequently Asked Questions

What is a Cloud shell?

Cloud Shell is a free online environment that includes an online code editor for cloud development and command-line access for controlling your infrastructure.

What is an instance in a VM?

A virtual machine (VM) hosted on Google's infrastructure is referred to as an instance. The Google Cloud app, Google Cloud CLI, or Compute Engine API can all be used to create an instance or a collection of managed instances.

Which command is used to list the available compute instances for the project?

The gcloud compute instances list command is used to list the available compute instances for the project

Conclusion

In this article, we have extensively discussed the details of Cloud Shell and gcloud along with the details of using the gcloud tool and Cloud Shell to connect to computing resources hosted on Google Cloud.

We hope that this blog has helped you enhance your knowledge regarding Cloud Shell and gcloud, and if you would like to learn more, check out our articles on Google Cloud Certification. You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews. Do upvote our blog to help other ninjas grow. Happy Coding!!

Thank You Image
Previous article
Windows Server Instance in GCP
Next article
Set Up Network and HTTP Load Balancers in GCP
Live masterclass