Skip to content

Instantly share code, notes, and snippets.

@gsusI
Last active April 29, 2024 17:52
Show Gist options
  • Save gsusI/c078f793a014f7b295395b86817eb9b2 to your computer and use it in GitHub Desktop.
Save gsusI/c078f793a014f7b295395b86817eb9b2 to your computer and use it in GitHub Desktop.

Google Cloud Platform Command line cheat sheet

This is just the formatted version of the post originally created by Sarath Tamminana in Medium: Google Cloud Platform Command line cheat sheet


Authorization and Access related commands


  1. To authenticate a user account with gcloud and minimal user output

    gcloud auth login  \
        --brief
  2. To list all credentialed accounts and identify the current active account

    gcloud auth list
  3. To revoke credentials for a user account (like logging out)

    gcloud auth revoke [email protected]
  4. To authorize gcloud to access Google Cloud Platform using an existing service account while also specifying a project

    gcloud auth activate-service-account [email protected]  \
        --key-file=/path/key.json  \
        --project=sampleproject
  5. To set an existing account to be the current active account, run:

    gcloud config set core/account [email protected]
  6. If you don’t have an existing account, create one using:

    gcloud init
  7. To list the active account name:

    gcloud auth list  \
        --filter=status:ACTIVE  \
        --format=”value(account)”
  8. To list the inactive account names with prefix test:

    gcloud auth list  \
        --filter=”-status:ACTIVE account:test*”  \
        --format=”value(account)”
  9. To obtain access credentials for your user account

    gcloud auth login
  10. To list down the service accounts

    gcloud iam service-accounts list
  11. To create a role

    gcloud iam roles create
  12. To list down the roles

    gcloud iam roles list
  13. To list down the projects

    gcloud projects list
  14. To describe the roles

    gcloud iam roles describe
  15. To create a configuration

    gcloud config configurations create quantiphi
  16. To list down the configurations

    gcloud config configurations list
  17. To activate the configuration

    gcloud config configurations activate quantiphi
  18. To set an account

    gcloud config set account [email protected]
  19. To list down the projects

    gcloud projects list
  20. To set the projects

    gcloud config set project quantiphi-project
  21. To list down all the active configurations

    gcloud config list
  22. To set the region

    gcloud config set compute/region us-west1
  23. To set the zone

    gcloud config set compute/zone us-west1-a
  24. To list down the regions

    gcloud compute regions list
  25. To list down the zones using filter

    gcloud compute zones list  \
        --filter=region:us-central1
  26. To get the IAM policy into an yaml file

    gcloud projects get-iam-policy (project_id) > filename.yaml
  27. To set the IAM policy using yaml file

    gcloud projects set-iam-policy project_id filename.yaml
  28. To bind an IAM policy to a specific user

    gcloud projects add-iam-policy-binding project_id  \
        --member user:[email protected]  \
        --role roles/editor
    

Billing


  1. To list billing accounts

    gcloud beta billing
  2. To link a billing account with a project

    gcloud alpha billing projects link my-project  \
        --billing-account 0X0X0X-0X0X0X-0X0X0X

Compute Service


  1. To create a disk

    gcloud compute disks create
  2. To resize the disks

    gcloud compute disks resize
  3. To list down the deprecated images

    gcloud compute images list/describe/create/deprecate
  4. To create a instance disabling auto termination of disk

    gcloud compute instances create instance-1  \
        --no-auto-delete  \
        --disk exampledisk
  5. To create a network in custom mode

    gcloud compute networks create ace-exam-vpc1  \
        --subnet-mode=custom
  6. To create a network and enabling flow-logs

    gcloud beta compute networks subnets create cert-exam-vpc-subnet1  \
        --network=cert-exam-vpc1  \
        --region=us-west2  \
        --range=10.10.0.0/16  \
        --enable-private-ip-google-access  \
        --enable-flow-logs
  7. Expanding the IP address range

    gcloud compute networks subnets expand-ip-range cert-exam-subnet1  \
        --prefix-length 16

