GCP: gcloud compute command-line tool
The gcloud compute
command-line tool enables us to easily manage our
Google Compute Engine resources in a friendlier format than using the
Compute Engine API.
The gcloud
tool is part of the Cloud SDK and is a unified command-line
tool that includes features like statement autocompletion, in-place updating,
extensive man page style help, human-readable and machine-parsable output
formats, and integration with Google Cloud SDK -
gcloud compute
To install gcloud compute
, install the
Google Cloud SDK.
To update it:
$ gcloud components update Your current Cloud SDK version is: 215.0.0 You will be upgraded to version: 334.0.0 ┌─────────────────────────────────────────────────────────────────────────────┐ │ These components will be updated. │ ├─────────────────────────────────────────────────────┬────────────┬──────────┤ │ Name │ Version │ Size │ ├─────────────────────────────────────────────────────┼────────────┼──────────┤ │ BigQuery Command Line Tool │ 2.0.66 │ < 1 MiB │ │ BigQuery Command Line Tool (Platform Specific) │ 2.0.65 │ < 1 MiB │ │ Cloud SDK Core Libraries │ 2021.03.26 │ 17.8 MiB │ │ Cloud SDK Core Libraries (Platform Specific) │ 2021.03.12 │ < 1 MiB │ │ Cloud Storage Command Line Tool │ 4.60 │ 3.9 MiB │ │ Cloud Storage Command Line Tool (Platform Specific) │ 4.59 │ < 1 MiB │ │ gcloud Beta Commands │ 2019.05.17 │ < 1 MiB │ │ gcloud cli dependencies │ 2021.03.26 │ 10.8 MiB │ │ gcloud cli dependencies │ 2020.06.12 │ < 1 MiB │ │ kubectl │ 1.17.17 │ < 1 MiB │ │ kubectl │ 1.17.17 │ 77.8 MiB │ └─────────────────────────────────────────────────────┴────────────┴──────────┘ For the latest full release notes, please visit: https://cloud.google.com/sdk/release_notes Do you want to continue (Y/n)? Y ╔════════════════════════════════════════════════════════════╗ ╠═ Creating update staging area ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: BigQuery Command Line Tool ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: BigQuery Command Line Tool (Platform Sp... ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: Cloud SDK Core Libraries ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: Cloud SDK Core Libraries (Platform Spec... ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: Cloud Storage Command Line Tool ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: Cloud Storage Command Line Tool (Platfo... ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: gcloud Beta Commands ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: gcloud cli dependencies ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: gcloud cli dependencies ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: kubectl ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: kubectl ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: BigQuery Command Line Tool ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: BigQuery Command Line Tool (Platform Spec... ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: Cloud SDK Core Libraries ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: Cloud SDK Core Libraries (Platform Specific) ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: Cloud Storage Command Line Tool ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: Cloud Storage Command Line Tool (Platform... ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: gcloud Beta Commands ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: gcloud cli dependencies ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: gcloud cli dependencies ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: kubectl ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: kubectl ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Creating backup and activating new installation ═╣ ╚════════════════════════════════════════════════════════════╝ Performing post processing steps...done. ==> Start a new shell for the changes to take effect. Update done! To revert your SDK to the previously installed version, you may run: $ gcloud components update --version 215.0.0 $ gcloud -v Google Cloud SDK 334.0.0 beta 2021.03.26 bq 2.0.66 core 2021.03.26 gsutil 4.60 kubectl 1.17.17
Google Compute Engine uses OAuth2 to
authenticate and authorize access. Before we can use gcloud compute
, we
must first authorize the Cloud SDK on our behalf to access our project and
acquire an auth token.
If we are using the gcloud
command-line tool for the first time, gcloud
automatically uses
the default
configuration. For most cases, we only need
the default
configuration.
- Run gcloud init to start the authentication process. Hit enter when prompted.
The command prints a URL and tries to open a browser window to request access to our project. If a browser window can be opened, we will see the following output:
$ gcloud init Welcome! This command will take you through the configuration of gcloud. Your current configuration has been set to: [default] You can skip diagnostics next time by using the following flag: gcloud init --skip-diagnostics Network diagnostic detects and fixes local network connection issues. Checking network connection...done. Reachability Check passed. Network diagnostic (1/1 checks) passed. You must log in to continue. Would you like to log in (Y/n)? Y Your browser has been opened to visit: https://accounts.google.com/o/oauth2/auth?redirect_uri=http%3A%2F%2Flocalhost%3A8085%2F&prompt=select_account&response_type=code&client_id=32555940559.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&access_type=offline
You are logged in as: [kihyuck.hong@gmail.com]. Pick cloud project to use: [1] djangotest-sfvue [2] epicmath-local [3] linuxvm-216022 [4] mykubernetesproject-164618 [5] pytato-142916 [6] xophist-1248 [7] youtubeinfo-1224 [8] Create a new project Please enter numeric choice or text value (must exactly match list item): 3 ...
Use the instances create command to create a new instance. For example, the following command creates an instance named "my-instance" in the "us-central1-a" zone:
$ gcloud compute instances create my-instance Created [https://www.googleapis.com/compute/v1/projects/linuxvm-216022/zones/us-east1-c/instances/my-instance]. NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS my-instance us-east1-c n1-standard-1 10.142.0.2 35.229.21.208 RUNNING
If we omit the "--zone" flag, gcloud can infer our desired zone based on our default properties.
Other required instance settings, like machine type and image, if not specified in the create command, are set to default values. We can see the default values by displaying help for the create command:
$ gcloud compute instances create --help
To list instances:
$ gcloud compute instances list NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS my-instance us-east1-c n1-standard-1 10.142.0.2 35.229.21.208 RUNNING
To create an instance with specific machine type, image family, and resion. We need to get the list:
$ gcloud compute machine-types list | grep -E 'e2-.*us-east4-c' WARNING: The following filter keys were not present in any resource : deprecated.state e2-highcpu-16 us-east4-c 16 16.00 e2-highcpu-2 us-east4-c 2 2.00 e2-highcpu-32 us-east4-c 32 32.00 e2-highcpu-4 us-east4-c 4 4.00 e2-highcpu-8 us-east4-c 8 8.00 e2-highmem-16 us-east4-c 16 128.00 e2-highmem-2 us-east4-c 2 16.00 e2-highmem-4 us-east4-c 4 32.00 e2-highmem-8 us-east4-c 8 64.00 e2-medium us-east4-c 2 4.00 e2-micro us-east4-c 2 1.00 e2-small us-east4-c 2 2.00 e2-standard-16 us-east4-c 16 64.00 e2-standard-2 us-east4-c 2 8.00 e2-standard-32 us-east4-c 32 128.00 e2-standard-4 us-east4-c 4 16.00 e2-standard-8 us-east4-c 8 32.00 $ gcloud compute images list NAME PROJECT FAMILY DEPRECATED STATUS centos-7-v20210316 centos-cloud centos-7 READY centos-8-v20210316 centos-cloud centos-8 READY centos-stream-8-v20210316 centos-cloud centos-stream-8 READY cos-77-12371-1109-0 cos-cloud cos-77-lts READY cos-81-12871-1245-15 cos-cloud cos-81-lts READY cos-85-13310-1209-17 cos-cloud cos-85-lts READY cos-beta-89-16108-403-7 cos-cloud cos-beta READY cos-dev-93-16295-0-0 cos-cloud cos-dev READY debian-10-buster-v20210316 debian-cloud debian-10 READY debian-9-stretch-v20210316 debian-cloud debian-9 READY ...
Then, create it:
$ gcloud compute instances create my-instance-2 \ --machine-type=e2-micro --image-family=debian-10 --image-project=debian-cloud \ --zone=us-east4-c Created [https://www.googleapis.com/compute/v1/projects/hello-kube-231718/zones/us-east4-c/instances/my-instance-2]. NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS my-instance-2 us-east4-c e2-micro 10.150.0.2 35.245.189.144 RUNNING
To delete the instance:
$ gcloud compute instances delete my-instance-2 --zone=us-east4-c The following instances will be deleted. Any attached disks configured to be auto-deleted will be deleted unless they are attached to any other instances or the `--keep-disks` flag is given and specifies them for keeping. Deleting a disk is irreversible and any data on the disk will be lost. - [my-instance-2] in [us-east4-c] Do you want to continue (Y/n)? y Deleted [https://www.googleapis.com/compute/v1/projects/hello-kube-231718/zones/us-east4-c/instances/my-instance-2].
The gcloud compute ssh command provides wrappers around around SSH, which takes care of authentication and the mapping of instance name to IP address.
to ssh in to "my-instance" in the "us-east1-c" zone, we can use:
$ gcloud compute ssh my-instance --zone us-east1-c Updating project ssh metadata...⠶Updated [https://www.googleapis.com/compute/v1/projects/linuxvm-216022]. Updating project ssh metadata...done. Waiting for SSH key to propagate. Warning: Permanently added 'compute.4873874219097322680' (ECDSA) to the list of known hosts. Linux my-instance 4.9.0-8-amd64 #1 SMP Debian 4.9.110-3+deb9u3 (2018-08-19) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. kihyuckhong@my-instance:~$
To copy the local file "file-1" to "my-instance" in the "us-east1-c" zone, we can use:
$ gcloud compute scp ~/file-1 my-instance:~/test --zone us-east1-c file-1
kihyuckhong@my-instance:~/test$ ls file-1
The scp command can also be used to copy files from an instance to our local machine. For example, to create a local copy of "file-1", which is on the instance "my-instance" in the "us-east1-c" zone, we can use:
$ gcloud compute scp my-instance:~/test/file-2 ~/ --zone us-east1-c file-2
Both the gcloud compute ssh and gcloud compute scp commands, by default, use the private key file located at "~/.ssh/google_compute_engine".
$ ls ~/.ssh google_compute_engine google_compute_engine.pub google_compute_known_hosts known_hosts
To set default zone and region in our local client. We can manually choose a different zone or region without updating the metadata server by setting these properties locally on our gcloud client.
We can change the default zone and region in our metadata server by making a request to the metadata server. For example:
$ gcloud compute project-info add-metadata \ --metadata google-compute-default-region=us-east1,google-compute-default-zone=us-east1-c Updated [https://www.googleapis.com/compute/v1/projects/linuxvm-216022].
GCP (Google Cloud Platform)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization