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

App Dev - Deploying the Application into Kubernetes Engine: Node.js

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

Overview

In this lab, you deploy the Quiz application into Kubernetes Engine (formerly known as Container Engine), and leverage Google Cloud Platform resources, including Cloud Build and Container Registry, and Kubernetes resources, including Deployments, Pods, and Services.

Objectives

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

  • Create Dockerfiles to package up the Quiz application frontend and backend code for deployment.
  • Harness Cloud Build to produce Docker images.
  • Provision a Kubernetes Engine cluster to host the Quiz application.
  • Employ Kubernetes deployments to provision replicated Pods into Kubernetes Engine.
  • Leverage a Kubernetes service to provision a load balancer for the quiz frontend.

Setup and requirements

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.

Activate Google Cloud Shell

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.

  1. In Cloud console, on the top right toolbar, click the Open Cloud Shell button.

    Highlighted Cloud Shell icon

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

Project ID highlighted in the Cloud Shell Terminal

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  • You can list the active account name with this command:
gcloud auth list

Output:

Credentialed accounts: - @.com (active)

Example output:

Credentialed accounts: - google1623327_student@qwiklabs.net
  • You can list the project ID with this command:
gcloud config list project

Output:

[core] project =

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: Full documentation of gcloud is available in the gcloud CLI overview guide .

Task 1. Preparing the case study application

In this section, you access Cloud Shell, clone the git repository containing the Quiz application, configure environment variables, and run the application.

Clone source code in Cloud Shell

  1. In Cloud Shell, clone the repository for the class:
git clone --depth=1 https://github.com/GoogleCloudPlatform/training-data-analyst
  1. Create a soft link as a shortcut to your working directory:
ln -s ~/training-data-analyst/courses/developingapps/v1.3/nodejs/containerengine ~/containerengine

Configure the case study application and review code

  1. Change the working directory:
cd ~/containerengine/start
  1. To replace the default region in a file with the lab-assigned region, run the following commands:
export APP_REGION={{{project_0.startup_script.app_gcp_region | APP_REGION}}} export REGION={{{project_0.default_region | REGION}}} sed -i 's/us-central1/'"$REGION"'/g' prepare_environment.sh sed -i 's/us-central/'"$APP_REGION"'/g' prepare_environment.sh
  1. Configure the Quiz application:
. prepare_environment.sh Note: This script file:

  • Creates a Google App Engine application.
  • Exports environment variables GCLOUD_PROJECT and GCLOUD_BUCKET.
  • Runs npm install.
  • Creates entities in Google Cloud Datastore.
  • Creates a Google Cloud Pub/Sub topic.
  • Creates a Cloud Spanner Instance, Database, and Table.
  • Prints out the Google Cloud Platform Project ID.
  1. In Cloud Shell, click Open Editor.
  2. Navigate to containerengine/start.
Note: The folder structure for the Quiz application changes to reflect how it is deployed in Kubernetes Engine.

The web application is in a folder called frontend.

The worker application code that subscribes to Cloud Pub/Sub and processes messages is in a folder called backend.

There are configuration files for Docker (a Dockerfile in the frontend and backend folder) and Kubernetes Engine (\*.yaml).

Click Check my progress to verify the objective. Configure the case study application

Task 2. Creating a Kubernetes Engine cluster

In this section, you create a Kubernetes Engine cluster to host the Quiz application.

Create a Kubernetes Engine cluster

  1. In the Cloud Platform Console, on the Navigation menu, click APIs & Services.

  2. Scroll down in the list of enabled APIs and confirm that both of these APIs are enabled:

    • Kubernetes Engine API
    • Google Container Registry API

If either API is missing, click + ENABLE APIS AND SERVICES at the top. Search for the above APIs by name and enable each for your current project. (You noted the name of your GCP project above.)

  1. In the Cloud Platform Console, on the Navigation menu, click Kubernetes Engine > Clusters.
  2. Click Create.
  3. Click Configure under Standard: You manage your cluster.
  4. Configure the cluster using the following table:
