
准备工作
- 实验会创建一个 Google Cloud 项目和一些资源,供您使用限定的一段时间
- 实验有时间限制,并且没有暂停功能。如果您中途结束实验,则必须重新开始。
- 在屏幕左上角,点击开始实验即可开始
Create a Cloud SQL Instance and database
/ 20
Create Service Account
/ 20
Create Secrets
/ 30
Deploy the SQL Proxy agent as a sidecar container
/ 30
In this lab, you set up a Kubernetes Deployment of WordPress connected to Cloud SQL via the SQL Proxy. The SQL Proxy lets you interact with a Cloud SQL instance as if it were installed locally (localhost:3306), and even though you are on an unsecured port locally, the SQL Proxy makes sure you are secure over the wire to your Cloud SQL Instance.
To complete this lab you'll create several components. First you create a GKE cluster, next you create a Cloud SQL Instance to connect to, and a Service Account to provide permission for your Pods to access the Cloud SQL Instance, this will be authenticated using Workload Identity. Finally you deploy WordPress on your GKE cluster, with the SQL Proxy as a Sidecar, connected to your Cloud SQL Instance.
In this lab, you learn how to perform the following tasks:
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Sign in to Qwiklabs using an incognito window.
Note the lab's access time (for example, 1:15:00
), and make sure you can finish within that time.
There is no pause feature. You can restart if needed, but you have to start at the beginning.
When ready, click Start lab.
Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.
Click Open Google Console.
Click Use another account and copy/paste credentials for this lab into the prompts.
If you use other credentials, you'll receive errors or incur charges.
Accept the terms and skip the recovery resource page.
After you complete the initial sign-in steps, the project dashboard appears.
Google 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.
Google Cloud Shell provides command-line access to your Google Cloud resources.
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
Click Continue.
It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Example output:
Output:
Example output:
kubectl
command-line tool:In the Google Cloud Console, on the Navigation menu (), click APIs & Services.
Click + Enable APIs and Services.
For Search for APIs & Services, type SQL and then click the Cloud SQL API tile.
Click Enable to enable Cloud SQL API.
If the API is already enabled, a Manage button appears instead, with an API enabled message. In that case, no action is required.
sql-instance
listed , click on the name, and then click on the Users menu.When you see the existing root
user listed you can proceed to the next step.
Click Add User Account and create an account, using sqluser
as the username and sqlpassword
as the password.
Leave the Host name option set to Allow any host (%). and click ADD.
Go back to Overview menu, still in your instance (sql-instance
), and copy your Instance connection name.
You will probably need to scroll down a bit to see it.
The mysql>
prompt appears indicating that you are now connected to the Cloud SQL instance using the MySQL client.
wordpress
by default:This will report Empty set as you have not created any tables yet.
Click Check my progress to verify the objective.
To create a Service Account, in the Google Cloud console navigate to IAM & Admin> Service Accounts.
Click + Create Service Account.
Specify the Service account name called sql-access
then click Create and Continue.
Click Select a role.
Search for Cloud SQL, select Cloud SQL Client and click Continue.
Click Done.
Locate the service account sql-access
and click on three dots icon in Actions column.
Select Manage keys.
Then click on ADD KEY and select Create new key.
Ensure JSON key type is selected and click CREATE.
This will create a public/private key pair, and download the private key file automatically to your computer. You'll need this JSON file later.
credentials.json
Click Check my progress to verify the objective.
You create two Kubernetes Secrets: one to provide the MySQL credentials and one to provide the Google credentials (the service account).
If you used a different username and password when creating the Cloud SQL user accounts substitute those here.
In the Cloud Shell, click More () on the far right of the Cloud Shell menu bar.
Select Upload, leave File selected and click on Choose Files and Upload the credentials.json
credential file you downloaded in the previous task to the Cloud Shell.
In the Cloud Shell move the credential file to the current working directory:
Files uploaded to the Cloud Shell are uploaded to the user's home directory. It is easier to work with files in the current working directory with kubectl
so moving it makes the next step simpler.
Click Check my progress to verify the objective.
Let's create a deployment manifest file called sql-proxy.yaml
that deploys a demo Wordpress application container with the SQL Proxy agent as a sidecar container.
In the Wordpress container environment settings the WORDPRESS_DB_HOST is specified using the localhost IP address. The cloudsql-proxy
sidecar container is configured to point to the Cloud SQL instance you created in the previous task. The database username and password are passed to the Wordpress container as secret keys, and Workload Identity is configured. A Service is also created to allow you to connect to the Wordpress instance from the internet.
Create and open a file called sql-proxy.yaml
with nano using the following command:
sql-proxy.yaml
file:Press Ctrl+O, and then press Enter to save your edited file.
Press Ctrl+X to exit the nano text editor.
The important sections to note in this manifest are:
spec
section the Kubernetes Service Account is configured.WORDPRESS_DB_HOST
is set to 127.0.0.1:3306.
This will connect to a container in the same Pod listening on port 3306. This is the port that the SQL-Proxy listens on by default.env
section the variables WORDPRESS_DB_USER
and WORDPRESS_DB_PASSWORD
are set using values stored in the sql-credential
Secret you created in the last task.cloudsql-proxy
container section the command switch that defines the SQL Connection name, "INSTANCE_CONNECTION_NAME>
contains a placeholder variable that is not configured using a ConfigMap or Secret and so must be updated directly in this example manifest to point to your Cloud SQL instance."wordpress-service
" that allows the application to be accessed from external internet addresses.sed
to update the placeholder variable for the SQL Connection name to the instance name of your Cloud SQL instance:Output:
The external LoadBalancer ip-address for the wordpress-service is the address you use to connect to your Wordpress blog.
Click Check my progress to verify the objective.
None of these values are particularly important, you will not need to use them.
After a few seconds you will see the Success! Notification. You can log in if you wish to explore the Wordpress admin interface but it is not required for the lab.
The initialization process has created new database tables and data in the wordpress database on your Cloud SQL instance. You will now validate that these new database tables have been created using the SQL proxy container.
The mysql>
prompt appears indicating that you are now connected to the Cloud SQL instance using the MySQL client.
This will now show a number of new database tables that were created when Wordpress was initialized demonstrating that the sidecar SQL Proxy container was configured correctly:
This will list the database record for the Wordpress admin account showing the email you chose when initializing Wordpress.
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2022 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.
此内容目前不可用
一旦可用,我们会通过电子邮件告知您
太好了!
一旦可用,我们会通过电子邮件告知您
一次一个实验
确认结束所有现有实验并开始此实验