gcloud CLI: A Beginner's Guide

실습 10분 universal_currency_alt 무료 show_chart 입문
info 이 실습에는 학습을 지원하는 AI 도구가 통합되어 있을 수 있습니다.
이 콘텐츠는 아직 휴대기기에 최적화되지 않음
최상의 경험을 위해 데스크톱 컴퓨터에서 이메일로 전송된 링크를 사용하여 방문하세요.

GSP693

Google Cloud self-paced labs logo

Overview

In this hands-on lab, you learn how to connect to computing resources hosted on Google Cloud via gcloud, Google Cloud's CLI tool.

You are encouraged to type the commands themselves, which reinforces the core concepts. This lab uses code blocks that contain 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 learn to do

  • Practice using gcloud commands.
  • Connect to compute services hosted on Google Cloud.

Setup and requirements

  • Labs are timed and cannot be paused. The timer starts when you click Start Lab.
  • The included cloud terminal is preconfigured with the gcloud SDK.
  • Use the terminal to execute commands and then click Check my progress to verify your work.

Pre-configured resource:

You have a pre-configured VM instance named gcelab2 in the default network for this lab.

Throughout the lab, you will use the zone:

  • Create an environment variable to store your zone:

    export ZONE={{{project_0.default_zone | ZONE}}}

Task 1. Connecting to your VM instance

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.

SSH stands for Secure Shell. It is a network protocol that allows you to securely access and manage a virtual machine (VM).

  1. To connect to your VM with SSH in a specific zone, run the following command:

    gcloud compute ssh gcelab2 --zone $ZONE

    Output:

    WARNING: The private SSH key file for gcloud does not exist. WARNING: The public SSH key file for gcloud does not exist. WARNING: You do not have an SSH key for gcloud. WARNING: SSH keygen will be executed to generate a key. Generating public/private rsa key pair. Enter passphrase (empty for no passphrase):
  2. In a production environment you should set a passphrase, but for this lab it is not required. Leave the passphrase empty by pressing Enter twice.

  3. You have connected to the virtual machine pre-created for the lab.

    Did you notice how the command prompt changed?

    The prompt now says something similar to sa_xxxxxxxxxxxxxxxxxxxx@gcelab2

    • The reference before the @ sign indicates the account being used.
    • After the @ sign indicates the host machine being accessed.
  4. Install nginx web server on to the virtual machine:

    sudo apt install -y nginx
  5. You don't need to do anything here. 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 2. Updating the firewall

When using compute resources such as virtual machines, its important to understand the associated firewall rules.

  1. List the firewall rules for the project:

    gcloud compute firewall-rules list

    Output:

    NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED default-allow-icmp default INGRESS 65534 icmp False default-allow-internal default INGRESS 65534 tcp:0-65535,udp:0-65535,icmp False default-allow-rdp default INGRESS 65534 tcp:3389 False default-allow-ssh default INGRESS 65534 tcp:22 False

    From the above you can see the default networks available, where the virtual machine gcelab2 is located.

  2. Try to access the nginx service running on the gcelab2 virtual machine.

    Send HTTP request using cURL to the nginx web server and see if the server responds:

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

    The nginx server will not respond and you will see a frozen remote shell. Press Ctrl-c to stop cURL.

    Communication with the virtual machine will fail as it does not have an appropriate firewall rule. Nginx uses port 80 for HTTP traffic by default. The nginx web server is expecting to communicate on tcp:80.

    To get communication working you need to updated a firewall rule which allows incoming traffic on TCP port 80 from any source targeting gcelab2 virtual machine.

  3. Update the firewall rule to allow:

    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

    Notice --target-tags=http-server in the above command. This firewall rule applies only to instances that have the http-server network tag, which means that incoming traffic on port 80 would be allowed to those instances.

  4. Add the http-server network tag to the gcelab2 virtual machine:

    gcloud compute instances add-tags gcelab2 --tags http-server --zone $ZONE
  5. List the firewall rules for the project:

    gcloud compute firewall-rules list --filter=ALLOW:'80'

    Output:

    NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED default-allow-http default INGRESS 1000 tcp:80 False
  6. List instances that are tagged with the http-server network tag:

    gcloud compute instances list --filter='tags:http-server'

    You can see the 'gcelab2' virtual machine listed.

  7. Verify communication is possible for http to the virtual machine:

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

    You can see the default nginx output.

    Click Check my progress to verify the objective. Update the firewall.

