Gcloud
Command line interface to interact with Google Cloud Resources
Most GCP services can be managed from CLI using Gcloud:
- Compute Engine Virtual Machines
- Managed Instance Group
- Databases
- and… mant more
You can create/delete/update/read existing resources and perform actions life deployment as well!
(REMEMBER) SOME GCP services have specific CLI tools
- Cloud Storage – gsutil
- Cloud BigQuery – bq
- Cloud Bigtable – cbt
- Kubernetes – kubectl(in addition to Gcloud which is used to manage clusters)
Gloud – Getting Started
Installation
- Gcloud is part of Google Cloud SDK
- Cloud SDK requires Python
- Instructions to install Cloud SDK (and Gcloud) =>
https://cloud.google.com/sdk/docs/install
- You can also use Gcloud on Cloud Shell
Connecting to GCP
gcloud init– initialize or reinitialize gcloud- Authorize gcloud to use your user account credentials
- Setup configuration
- includes current project, default zone etc.
- gcloud config list – list all properties of the active configuration
gcloud config list project
gcloud config configurations list
gcloud config configurations activate my-default-configuration
gcloud config list
gcloud config configurations describe my-second-configuration
gcloud compute instances list
gcloud compute instances create
gcloud compute instances create my-first-instance-from-gcloud
gcloud compute instances describe my-first-instance-from-gcloud
gcloud compute instances delete my-first-instance-from-gcloud
gcloud compute zones list
gcloud compute regions list
gcloud compute machine-types list
gcloud compute machine-types list --filter zone:asia-southeast2-b
gcloud compute machine-types list --filter "zone:(asia-southeast2-b asia-southeast2-c)"
gcloud compute zones list --filter=region:us-west2
gcloud compute zones list --sort-by=region
gcloud compute zones list --sort-by=~region
gcloud compute zones list --uri
gcloud compute regions describe us-west4
gcloud compute instance-templates list
gcloud compute instance-templates create instance-template-from-command-line
gcloud compute instance-templates delete instance-template-from-command-line
gcloud compute instance-templates describe my-instance-template-with-custom-image
gcloud config set
- Sets the specified property in your active configuration
- gcloud config set core/project VALUE
- gcloud config set compute/region VALUE
- gcloud config set compute/zone VALUE
- gcloud config set core/verbosity VALUE(debug)
- Syntax – gcloud config set SECTION/PROPERTY VALUE
- core, compute – SECTIONS
- project, region, zone – PROPERTIES
- Specifying core is optional as it i s the default SECTION!
- gloud config set project VALUE
- gloud config set verbosity VALUE (debug)
- Get more details with gcloud config set –help
- Look for AVAILABLE PROPERTIES in the content
- Opposite – gloud config unset
Gcloud – managing multiple configurations
- Scenario: You are working on multiple projects from the same machine. You would want to be able to execute commands using different configurations.
- How do you simplify this?
- Gcloud config configurations create/delete/describe/active/list
- create new configuration: gcloud config configuartions create NAME (dev)
- Activate specific configuration: gcloud config configurations active NAME (dev)
- list Configurations: gcloud config configurations list
- Gcloud config configurations create/delete/describe/active/list
- How do you simplify this?
gcloud command structure – Playing with Services
- gcloud GROUP SUBGROUP ACTION
- GROUP – config or compute or container or dataflow or functions or i am or ..
- Which service group are you playing with?
- SUBGROUP – instances or images or instance-templates or machine-types or regions or zones
- Which sub group of the service do you want to play with?
- ACTION – create or list or start to stop or describe or …
- What do you want to do?
- GROUP – config or compute or container or dataflow or functions or i am or ..
- Example:
- gcloud compute instances list
- gcloud compute zones list
- gcloud compute regions list
- gcloud compute machine-types list
- gcloud compute machine-types list –filter-“zone:us-centeral-b”
- gcloud compute machine-types list –filter=”zone:(us-central1-b europe-west1-d)”
gcloud compute instances create
- Create compute instances
- `gcloud compute instances create [NAME]
Compute Instances – Default Region and Zone
Three Options:
- Option 1: (Centralize Configuration): gcloud compute project-info add metadata
- `–metadata-[google-compute-default-region=REGION | google-compute-default-zone=ZONE
- Option 2: (Local gcloud configuration): gcloud config set compute/region REGION
- Option 3: (Command Specific): –zone in the command
Priority: Option 3 overrides Option 2 overrides option 1
List and Describe commands
- Typically list commands are used to list a set of resources
- gcloud compute RESOURCES list
- gcloud compute images/regions/zones/ disk-types list
- gcloud compute instances/disk/snapshots list
- Most list commands support a few common options
- `–filter=”zone:VALUE”
- `–sort-by (NAME, ~NAME)
- –uri
- gcloud compute images list –sort-by NAME –filter “PROJECT: (window-cloud ubuntu-os-cloud)”
- gcloud compute RESOURCES list
- Typically describe commands are used to describe a specific resource
- gcloud compute images describe ubuntu-1604-xenial-v202210203 –project ubuntu-os-cloud
- gcloud compute regions describe-us-central1
Playing with Compute Instances – gcloud
Playing with compute instances
gcloud compute instances list/start/stop/delete/reset/describe
Playing with Instance Templates
gcloud compute instance-templates list
How to create a VM from instances
gcloud compute instances create my-test-vm --source-instance-template="instance-name