Google Kubernetes Engine


  1. listing down the clusters

    gcloud container clusters list
  2. Getting details of the cluster

    gcloud container clusters describe  \
        --zone us-central1-a standard-cluster-1
  3. Listing down Kubernetes nodes

    kubectl get nodes
  4. Listing down the Kubernetes pods

    kubectl get pods
  5. Describing the nodes

    kubectl describe nodes
  6. Describing the pods

    kubectl describe pods
  7. Creating a cluster

    gcloud container clusters create example-cluster
  8. Resizing the cluster

    gcloud container clusters resize standard-cluster-1  \
        --node-pool default-pool  \
        --size 5  \
        --region=us-central1
  9. Auto-scaling

    gcloud container clusters update standard-cluster-1  \
        --enable-autoscaling  \
        --min-nodes 1  \
        --max-nodes 5  \
        --zone us-central1-a  \
        --node-pool default-pool
  10. listing down the deployments

    kubectl get deployments
  11. Scale the deployments

    kubectl scale deployment nginx-1  \
        --replicas 5
  12. Auto scaling the deployments

    kubectl autoscale deployment nginx-1  \
        --max 10  \
        --min 1  \
        --cpu-percent 80
  13. Deleting the deployments

    kubectl delete deployment nginx-1
  14. List down the services

    kubectl get services
  15. Running the deployment

    kubectl run hello-server  \
        --image=gcr.io/google/samples/hello-app:1.0  \
        --port 8080
  16. Exposing the deployment

    kubectl expose deployment hello-server  \
        --type=”LoadBalancer”
  17. Deleting the service

    kubectl delete service hello-server
  18. Listing down the images

    gcloud container images list
  19. Describing the images properties

    gcloud container images describe gcr.io/appengflex-project-1/nginx

App engine


  1. Deploying the application on to app engine

    gcloud app deploy app.yml
  2. Stopping the versions of app-engine

    gcloud app versions stop v1 v2
  3. Splitting the traffic

    gcloud app services set-traffic serv1  \
        --splits v1=.4,v2=.6

Cloud Function


  1. To deploy a function with all the specifications

    gcloud functions deploy cloud_storage_function_quant \
        --runtime python37 \
        --trigger-resource gcp-ace-quant-test-bucket \
        --trigger-event google.storage.object.finalize
  2. Deleting a cloud function

    gcloud functions delete

Components


  1. List down the components

    gcloud components list
  2. Update the components

    gcloud components update
  3. Install the components

    gcloud components install <component-name>

CloudSQL


  1. Create an SQL database

    gcloud sql databases create
  2. Connect to the sql

    gcloud sql connect ace-exam-mysql –user=root
  3. Create a backup

    gcloud sql backups create  \
        --async  \
        --instance ace-exam-mysql
  4. Export the data from cloudsql instance

    gcloud sql instances export sql quantiphi-mysql1 gs://quantiphi-buckete1/quantiphi-mysqlexport.sql  \
        --database=mysql

Cloud PubSub


  1. Topic creation

    gcloud pubsub topics create [TOPIC-NAME]
  2. Subscribtion create

    gcloud pubsub subscriptions create [SUBSCRIPTION-NAME]  \
        --topic [TOPIC-NAME]
  3. Publish a message to specific topic

    gcloud pubsub topics publish topic1  \
        --message “Quantiphi-AI/ML”
  4. Pull the messages

    gcloud pubsub subscriptions pull  \
        --auto-ack sub1

Dataproc


  1. Creating a cluster

    gcloud dataproc clusters create cluster-bc3d  \
        --zone us-west2-a
  2. Submitting the jobs

    gcloud dataproc jobs submit spark  \
        --cluster cluster-bc3d  \
        --jar Quantiphi.jar

BigQuery


  1. Listing down the jobs

    bq ls -j -a project

Datastore


  1. Creating indexes in Datastore

    gcloud datastore create-indexes
  2. Export all kinds in the exampleNs namespace in the exampleProject project to the exampleBucket

    gcloud datastore export gs://exampleBucket  \
        --namespaces=’exampleNs’  \
        --project=’exampleProject’

CloudStorage


  1. Lists all your buckets

    gsutil ls
  2. Help on the topic

    gsutil help <topic>
  3. Bucket creation

    gsutil mb gs://quanti-bucket
  4. Deletes the bucket.

    gsutil rm gs://<bucket_name>
  5. Files - copies the local filename into the bucket

    gsutil cp <filename> gs://<bucket_name>/
  6. Copies the local filename into the directory

    gsutil cp <filename> gs://<bucket_name>/directory/
  7. Moves the local src_filename to the directory and renames it as quanti-fi1

    gsutil mv <src_filename> gs://<bucket_name>/directory/quanti-fi1
  8. Deletes the file_or_dir object.

    gsutil rm gs://<bucket_name>/file_or_dir
  9. Changing the storage class

    gsutil rewrite -s [STORAGE_CLASS] gs://[PATH_TO_OBJECT]
  10. Modifying the access control list

    gsutil acl ch -u [SERVICE_ACCOUNT_ADDRESS]:W gs://[BUCKET_NAME]
  11. Assigning roles

    gsutil iam ch user : <user_email>:<role1,role2> gs://<BUCKET>
  12. Getting Versioning status

    gsutil versioning get gs://bucket
  13. Enabling versioning

    gsutil versioning set on gs://bucket
  14. Life cycle status

    gsutil lifecycle get gs://bucket > filename.json
  15. Setting the life cycle version

    gsutil lifecycle set filename.json gs://bucket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment