检查点
Create an instance and database
/ 40
Create a schema for the database
/ 30
Create an instance and database with CLI
/ 30
Cloud Spanner - Database Fundamentals
GSP1048
Overview
Cloud Spanner is Google’s fully managed, horizontally scalable relational database service. Customers in financial services, gaming, retail and many other industries trust it to run their most demanding workloads, where consistency and availability at scale are critical.
In this lab, you perform basic administrative tasks within a Cloud Spanner instance.
What you'll do
In this lab, you learn how to perform the following tasks:
- Create an instance.
- Create a database.
- Create tables.
- Insert and modify data.
- Use the Google Cloud CLI with Cloud Spanner.
- Use Automation Tools with Cloud Spanner.
- Delete an instance.
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.
This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud console
-
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
- The Open Google Cloud console button
- Time remaining
- The temporary credentials that you must use for this lab
- Other information, if needed, to step through this lab
-
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account. -
If necessary, copy the Username below and paste it into the Sign in dialog.
{{{user_0.username | "Username"}}} You can also find the Username in the Lab Details panel.
-
Click Next.
-
Copy the Password below and paste it into the Welcome dialog.
{{{user_0.password | "Password"}}} You can also find the Password in the Lab Details panel.
-
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges. -
Click through the subsequent pages:
- Accept the terms and conditions.
- Do not add recovery options or two-factor authentication (because this is a temporary account).
- Do not sign up for free trials.
After a few moments, the Google Cloud console opens in this tab.
Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
- Click Activate Cloud Shell at the top of the Google Cloud console.
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
- (Optional) You can list the active account name with this command:
- Click Authorize.
Output:
- (Optional) You can list the project ID with this command:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Task 1. Create an instance
-
The first step in using Cloud Spanner is to create an instance. An instance is an allocation of Google Cloud compute and storage resources. On the Cloud Console Navigation menu (), under Databases click Spanner.
-
Accept any acknowledgement or information window that may appear.
-
Then click Create a Provisioned Instance.
-
Fill in the following fields, leave the remainder with the default values:
Item | Value |
---|---|
Instance Name | banking-instance |
Select a configuration | |
Allocate Compute Capacity | Unit - Nodes // Quantity - 1 |
- Click Create. Now you can see your instance on the Instance Details page. Here you have an overview of how the instance is performing, utilization, etc.. The next step is to create a database.
Task 2. Create a database
-
From the instance details page, click Create database.
-
For the database name, enter banking-db.
-
Skip the Define your schema (optional) step for now. You'll define your schema in the next section.
-
Click Create.
-
You're now on the Overview page for the new database you created. You can see that the page is similar to the Instance one, but the statistics refer to the specific database. Also note the new options on the left menu.
-
Click Check my progress to verify the objective.
Task 3. Create a table in your database
- On the Database Details page for your banking-db database, scroll down the page and click Create table.
- Click the blue + icon to open the Query page, enter:
-
Click Run.
-
When the operation is complete, click Overview under Database in the left menu. Then scroll down to Tables and click Customer to see the schema details:
-
Click Check my progress to verify the objective.
Task 4. Insert and modify data
The Cloud Console provides an interface for inserting, editing, and deleting data.
Insert data
-
While on the Schema page, click Data in the left menu. Then click Insert.
-
This takes you to the Query tab of the Spanner Studio automatically. Click Clear Query, paste the query below, and click Run:
-
The lower page of the screen shows the result. The Customer table now has one row.
-
Add a second row. Replace the previous statement with the following, and click Run:
Run a query
-
You can execute a SQL statement on the query page of your database.
-
In the left pane of the Cloud Platform Console, click Spanner Studio to navigate to the Query UI window.
-
Click the blue + icon to open the Query page. Click Clear Query, paste the query below, and click Run:
-
Click Run.
-
The Cloud Console displays the result of your query.
Task 5. Use the Google Cloud CLI with Cloud Spanner
The Cloud Console is very useful, but in some use cases you want to manage Spanner instances using other methods. Google Cloud services can also be managed through the command line tool named gcloud. The easiest way to use the gcloud CLI is via the Cloud Shell but it can also be installed on a wide variety of operating systems.
Create an instance with CLI
- Creating a Spanner instance via gcloud is very simple. The core command is as follows:
- In the Cloud Shell, create a new Cloud Spanner using the command below.
Listing instances
- You can run the following command to list the Spanner instances available in your project.
Creating a database
-
You can also create databases in a Spanner instance using gcloud.
-
In the Cloud Shell, create a new database using the command below.
- Click Check my progress to verify the objective.
Modifying number of nodes
Remember that it is important to provision enough nodes to keep CPU utilization and storage utilization below the recommended maximum values. However, sometimes it is necessary to reduce the number of nodes.
-
You are now going to reduce the number of nodes of the instance banking-instance-2 from two to one.
-
Use the following gcloud command to adjust the instance:
- After completion, check that the number of nodes has been reduced:
Task 6. Use Automation Tools with Cloud Spanner
As your Spanner architectures grow and get more complex, you want to automate the deployment and management of Spanner instances. One of the tools available to deploy and manage Spanner is Terraform.
The goal of this lab is not to explain how Terraform works. If you are not familiar with Terraform, you can check the documentation.
In short, you provide Terraform with a set of files that describe the infrastructure or services that you want it to deploy in Google Cloud, which is called a configuration. Then you instruct Terraform to initialize and plan the deployment, before applying the changes.
Verify Terraform installation
- Terraform comes pre-installed in the Cloud Shell. Using the previous Cloud Shell (or open it again if you closed it), verify that terraform is available:
- You should see an output similar to this:
Create Terraform Configuration
- In the cloud shell enter the following command to invoke the Nano text editor and create a new empty configuration file named spanner.tf.
- In the Nano editor, paste the code block listed below.
- Press Ctrl+X to exit Nano, Y to confirm the update, and press Enter to save your changes.
Deploy
- The next step is to make sure all the Terraform service providers are available (in this case, the Spanner service provider). For that, run the following command in the Cloud Shell:
- Next instruct Terraform to create an execution plan that is based on the configuration file that you created a few steps ago. Run the following command:
- The output show details about the new instance that will be created. Run the following command to apply the plan to your project:
-
The plan will be displayed again and Terraform will pause for approval to continue. Type yes and Terraform will create the new instance.
-
The apply will process and return the following output:
- Confirm that the new instance, banking-instance-3, was created:
Task 7. Deleting instances
- A very quick way to delete an instance is using the CLI. Run the following command:
- To confirm that banking-instance-2 was deleted run the following command:
Congratulations!
You now have now a solid understanding of several basic administrative actions when using a Cloud Spanner Instance.
Google Cloud training and certification
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated May 23, 2024
Lab Last Tested May 23, 2024
Copyright 2024 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.