Property Value
Name quiz-cluster
Location type Zonal
Zone
In the Node pools area, click default-pool In the Security > Access scopes area, Select Allow full access to all Cloud APIs
  1. Click Create.
Note: The cluster takes a couple of minutes to provision.

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

Connect to the cluster

  1. After the cluster is ready, click the three vertical dots on the right side and then click Connect.

  2. To populate Cloud Shell with the connection command, in Connect to the cluster, click Run in Cloud Shell. The command should resemble this:

gcloud container clusters get-credentials quiz-cluster --zone {{{project_0.default_zone|ZONE}}} --project {{{project_0.project_id|Project ID}}}
  1. Press ENTER to run the command in Cloud Shell.

  2. List the pods in the cluster:

kubectl get pods Note: The response should indicate that there are no pods in the cluster.

This confirms that you have configured security to allow the kubectl command-line tool to perform operations against the cluster.

Task 3. Building Docker images using Cloud Build

In this section, you create a Dockerfile for the application frontend and backend and employ Cloud Build to build images and store them in the Container Registry.

Create the Dockerfile for the frontend and backend

  1. In the Cloud Shell code editor, open frontend/Dockerfile.
  2. After the existing text, enter the Dockerfile commands to initialize the creation of a custom Docker image using Google's NodeJS App Engine image as the starting point.
Note: The image you are going to use is: gcr.io/google_appengine/nodejs.
  1. Add the Dockerfile command to copy the contents from the current folder to a destination folder in the image /app/.
  2. Add the Dockerfile command to execute npm install -g npm@8.1.3 as part of the build process to ensure that the container runs a compatible version of npm for the application.
  3. Add the Dockerfile command to execute npm update.
  4. Complete the Dockerfile by entering the statement, npm start, which executes when the container runs:

...frontend/Dockerfile

FROM gcr.io/google_appengine/nodejs RUN /usr/local/bin/install_node '>=0.12.7' COPY . /app/ RUN npm install -g npm@8.1.3 --unsafe-perm || \ ((if [ -f npm-debug.log ]; then \ cat npm-debug.log; \ fi) && false) RUN npm update CMD npm start
  1. Save the Dockerfile.
  2. Repeat the previous steps for the backend/Dockerfile file:

...backend/Dockerfile

FROM gcr.io/google_appengine/nodejs RUN /usr/local/bin/install_node '>=0.12.7' COPY . /app/ RUN npm install -g npm@8.1.3 --unsafe-perm || \ ((if [ -f npm-debug.log ]; then \ cat npm-debug.log; \ fi) && false) RUN npm update CMD npm start
  1. Save the second Dockerfile.

Build Docker images with Cloud Build

  1. In Cloud Shell, build the frontend Docker image:
gcloud builds submit -t gcr.io/$DEVSHELL_PROJECT_ID/quiz-frontend ./frontend/ Note: The files are staged into Cloud Storage, and a Docker image is built and stored in the Container Registry. This takes a few minutes.
  1. Build the backend Docker image:
gcloud builds submit -t gcr.io/$DEVSHELL_PROJECT_ID/quiz-backend ./backend/
  1. In the Cloud Platform Console, on the Navigation menu, click Artifact Registry.
Note: You should see two items: quiz-frontend and quiz-backend.
  1. Click the gcr.io repository.

  2. Click quiz-frontend.

Note: You should see the image name, tags (latest), and size (around 275 MB).

Task 4. Creating Kubernetes Deployment and Service resources

In this section, you modify template yaml files that contain the specification for Kubernetes Deployment and Service resources, and then create the resources in the Kubernetes Engine cluster.

Create a Kubernetes Deployment file

  1. In the Cloud Shell code editor, open the frontend-deployment.yaml file.
Note: The file skeleton has been created for you. Your job is to replace placeholders with values specific to your project.
  1. Replace the placeholders in the frontend-deployment.yaml file using the following values:

Placeholder Name

Value

[GCLOUD_PROJECT]

GCP Project ID
(Display the Project ID by entering
echo $GCLOUD_PROJECT in Cloud Shell)

[GCLOUD_BUCKET]

