The gcloud compute command group lets you create, configure, and manipulate Compute Engine virtual machine (VM) instances.
Ref: https://cloud.google.com/sdk/gcloud/reference/compute
List projects, regions, zones, VMs:
gcloud projects list
gcloud compute zones list
gcloud compute regions list
gcloud compute instances list
Get values:
gcloud config get-value project
gcloud config get-value compute/zone
gcloud config get-value compute/region
Metadata server
Some gcloud compute commands require --project
, --region
or --zone
flags. Instead of specifying these flags each time you run commands, you can set a default region and zone for your project.
The default region and zone in the metadata server apply to all configurations in a project.
Set the default region, zone:
gcloud compute project-info add-metadata --metadata google-compute-default-region= us-central1, google-compute-default-zone= us-central1-b
Then, Initialize the change in your local client:
gcloud init
Unset Metadata server
gcloud compute project-info remove-metadata –keys=google-compute-default-region, google-compute-default-zone
Set/unset the default object:
gcloud config set <object> <object_id>
gcloud config unset <object>
where object:
- project
- compute/region
- compute/zone
Using the Environment variables with set
command to set the project, region and zone variables.
set CLOUDSDK_CORE_PROJECT=PROJECT_ID
set CLOUDSDK_COMPUTE_REGION=REGION_ID
set CLOUDSDK_COMPUTE_ZONE=ZONE_ID
and unset:
set CLOUDSDK_CORE_PROJECT=
set CLOUDSDK_COMPUTE_REGION=
set CLOUDSDK_COMPUTE_ZONE=
Working with VMs
Creating VMs:
gcloud compute instances create VM_NAME [--image IMAGE | --image-family IMAGE_FAMILY] --image-project IMAGE_PROJECT
where
- VM_NAME: the name of the new VM
- IMAGE or IMAGE_FAMILY: Refer https://cloud.google.com/compute/docs/images/os-details#general-info
- IMAGE_PROJECT: the project containing the image.
More options: https://cloud.google.com/compute/docs/instances/create-start-instance
Example:
Create a VM with overwriting the default vars:
- named catdcwebt1,
- zone us-central1-b,
- OS windows-server-2019-dc-v20210413,
- 4 vCPU,
- 16384 = 16 Gb of RAM,
- 150Gb of SSD
- Allow http, https
gcloud compute --project=nrd-test-311308 instances create catdcwebt1 --zone=us-central1-b --machine-type=n2-custom-4-16384 --subnet=default --tags=http-server,https-server --image=windows-server-2019-dc-v20210413 --image-project=windows-cloud --boot-disk-size=150GB --boot-disk-type=pd-ssd --boot-disk-device-name= catdcwebt1
Starting, Stopping VMs:
gcloud compute instances start|stop VM_NAME
Resetting Windows Password Logon:
gcloud compute --project PROJECT_ID reset-windows-password VM_NAME --zone ZONE_ID
Example:
gcloud compute --project "nrd-test-311308" reset-windows-password "catdcwebt1" --zone "us-central1-b"
Connecting to VMs:
Using SSH to connect to instances:
gcloud compute ssh VM_NAME
Example:
gcloud compute ssh --zone "us-central1-b" "catdcwebt1" --tunnel-through-iap --project "tdc-test-311308"
Port forwarding over SSH
gcloud compute ssh catdcwebt1 --project tdc-test-311308 --zone us-central1-b -- [-L 2222:localhost:8888]
where:
2222
is the local port you’re listening on.8888
is the remote port you’re connecting to.
IAP for TCP forwarding:
Starts a tunnel to Cloud IAP (Identity-Aware Proxy) for TCP forwarding RDP port 3389
on the VM and turned it to the localhost with port 1975
:
gcloud compute start-iap-tunnel catdcwebt1 3389 --local-host-port=localhost:1975
Transfer files
To transfer files using SCP, create a firewall rule that allows SSH connections on port 22.
Copy files to a VM:
gcloud compute scp LOCAL_FILE_PATH VM_NAME:REMOTE_DIRECTORY
Download files from a VM:
gcloud compute scp VM_NAME:REMOTE_DIRECTORY LOCAL_FILE_PATH
More options: https://cloud.google.com/compute/docs/instances/transfer-files#transfergcloud
2021/09/25 at 4:39 am
Last modified on October 9th, 2024 at 11:47 pm
Nam Le
lequocnam
0 responds