Google Cloud Series, Part 2: G-Cloud Command Line Tool
In Part One of our Google Cloud (GC) Series, we discussed GC in general, as well as how to create virtual machines. Part Two focuses, specifically, on gcloud Command Line Tool. GC Platform allows you access almost all services in three ways:
Google Cloud Console(Your dashboard)
GCloud(Command line tool available from google)
RestApi(You can also create apps to control your resources)
Let’s get started.
What is gcloud ?
It’s a command line tool you can use to access google cloud services, either using scripts or from command line, and run other automations.
Yes, you can write scripts to spin up the instances according to your requirements, and you can even set triggers to run those scripts–but we will save those details for another day…
How can you access gcloud ?
GCloud is part of the Google Cloud SDK. You have to download and install the SDK on your system and initialize it before you can use gcloud. GCloud provides access to generally available commands by default.
There are several release levels of gcloud commands:
General Availability – Fully stable and production-ready.
Beta – Fully functional but may still have some issues (not covered in SLAs)
Alpha – In early stage release and highly changeable.
Preview – Unstable and may change without notice.
When it comes to access, if you don’t want to install the SDK, you can use the Google Command Shell. This provides you with gcloud command-line access to computing resources hosted on Google Cloud Platform and available through the Google Cloud Platform Console. Cloud Shell is a Debian-based virtual machine with a persistent 5GB home directory, which makes it easy for you to manage your Cloud Platform projects and resources. With Cloud Shell, the Cloud SDK gcloudand other utilities you need are pre-installed and always available.
Open your cloud console and start Google Command Shell by clicking on the top right icon in the toolbar:
Then click “Start Cloud Shell”. This will take few moments to provision and connect to the environment. When you start Cloud Shell, it provisions a g1-small Google Compute Engine virtual machine running a Debian-based Linux operating system. Cloud Shell instances are provisioned on a per-user, per-session basis. The instance persists while your Cloud Shell session is active and terminates after an hour of inactivity.
After Cloud Shell is activated, you can use the command line to invoke the Cloud SDK gcloud command, or other tools, available on the virtual machine instance.
Getting started with commands
Here’s a favorite command:
Output of this will be:
Usage: gcloud [optional flags] <group | command> group may be alpha | app | auth | beta | components | compute | config | container | dataflow | dataproc | datastore | debug | deployment-manager | dns | domains | endpoints | firebase | iam | kms | logging | ml | ml-engine | organizations | projects | pubsub | service-management | services | source | spanner | sql | topic command may be docker | feedback | help | info | init | version
For detailed information on this command and its flags, run: gcloud --help
You can clearly see all the gcloud commands are divided into groups. Projects, Compute, Container–all of these are different groups, and each group contains commands relevant to that group. Alpha group is used to access alpha level commands and beta group is used to access beta level commands.
With prior understanding of instances, it makes sense to explore the compute group. Running these commands is a far better method than controlling them from console. Start by running:
Compute command is also divided into further groups. Compare these groups from the compute section on Google Cloud console:
Most of the groups available in the command output are same as they are available in console.
Creating a virtual machine, even if you don’t know any command using gcloud, is possible. Using the help command, you can follow the documentation available in the help manual of that command.
gcloud compute --help
This yields more information about command and presents a group named “instances” that can be used in reading and manipulating compute engine virtual machine instances.
groups:....... instance-groups Read and manipulate Google Compute Engine instance groups. instance-templates Read and manipulate Google Compute Engine instances templates. instances Read and manipulate Google Compute Engine virtual machine instances. interconnects Read and manipulate Google Compute Engine interconnects.........
Move inside that group and run the help command again:
gcloud compute instances --help
You’ll see a bunch of commands that can be executed on this group like start, stop, attach-disk, delete, create etc. Additionally, you’ll see the description of each command to avoid any confusion. Observe the “create” command.
COMMANDS .........attach-disk Attach a disk to an instance. create Create Google Compute Engine virtual machine instances. delete Delete Google Compute Engine virtual machine instances. delete-access-config Delete an access configuration from a virtual machine network interface...........
gcloud compute instances create --help
Output for above command will be something like
The exact format that gcloud expects and all the possible parameters that I can pass to it are visible. It’s possible to look into the description provided below with information of all the available flags and there possible values: