Published 10月 30, 2021 by with 0 comment

GSP002 - Getting Started with Cloud Shell and gcloud



Google Could Self-Paced (GSP) Labs 002 - Getting Started with Cloud Shell and gcloud

Overview
Cloud Shell provides you with command-line access to computing resources hosted on Google Cloud. Cloud Shell is a Debian-based virtual machine with a persistent 5-GB home directory, which makes it easy for you to manage your Google Cloud projects and resources. The gcloud command-line tool and other utilities you need are pre-installed in Cloud Shell, which allows you to get up and running quickly.
In this hands-on lab, you learn how to connect to computing resources hosted on Google Cloud via Cloud Shell with the gcloud tool.
You are encouraged to type the commands themselves, which reinforces the core concepts. Many labs will include a code block that contains the required commands. You can easily copy and paste the commands from the code block into the appropriate places during the lab.

What you'll do
Practice using gcloud commands.
Connect to compute services hosted on Google Cloud.


Prerequisites
Familiarity with standard Linux text editors such as vim, emacs, or nano.


Source:
This lab is from Qwiklabs.


Task 1: Sign in to the Google Cloud Platform (GCP) Console

Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.
Click Continue.
It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
You can list the active account name with this command:
gcloud auth list

You can list the project ID with this command:
gcloud config list project

After Cloud Shell is activated, you can use the command line to invoke the Cloud SDK gcloud tool or other tools available on the virtual machine instance. Later in the lab, you will use your $HOME directory, which is used in persistent disk storage to store files across projects and between Cloud Shell sessions. Your $HOME directory is private to you and cannot be accessed by other users.


Task 2: Configure your environment
In this section, you'll learn about aspects of the development environment that you can adjust.

Understanding regions and zones
Certain Google Compute Engine resources live in regions or zones. A region is a specific geographical location where you can run your resources. Each region has one or more zones. For example, the us-central1 region denotes a region in the Central United States that has zones us-central1-a, us-central1-b, us-central1-c, and us-central1-f. The following image shows zones in their respective regions:


Resources that live in a zone are referred to as zonal resources. Virtual machine instances and persistent disks live in a zone. If you want to attach a persistent disk to a virtual machine instance, both resources must be in the same zone. Similarly, if you want to assign a static IP address to an instance, the instance must be in the same region as the static IP address.
To see what your default region and zone settings are, run the following commands:
gcloud config get-value compute/zone
gcloud config get-value compute/region
If the google-compute-default-region or google-compute-default-zone responses are (unset), that means no default zone or region is set.

Identify your default region and zone
1. Copy your project ID to your clipboard or text editor. The project ID is listed in 2 places:
In the Google Cloud Console, on the Dashboard, under Project info. (Click Navigation menu (Navigation menu), and then click Home > Dashboard.)

2. In Cloud Shell, run the following gcloud command, replacing <your_project_ID> with the project ID you copied:
gcloud compute project-info describe --project <your_project_ID>

Find the default zone and region metadata values in the output. You'll use the zone (google-compute-default-zone) from the output later in this lab.

If the google-compute-default-region and google-compute-default-zone keys and values are missing from the output, no default zone or region is set.

Set environment variables
Environment variables define your environment and help save time when you write scripts that contain APIs or executables.

1. Create an environment variable to store your Project ID, replacing <your_project_ID> with the value for name from the gcloud compute project-info describe command you ran earlier:
export PROJECT_ID=<your_project_ID>

2. Create an environment variable to store your Zone, replacing <your_zone> with the value for zone from the gcloud compute project-info describe command you ran earlier:
export ZONE=<your_zone>

3. To verify that your variables were set properly, run the following commands:
echo $PROJECT_ID
echo $ZONE

If the variables were set correctly, the echo commands will output your Project ID and Zone.

Create a virtual machine with the gcloud tool
Use the gcloud tool to create a new virtual machine (VM) instance.