Task 3. Viewing the system logs

Viewing logs is essential to understanding how your project works. Use gcloud to access the different logs available on Google Cloud.

  1. View the available logs on the system:

    gcloud logging logs list

    Output:

    NAME: projects/qwiklabs-xxx-xx-xxxxxxxxxxxx/logs/GCEGuestAgent NAME: projects/qwiklabs-xxx-xx-xxxxxxxxxxxx/logs/OSConfigAgent NAME: projects/qwiklabs-xxx-xx-xxxxxxxxxxxx/logs/cloudaudit.googleapis.com%2Factivity NAME: projects/qwiklabs-xxx-xx-xxxxxxxxxxxx/logs/cloudaudit.googleapis.com%2Fdata_access NAME: projects/qwiklabs-xxx-xx-xxxxxxxxxxxx/logs/cloudaudit.googleapis.com%2Fsystem_event NAME: projects/qwiklabs-xxx-xx-xxxxxxxxxxxx/logs/compute.googleapis.com%2Fshielded_vm_integrity NAME: projects/qwiklabs-xxx-xx-xxxxxxxxxxxx/logs/diagnostic-log
  2. View the logs that relate to compute resources:

    gcloud logging logs list --filter="compute"

    Output:

    NAME: projects/qwiklabs-xxx-xx-xxxxxxxxxxxx/logs/compute.googleapis.com%2Fshielded_vm_integrity
  3. Read the logs related to the resource type of gce_instance:

    gcloud logging read "resource.type=gce_instance" --limit 5
  4. Read the logs for a specific virtual machine:

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

Congratulations!

You learned how to launch cloud terminal and run some sample gcloud commands.

Next steps / Learn more

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated January 9, 2024

Lab Last Tested November 12, 2024

Copyright 2026 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

시작하기 전에

  1. 실습에서는 정해진 기간 동안 Google Cloud 프로젝트와 리소스를 만듭니다.
  2. 실습에는 시간 제한이 있으며 일시중지 기능이 없습니다. 실습을 종료하면 처음부터 다시 시작해야 합니다.
  3. 화면 왼쪽 상단에서 실습 시작을 클릭하여 시작합니다.

시크릿 브라우징 사용

  1. 실습에 입력한 사용자 이름비밀번호를 복사합니다.
  2. 비공개 모드에서 콘솔 열기를 클릭합니다.

콘솔에 로그인

    실습 사용자 인증 정보를 사용하여
  1. 로그인합니다. 다른 사용자 인증 정보를 사용하면 오류가 발생하거나 요금이 부과될 수 있습니다.
  2. 약관에 동의하고 리소스 복구 페이지를 건너뜁니다.
  3. 실습을 완료했거나 다시 시작하려고 하는 경우가 아니면 실습 종료를 클릭하지 마세요. 이 버튼을 클릭하면 작업 내용이 지워지고 프로젝트가 삭제됩니다.

현재 이 콘텐츠를 이용할 수 없습니다

이용할 수 있게 되면 이메일로 알려드리겠습니다.

감사합니다

이용할 수 있게 되면 이메일로 알려드리겠습니다.

한 번에 실습 1개만 가능

모든 기존 실습을 종료하고 이 실습을 시작할지 확인하세요.

시크릿 브라우징을 사용하여 실습 실행하기

이 실습을 실행하는 가장 좋은 방법은 시크릿 모드 또는 시크릿 브라우저 창을 사용하는 것입니다. 개인 계정과 학생 계정 간의 충돌로 개인 계정에 추가 요금이 발생하는 일을 방지해 줍니다.