Cloud Storage bucket name for the media bucket in your project
(Display the bucket name by entering
echo $GCLOUD_BUCKET in Cloud Shell)

[FRONTEND_IMAGE_IDENTIFIER]

The frontend image identified in the form gcr.io/[Project_ID]/quiz-frontend

Note: The quiz-frontend deployment provisions three replicas of the frontend Docker image in Kubernetes pods, which are distributed across the three nodes of the Kubernetes Engine cluster.
  1. Save the file.
  2. Replace the placeholders in the backend-deployment.yaml file using the following values:

Placeholder Name

Value

[GCLOUD_PROJECT]

GCP Project ID

[GCLOUD_BUCKET]

Cloud Storage bucket ID for the media bucket in your project

[BACKEND_IMAGE_IDENTIFIER]

The backend image identified in the form gcr.io/[Project_ID]/quiz-backend

Note: The quiz-backend deployment provisions two replicas of the backend Docker image in Kubernetes pods, which are distributed across two of the three nodes of the Kubernetes Engine cluster.
  1. Save the file.
  2. Review the contents of the frontend-service.yaml file.
Note: The service exposes the frontend deployment using a load balancer. The load balancer will send requests from clients to all three replicas of the frontend pod.

Execute the Deployment and Service files

  1. In Cloud Shell, provision the quiz frontend Deployment:
kubectl create -f ./frontend-deployment.yaml
  1. Provision the quiz backend Deployment:
kubectl create -f ./backend-deployment.yaml
  1. Provision the quiz frontend Service:
kubectl create -f ./frontend-service.yaml Note: Each command provisions resources in Kubernetes Engine. This takes a few minutes to complete the process.

Click Check my progress to verify the objective. Creating Kubernetes Deployment and Service Resources

Task 5. Testing the Quiz application

In this section you review the deployed Pods and Service and navigate to the Quiz application.

Review the deployed resources

  1. In the Cloud Console, on the Navigation menu, click Kubernetes Engine.
  2. Click Kubernetes Engine > Workloads.
Note: You should see two items, quiz-frontend and quiz-backend.

You may see that the pod status is OK or in the process of being created. You may need to click refresh a couple times until you see OK.
  1. Click quiz-frontend.
  2. Scroll down to Managed pods.
Note: You should see that there are three quiz-frontend pods.
  1. Click Kubernetes Engine > Gateways, Services & Ingress.

  2. Click the Services tab.

Note: You may see that the quiz-frontend load balancer is being created or is OK.

Wait until the Service is OK before continuing.

You should see an IP address endpoint when the service is ready.
  1. Under Endpoints, click the Service IP address.
Note: You should see the Quiz application.
  1. Create a question or take a test.
Note: The application works as expected! Note: Review

Which Docker command is used to execute a command when the container is being constructed?

  1. FROM
  2. COPY
  3. RUN
  4. CMD
Which Docker command is used to execute a command when the container has been deployed?

  1. FROM
  2. COPY
  3. RUN
  4. CMD
Which Kubernetes command is used to retrieve the list of pods running on a cluster?

  1. kubectl pods list
  2. kubectl deployments list
  3. kubectl get pods
  4. kubectl get deployments

Task 6. Bonus: Deploying the Leaderboard to Kubernetes Engine

When a student completes a quiz, their answers are submitted in an API call back to the server. Your job is to capture the student-submitted answers and the correct answers and save them into Cloud Spanner.

To do this you will:

  1. Create a Cloud Pub/Sub topic called answers.
  2. Create a Cloud Spanner table called Answers with appropriate column names and data types.
  3. Post the answer data to the answers topic.
  4. Create a new answer-backend deployment, where the application subscribes to the answers topic in the console application and inserts the answer data into the Answers table.
  5. Create a handler and pug template in the quiz-frontend to display the data from Cloud Spanner when the user browses to the Leaderboard.

The details are left up to you!

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. 除非您已完成此实验或想要重新开始,否则请勿点击结束实验,因为点击后系统会清除您的工作并移除该项目

此内容目前不可用

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

太好了!

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

一次一个实验

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

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

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