1. To create your VM, run the following command:
gcloud compute instances create gcelab2 --machine-type n1-standard-2 --zone $ZONE

Command details
gcloud compute allows you to manage your Compute Engine resources in a format that's simpler than the Compute Engine API.
instances create creates a new instance.
gcelab2 is the name of the VM.
The --machine-type flag specifies the machine type as n1-standard-2.
The --zone flag specifies where the VM is created.
If you omit the --zone flag, the gcloud tool can infer your desired zone based on your default properties. Other required instance settings, such as machine type and image, are set to default values if not specified in the create command.
To open help for the create command, run the following command:
gcloud compute instances create --help

Note: Press ENTER or the spacebar to scroll through the help content. To exit the content, type Q.

Explore gcloud commands
The gcloud tool offers simple usage guidelines that are available by adding the -h flag (for help) onto the end of any gcloud command.
1. Run the following command:
gcloud -h

You can access more verbose help by appending the --help flag onto a command or running the gcloud help command.

2. Run the following command:
gcloud config --help

Note: Press ENTER or the spacebar to scroll through the help content. To exit the content, type Q.

3. Run the following command:
gcloud help config

The results of the gcloud config --help and gcloud help config commands are equivalent. Both return long, detailed help.

Note: Press ENTER or the spacebar to scroll through the help content. To exit the content, type Q.
gcloud Global Flags govern the behavior of commands on a per-invocation level. Flags override any values set in SDK properties.

4. View the list of configurations in your environment:
gcloud config list

5. To see all properties and their settings:
gcloud config list --all

6. List your components:
gcloud components list

This command displays the gcloud components that are ready for you to use in this lab.


Task 3: Install a new component
Next, you'll install a gcloud component that makes working in the gcloud tool easier.

Auto-complete mode
gcloud interactive has auto prompting for commands and flags and displays inline help snippets in the lower section of the pane as the command is typed.

You can use dropdown menus to auto-complete static information, such as command and sub-command names, flag names, and enumerated flag values.

1. Install the beta components:
sudo apt-get install google-cloud-sdk

2. Enable the gcloud interactive mode:
gcloud beta interactive
When using the interactive mode, press TAB to complete file path and resource arguments. If a dropdown menu appears, press TAB to move through the list, and press the spacebar to select your choice.

3.To try this feature, start typing the following command, and use auto-complete to replace <your_vm> with an existing VM in your project:
gcloud compute instances describe <your_vm>
A list of commands is displayed below the Cloud Shell pane. Pressing F2 toggles the active help section to ON or OFF.

4. To exit from the interactive mode, run the following command:
exit


Task 4: Connect to your VM instance with SSH
gcloud compute makes connecting to your instances easy. The gcloud compute ssh command provides a wrapper around SSH, which takes care of authentication and the mapping of instance names to IP addresses.

1. To connect to your VM with SSH, run the following command:
gcloud compute ssh gcelab2 --zone $ZONE

2. To continue, type Y.

3. To leave the passphrase empty, press ENTER twice.

4. You don't need to do anything here, so to disconnect from SSH and exit the remote shell, run the following command:
exit

You should be back at your project's command prompt.

Task 5: Use the Home directory
Now try out your Home directory. The contents of your Cloud Shell Home directory persist across projects between all Cloud Shell sessions, even after the virtual machine is terminated and restarted.

1. Change your current working directory:
cd $HOME

2. Open your .bashrc configuration file by using the vi text editor:
vi ./.bashrc
The editor opens and displays the contents of the file.

3. To exit the editor, press ESC, then type :wq, and then press Enter.


Congratulations!
You learned how to launch Cloud Shell and run some sample gcloud commands.


Reference:
1. Qwiklabs

2. Google Cloud Certification - Associate Cloud Engineer

3. Read the Google Cloud Platform documentation on Regions and zones.

4. Read the Google Cloud Platform documentation on gcloud tool overview.


最初發表 / 最後更新: 2021.10.30 / 2021.10.30

0 comments:

張貼留言