Google Cloud SDK contains tools and libraries that enable you to easily create and manage resources on Google Cloud Platform, including App Engine , Compute Engine , Cloud Storage , BigQuery , Cloud SQL , and Cloud DNS .
In this lab we will learn how to install Cloud SDK to a virtual machine, initialize it and run core gcloud commands from the command-line.
The Cloud SDK RPM packages are supported for Red Hat Enterprise Level 7 and CentOS 7.
Set up a VM to configure and use SDK
Create a VM with either Centos or Redhat. You can choose which one to use, the steps will be the same.
In the Cloud Console, go to Compute Engine > VM instances, then click Create.
In the Boot disk section, click Change to begin configuring your boot disk:
- Choose CentOS 7, then click the Select button.
In the Firewall section, select Allow HTTP traffic.
Update the Cloud SDK RPM packages
The Cloud SDK RPM packages are supported for Red Hat Enterprise Level 7 and CentOS 7. They may also work on Fedora systems using yum or dnf, but this has not been tested.
- Login to the newly created my-vm using ssh.
2. Run the following in the SSH window to set up Cloud SDK:
# Update YUM with Cloud SDK repo information:sudo tee -a /etc/yum.repos.d/google-cloud-sdk.repo << EOM
name=Google Cloud SDK
EOM# The indentation for the 2nd line of gpgkey is important.# Install the Cloud SDKsudo yum install google-cloud-sdkcontent_copy
3. Respond Y when prompted to confirm the total download size.
Initialize the SDK in your instance
gcloud initcommand to perform several common SDK setup tasks. These include authorizing the SDK tools to access Google Cloud using your user account credentials and setting up the default SDK configuration.
- To initialize the SDK, run the following:
gcloud init --console-only
Note: This prevents the
gcloud init command from launching a web browser.
2. Choose option 2, to log in with a new account.
3. You will get confirmation that you’re running on virtual machine. Type Y to allow the credentials you logged into the lab with (this is your personal account for this lab) to be used to authenticate your account.
4. You’ll be given a long URL click on it or paste it into a new browser.
5. Now Login with your personal GCP credentials. you will be presented with a Verification code.
6. Copy the code and paste it into the SSH window at the command prompt, then press Enter.
7. Now type the number corresponding to you Project ID.
You will see a confirmation that you have completed the setup steps successfully that will look like this:
You are all set to use cloud SDK now!!
Run core “gcloud“ commands
gcloud commands to view information about your SDK installation:
- List accounts whose credentials are stored on this VM:
gcloud auth list
A list of credentialed accounts displays:
2. This command will list the properties in your active SDK configuration:
gcloud config list
The list of properties will display:
3. Run the following to view information on your Cloud SDK installation and the active SDK configuration:
The summary includes information about:
- your system
- the installed SDK components
- the active user account and current project
- the properties in the active SDK configuration
4. You can see information about
gcloud commands and other topics from the command line by running the following:
gcloud help compute instances create
You’ll see a help topic that contains a description of the command, a list of command flags and arguments, and examples of how to use it.
Working with gcloud : VM Creation Scenarios
The gcloud auth list command Lists accounts whose credentials have been obtained using
gcloud auth login and
gcloud auth activate-service-account, and shows which account is active. The active account is used by gcloud and other Cloud SDK tools to access Google Cloud Platform. While there is no limit on the number of accounts with stored credentials, there is only one active account.
Below Command shows that “firstname.lastname@example.org” is the current active account.
Now Let’s try to create a VM using below command:
gcloud compute instances create my-vm2
You will see that a vm called “my-vm2” is up and running.
Now Try to change the active account to email@example.com using below command:
gcloud config set account firstname.lastname@example.org
Now try to create another vm called “my-vm3″ using below command:
gcloud compute instances create my-vm3
We get an error. why So?
The reason we get the error is because we have not authenticated to gcloud init with the service account email@example.com.
Note: We can’t use service account to login to GCP directly either.
Interestingly when we have created “my-vm2” using account “firstname.lastname@example.org”, it used the service account — email@example.com to create the VM.
Confusing ?? Give it a thought.