实验设置说明和要求
保护您的账号和进度。请务必在无痕浏览器窗口中,使用实验凭证运行此实验。

Running Databases in GKE

实验 1 小时 universal_currency_alt 5 积分 show_chart 中级
info 此实验可能会提供 AI 工具来支持您学习。
此内容尚未针对移动设备进行优化。
为获得最佳体验,请在桌面设备上访问通过电子邮件发送的链接。

Overview

In this lab, you create a Google Kubernetes Engine (GKE) cluster, and then deploy databases into it. You see two ways to deploy the databases: first using your own configuration code, and then using a Kubernetes package manager called Helm.

Objectives

In this lab, you learn how to perform the following tasks:

  • Create a GKE cluster.
  • Deploy MySQL on the cluster.
  • Use Helm to deploy MySQL on the cluster.

Setup

In this task, you perform initialization steps for your lab.

For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.

  1. Sign in to Google Skills using an incognito window.

  2. 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.

  3. When ready, click Start lab.

  4. Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.

  5. Click Open Google Console.

  6. 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.

  7. Accept the terms and skip the recovery resource page.

Task 1. Create a GKE cluster

  1. Open a new web browser window and navigate to the Google Cloud Console (console.cloud.google.com). Use the project selector to choose the first project with a leading name of 'qwiklabs-gcp.'

  2. On the Navigation menu (Navigation menu icon), click Kubernetes Engine > Clusters.

  3. Click Create.

  4. You can see Standard: You manage your cluster inside Create a cluster pop-up.

  5. Click Configure next to Standard: You manage your cluster.

  6. For Location type, select Zonal.

  7. Accept all defaults and click Create. It will take a couple minutes for the cluster to be ready.

  8. When the cluster is ready, click Connect from the Actions menu.

The Connect option selected in the expanded More actions menu

The command for connecting to the cluster is specified.

  1. To open Cloud Shell with the command entered, click Run in Cloud Shell, and if prompted click Continue.

The Run in CLoud Shell button highlighted on the Command-line access page

  1. Press ENTER to run the command. If prompted click Authorize.

You are connected to the cluster and ready to deploy a program.

  1. Test the connection with the following kubectl command:
kubectl get nodes

This command returns a list of the three virtual machines that make up this cluster.

Click Check my progress to verify the objective. Create a Kubernetes cluster

Review

You just created a Kubernetes cluster. Next, you configure and deploy MySQL to run in it.

Task 2. Deploy MySQL on the cluster

You need a root password for the database. You store the password as a Kubernetes secret. The secret is a key-value pair. In this case, the key is ROOT_PASSWORD and the value is password.

  1. To create the secret, enter the following command:
kubectl create secret generic mysql-secrets --from-literal=ROOT_PASSWORD="password"
  1. Create a folder for the configuration files you create, and change to it:
mkdir mysql-gke cd mysql-gke
  1. Create the Kubernetes configuration files. In Cloud Shell, click Open Editor.

The Open Editor button highlighted in the UI

  1. Select the mysql-gke folder on the left.

The mysql-gke folder highlighted in the Explorer

  1. Right click the mysql-gke folder, and then click New File.

  2. For name, type volume.yaml.

  3. Enter the following YAML code and save the file:

apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysql-data-disk spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi

This reserves 1 gigabyte of disk space for the MySQL database. Note the name mysql-data-disk. This name will be used in the next file.

  1. To configure the MySQL database, create another new file in the mysql-gke folder, name it deployment.yaml, and paste the following code into it:
apiVersion: apps/v1 kind: Deployment metadata: name: mysql-deployment labels: app: mysql spec: replicas: 1 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - name: mysql image: mysql:8.0 ports: - containerPort: 3306 volumeMounts: - mountPath: "/var/lib/mysql" subPath: "mysql" name: mysql-data env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-secrets key: ROOT_PASSWORD - name: MYSQL_USER value: testuser - name: MYSQL_PASSWORD value: password volumes: - name: mysql-data persistentVolumeClaim: claimName: mysql-data-disk

Notes:

  • On line 19, the MySQL docker image is specified.
  • Starting at line 26, an environment variable is created for the database root password using the secret you created earlier. There are also variables to create a test user with a simple password.
  • On the last line, the disk space you allocated in the previous file is used.

The database needs a service so it can be accessed.

  1. Create a third file in the mysql-gke folder, name it service.yaml, and paste the following code into it:
