As I am studying for the Associate Cloud Engineer Google Certification, collating a key set of commands which will really come handy for preparation of the exam and obviously as well in day to day operations.

Create Instance

  1. Following will use the defaults to create an instance
gcloud compute instances create instance1

#Use the following command to verify the defaults

gcloud compute project-info describe

2. Additional parameters that can be added to the creation of VM command

--boot-disk-type
--boot-disk-size
--preemptible
--labels
--machine-type

3. Sample command with additional creation parameters

#sample 1
gcloud compute instance create instance1 --machine-type=n1-standard-8

#sample 2 
gcloud compute instance create --preemptible --machine-type=n1-standard-8 instance1

gcloud wide flags

4. These are referred to as gcloud-wide flags, also known as gcloud global flags, and include the following:

  • --account specifies a GCP account to use overriding the default account.
  • --configuration uses a named configuration file that contains key-value pairs.
  • --flatten generates separate key-value records when a key has multiple values.
  • --format specifies an output format, such as a default (human readable) CSV, JSON, YAML, text, or other possible options.
  • --help displays a detailed help message.
  • --project specifies a GCP project to use, overriding the default project.
  • --quiet disables interactive prompts and uses defaults.
  • --verbosity specifies the level of detailed output messages. Options are debuginfowarning, and error.

Commands can take an optional --zone parameter. 

Start Instance

5. To start an instance

gcloud compute instances start INSTANCE_NAMES

gcloud compute instance start mots_instance1 mots_instance2

The --async parameter displays information about the start operation. 

gcloud compute instance start mots_instance1 mots_instance2 --async

GCP needs to know in which zone to create an instance. This can be specified with the --zone parameter as follows

gcloud compute instance start mots_instance1 mots_instance2 --zone us-central1-c

List the zones with the following command

gcloud compute zones list

Delete Instance

6. Delete instances with the delete command

gcloud compute instances delete mots-instance-1

Specify zone parameter to identify where the VM is located

gcloud compute instances delete mots-instance-1 --zone us-central1-c

When an instance is deleted, the disks on the VM may be deleted or saved by using the --delete-disks and --keep-disks parameters, respectively.

You either can specify :

  • all to keep all disks
  • boot to specify the partition of the root file system
  • data to specify nonboot disks.

Keeping all disks

gcloud compute instances delete mots-instance-1 --keep-disks=all

Delete all non boot disks

glcoud compute instances delete mots-instance-1 --delete-disks=data

View VM Inventory

7. View VM inventory

gcloud compute instances list

You can filter the list of VM’s & sort, by using the following

gcloud compute instances list --filter="zone:(us-central1-c europe-west1-d)" --sort-by name

Resource fields can be listed by the following command

gcloud compute instances describe mots-instance-1 

Snapshot

8. Create a snapshot of a disk

gcloud compute disks snapshot DISK_NAME --snapshot-name=NAME

List of snapshots

gcloud compute snapshots list

Detail info about snapshot

gcloud compute snapshots describe SNAPSHOT_NAME

Creation of Disk

gcloud compute disks create DISK_NAME --source-snapshot=SOURCE_SNAPSHOT

Sample with size and disk type

gcloud compute disks create mots-disk-01 --source-snapshot=abstract-snapshot --size=100 --type=pd-standard

#100GB Persistent Disk

Images

9. Working with images

gcloud compute images create IMAGE_NAME
  • --source-disk
  • --source-image
  • --source-image-family
  • --source-snapshot
  • --source-uri

Delete an image

gcloud compute images delete IMAGE_NAME

Export an image to Cloud Storage

gcloud compute images export --destination-uri DESTINATION_URI --image IMAGE_NAME

Create a new image from disk

gcloud compute images create IMAGE_NAME --source-disk DISK_NAME

Instance Groups

Create an Instance Template

gcloud compute instance-templates create INSTANCE_NAME

Create an Instance Template from existing Instance.

gcloud compute instance-templates create INSTANCE_TEMPLATE_NAME --source-instance=SOURCE_VM

Delete an Instance Template

gcloud compute instance-templates delete INSTANCE_TEMPLATE_NAME

List instance templates

gcloud compute instance-templates list 

List instance groups

gcloud compute instance-groups managed list-instances

List insances in an instance group

gcloud compute instance-groups managed list-instances INSTANCE_GROUP_NAME

Kubernetes

Exposing Kubernetes Cluster as a service through a network load balancer. This command will load balance traffic across the running instances, and can create a HA proxy for accessing the containers from outside the cluster.

kubectl expose deployments nginx --port=80 --type=Loadbalancer

List all pods in ps output format. Produces a YAML file.

kubectl get pods "app=nginx"

To create a cluster, run the following command. Cluster names must start with a letter, end with an alphanumeric, and cannot be longer than 40 characters.

gcloud container clusters create my-cluster

After creating your cluster, you need to get authentication credentials to interact with the cluster.To authenticate the cluster run the following command, replacing [CLUSTER-NAME] with the name of your cluster:

gcloud container clusters get-credentials my-cluster

This Kubernetes command creates a Deployment object that represents hello-app. In this command:

–image specifies a container image to deploy. In this case, the command pulls the example image from a Google Container Registry bucket. gcr.io/google-samples/hello-app:1.0 indicates the specific image version to pull. If a version is not specified, the latest version is used.

–port specifies the port that the container exposes.

kubectl create deployment hello-server --image=gcr.io/google-samples/hello-app:1.0

Now you can create a Kubernetes Service, which is a Kubernetes resource that lets you expose your application to external traffic, by running the following kubectl expose command:
Passing in type=”LoadBalancer” creates a Compute Engine load balancer for your container.

kubectl expose deployment hello-server --port=8080 --type="LoadBalancer"

List all services in the namespace

kubectl get service hello-server

Delete cluster

gcloud container clusters delete my-cluster