Thursday, January 24, 2019

gcloud cheat sheet

This article records the common gcloud commands.
Please refer to the gcloud doc.

1. Quick Start

1.1 Install google cloud SDK on Mac

Follow https://cloud.google.com/sdk/docs/quickstart-macos 

1.2 Initialize SDK

gcloud init --console-only

1.3 List accounts with credentials

$ gcloud auth list
           Credentialed Accounts
ACTIVE             ACCOUNT
*                  xxx@xxx.com

To set the active account, run:
    $ gcloud config set account `ACCOUNT`

1.4 View information of SDK installation and configuration

gcloud info

1.5 Help manual for gcloud

# Help for command "gcloud compute instances create":
gcloud help compute instances create

2. Configurations and Properties

2.1 List all configurations

$ gcloud config configurations list
NAME     IS_ACTIVE  ACCOUNT        PROJECT        DEFAULT_ZONE  DEFAULT_REGION
default  True       xxx@xxx.com    myprojectname  us-west1-a

2.2 Create a new configuration named "myconfig"

$ gcloud config configurations create myconfig
Created [myconfig].
Activated [myconfig].

2.3 Activate configuration named "myconfig"

$ gcloud config configurations activate myconfig
Activated [myconfig].

2.4 Switch configuration for a single command, just use --configuration flag.

gcloud auth list --configuration=[CONFIGURATION_NAME]

2.5 Delete a configuration named "myconfig"

$ gcloud config configurations delete myconfig
The following configurations will be deleted:
 - myconfig
Do you want to continue (Y/n)?  Y

Deleted [myconfig].
Note: you can not delete an active configuration.

2.6 View properties in the active configuration

$ gcloud config list
[core]
account = xxx@xxx.com
disable_usage_reporting = True
project = myprojectname

Your active configuration is: [default]

2.7 View ALL properties in the active configuration

gcloud config list --all

2.8 View properties in some other configuration

gcloud config configurations describe [CONFIGURATION_NAME]
Note: the properties are documented here.

2.9 Change default project

gcloud config set project [PROJECT_ID]

2.10 Change default compute zone

gcloud compute zones list
gcloud config set compute/zone us-west1-a
Note: here "compute" is the section name while "zone" is the property name.

2.11 Unset a property

gcloud config unset disable_usage_reporting

2.12 List all available properties

gcloud topic configurations

3 Components

3.1 List all components

gcloud components list

3.2 Install a component

gcloud components install [COMPONENT-ID]

3.3 Update all installed components to latest version

gcloud components update
Or to revert SDK to the previously installed version, say 228.0.0:
gcloud components update --version 228.0.0

3.4 Remove a component

gcloud components remove [COMPONENT-ID]

4. Account

4.1 List accounts whose credentials are stored on the local system

gcloud auth list

4.2 Switch the active account

gcloud config set account [ACCOUNT]

4.3 Revoke credentials for the account

gcloud auth revoke [ACCOUNT]

5. Compute Engine

5.1 List all VM instances

gcloud compute instances list
Or to list the instances with some pattern matched:
gcloud compute instances list --filter="name~'my-.*'"

 5.2 Create a VM instance

gcloud compute instances create my-instance

5.3 Show information about one VM instance

gcloud compute instances describe my-instance --zone us-central1-a

5.4 ssh to a VM instance

gcloud compute ssh my-instance --zone us-central1-a

5.5 scp a file to a VM instance

gcloud compute scp ~/file-1 my-instance:~/remote-destination --zone us-central1-a

5.6 scp a file from a VM instance

gcloud compute scp my-instance:~/file-1 ~/local-destination --zone us-central1-a

5.7 Generate a ssh configuration for ssh/scp to use directly

gcloud compute config-ssh
ssh my-instance.us-central1-a.myproject
Note, the ssh configuration is stored here : ~/.ssh/config

5.8 Add or Remove instance metadata 

gcloud compute instances add-metadata my-instance \
    --zone us-central1-a \
    --metadata role=worker
    
gcloud compute instances remove-metadata my-instance \
    --zone us-central1-a \
    --keys role

5.9 View, Add or Remove project level metadata

gcloud compute project-info describe

gcloud compute project-info add-metadata \
    --metadata-from-file startup-script=/local/path/to/script
    --metadata startup-id=1234
    
gcloud compute project-info remove-metadata --keys startup-script startup-id

5.10 Delete a VM instance

gcloud compute instances delete my-instance --zone us-central1-a

5.11 List operations

gcloud compute operations list
gcloud compute operations list --zones us-central1-a,us-central1-b

6. Container

6.1 Create a GKE cluster

gcloud container clusters create [CLUSTER_NAME]
It will create a 3 nodes(compute engine) cluster by default.

6.2 Delete a GKE cluster

gcloud container clusters delete [CLUSTER_NAME]

==

No comments:

Post a Comment

Popular Posts