apiVersion: v1 kind: Service metadata: name: mysql-service spec: selector: app: mysql ports: - protocol: TCP port: 3306 targetPort: 3306 Note: This creates a service that provides access to the database from within the cluster that forwards requests to the MySQL database.
  1. In Cloud Shell, click Open Terminal to return to the command line. Make sure you are in the correct folder and type ls to verify that you have your three YAML files.

  2. To deploy your database, enter the following kubectl commands:

kubectl apply -f volume.yaml kubectl apply -f deployment.yaml kubectl apply -f service.yaml
  1. Wait a minute for the resources to be created, and then enter the following command:
kubectl get pods

The pod that has your database installed should be running. If it is not running yet, wait a little while and try again.

Note: At this point, there are no client applications and the database is only accessible from inside the cluster. In the next step, you access the database from inside the pod where it is running.

Click Check my progress to verify the objective. Deploy MySQL on the cluster

  1. Copy the name of the pod obtained from the last command to the clipboard. It will begin with mysql-deployment- followed by a unique string.

  2. Enter the following command but < replace the pod name > with your pod's name:

kubectl exec -it <mysql-deployment-76fdc44468-rfhbp> -- /bin/bash

Now you're at a bash prompt within the MySQL pod.

  1. To log in to MySQL, enter the following:
mysql -u root -p
  1. When prompted, enter the password password. This gives you a mysql prompt.

  2. Run the following command:

show databases;
  1. Create a new database:
create database pets;
  1. To confirm that your database was created, enter:
show databases;
  1. Type exit to exit MySQL.

  2. Type exit again to return to the Cloud Shell command prompt.

  3. To remove everything that was created, enter the following commands:

kubectl delete -f service.yaml kubectl delete -f deployment.yaml kubectl delete -f volume.yaml

Review

You deployed a MySQL database to a Kubernetes cluster using Kubernetes configuration files.

Helm is a package manager for Kubernetes. It can make deploying databases and other applications easier on a Kubernetes cluster. You use it next.

Task 3. Use Helm to deploy MySQL on the cluster

You should still be in Cloud Shell, connected to your Kubernetes cluster.

  1. To add the Bitnami Helm repository to your cluster, enter the following command:
helm repo add bitnami https://charts.bitnami.com/bitnami
  1. To update the Helm packages, enter the following command:
helm repo update
  1. Install a MySQL named mydb using Helm:
helm install mydb bitnami/mysql

Click Check my progress to verify the objective. Use Helm to deploy MySQL on the cluster

  1. Read the output from the Helm install command and connect to your database using the instructions provided.

  2. When you are connected to the database, exit to return to the Cloud Shell command prompt.

  3. To see your Helm deployment, enter the following command:

helm ls

Notice that the deployment has the name mydb specified in the install command.

  1. To delete the deployment, enter the following command:
helm delete mydb

Congratulations! You have created a Kubernetes cluster and then deployed MySQL databases into it, first using your own configuration code, and then using Helm.

End your lab

When you have completed your lab, click End Lab. Google Skills 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:

  • 1 star = Very dissatisfied
  • 2 stars = Dissatisfied
  • 3 stars = Neutral
  • 4 stars = Satisfied
  • 5 stars = Very satisfied

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 2026 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.

准备工作

  1. 实验会创建一个 Google Cloud 项目和一些资源,供您使用限定的一段时间
  2. 实验有时间限制,并且没有暂停功能。如果您中途结束实验,则必须重新开始。
  3. 在屏幕左上角,点击开始实验即可开始

使用无痕浏览模式

  1. 复制系统为实验提供的用户名密码
  2. 在无痕浏览模式下,点击打开控制台

登录控制台

  1. 使用您的实验凭证登录。使用其他凭证可能会导致错误或产生费用。
  2. 接受条款,并跳过恢复资源页面
  3. 除非您已完成此实验或想要重新开始,否则请勿点击结束实验,因为点击后系统会清除您的工作并移除该项目

此内容目前不可用

一旦可用,我们会通过电子邮件告知您

太好了!

一旦可用,我们会通过电子邮件告知您

一次一个实验

确认结束所有现有实验并开始此实验

使用无痕浏览模式运行实验

使用无痕模式或无痕浏览器窗口是运行此实验的最佳方式。这可以避免您的个人账号与学生账号之间发生冲突,这种冲突可能导致您的个人账号产生额外费用。