arrow_back

App Dev: Deploying the Application into Kubernetes Engine - Python

Join Sign in

App Dev: Deploying the Application into Kubernetes Engine - Python

50 minutes 1 Credit

GSP188

Google Cloud selp-paced labs logo

Overview

Google Kubernetes Engine provides a managed environment for deploying, managing, and scaling your containerized applications using Google infrastructure. The environment Kubernetes Engine provides consists of multiple machines (specifically, Compute Engine instances) grouped together to form a cluster.

Kubernetes provides the mechanisms through which you interact with your cluster. You use Kubernetes commands and resources to deploy and manage your applications, perform administration tasks and set policies, and monitor the health of your deployed workloads.

In this lab, you deploy the Quiz application into Kubernetes Engine, leveraging Google Cloud resources, including Container Builder and Container Registry, and Kubernetes resources, such as 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 Container Builder 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

Qwiklabs setup

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).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

Cloud Console

How to start your lab and sign in to the Google Cloud Console

  1. 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 Console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Console. 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.
  3. If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.

  4. Copy the Password from the Lab Details panel and paste it into the Welcome dialog. Click Next.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Skills Boost credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  5. 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 Cloud Console opens in this tab.

Note: You can view the menu with a list of Google Cloud Products and Services by clicking the Navigation menu at the top-left. Navigation menu icon

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.

  1. In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.

Cloud Shell icon

  1. 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. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session 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.

  1. (Optional) You can list the active account name with this command:

gcloud auth list

(Output)

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:

gcloud config list project

(Output)

[core] project = <project_ID>

(Example output)

[core] project = qwiklabs-gcp-44776a13dea667a6 For full documentation of gcloud, in Google Cloud, Cloud SDK documentation, see the gcloud command-line tool overview.

Launch the Cloud Shell code editor

From Cloud Shell, click Open Editor icon to launch the code editor.

editor.png

Prepare the Quiz 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

Click Open Terminal and clone the repository for the lab.

git clone https://github.com/GoogleCloudPlatform/training-data-analyst

Create a soft link as a shortcut to the working directory.

ln -s ~/training-data-analyst/courses/developingapps/v1.2/python/kubernetesengine ~/kubernetesengine

Configure the Quiz application

Change the directory that contains the sample files for this lab.

cd ~/kubernetesengine/start

Configure the Quiz application.

. prepare_environment.sh

This script file:

  • Creates a Google App Engine application.
  • Exports environment variables GCLOUD_PROJECT and GCLOUD_BUCKET.
  • Updates pip then runs pip install -r requirements.txt.
  • Creates entities in Google Cloud Datastore.
  • Creates a Google Cloud Pub/Sub topic.
  • Creates a Cloud Spanner Instance, Database, and Table.
  • Prints out the Project ID.

The Quiz application is configured when you see the following message:

Example output message

Creating Cloud Pub/Sub topic Created topic [projects/qwiklabs-gcp-92b7e5716e0cbf7e/topics/feedback]. Created subscription [projects/qwiklabs-gcp-92b7e5716e0cbf7e/subscriptions/worker-subscription]. Creating Cloud Spanner Instance, Database, and Table Creating instance...done. Creating database...done. Project ID: qwiklabs-gcp-92b7e5716e0cbf7e

Click Check my progress to verify the objective. Configure the Quiz application

Review the code

In this section you examine the application files.

To view and edit files, you can use the shell editors that are installed in Cloud Shell, such as nano or vim or the Cloud Shell code editor. This lab uses the Cloud Shell code editor.

Examine the code

Navigate to training-data-analyst/courses/developingapps/v1.2/python/kubernetesengine/start.

The folder structure for the Quiz application reflects how it will be 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 backend-deployment and frontend-deployment Kubernetes Engine .yaml files.

Create and connect to a Kubernetes Engine Cluster

Create a Kubernetes Engine Cluster

  1. In the Cloud Platform Console, click Navigation menu > Kubernetes Engine > Clusters.

    kubernetes-cluster.png

  2. Click CREATE.

  3. CONFIGURE the Standard cluster. Set the following fields to the provided values, leave all others at the default value:

Property

Value

Name

quiz-cluster

Zone

us-central1-b

default Pool > Security > Access scopes

Select Allow full access to all Cloud APIs

  1. Click CREATE. The cluster takes a few minutes to provision.

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

Connect to the cluster

In this section you connect the Quiz application to the kubernetes cluster.

  1. When the cluster is ready, click on the Actions icon and select Connect.

connect.png

  1. In Connect to the cluster, click Run in Cloud Shell to populated Cloud shell with the command that resembles gcloud container clusters get-credentials quiz-cluster --zone us-central1-b --project [Project-ID]. Press Enter to run the commnand in Cloud Shell.

  2. Run the following command to list the pods in the cluster:

kubectl get pods

The response should be No resources found because there are no pods in the cluster. It confirms that you have configured security to allow the kubectl command-line tool to perform operations against the cluster.

Build Docker Images using Container Builder

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

Create the Dockerfile for the frontend and backend

In the Cloud Shell code editor, open frontend/Dockerfile. You will now add a block of code that does the following:

  • Enters the Dockerfile command to initialize the creation of a custom Docker image using Google's Python App Engine image as the starting point.
  • Writes the Dockerfile commands to activate a virtual environment.
  • Writes the Dockerfile command to execute pip install as part of the build process.
  • Writes the Dockerfile command to add the contents of the current folder to the /app path in the container.
  • Completes the Dockerfile by entering the statement, gunicorn ..., that executes when the container runs. Gunicorn (Green Unicorn) is an HTTP server that supports the Python Web Server Gateway Interface (WSGI) specification.

Copy and paste the following to Dockerfile:

FROM gcr.io/google_appengine/python RUN virtualenv -p python3.7 /env ENV VIRTUAL_ENV /env ENV PATH /env/bin:$PATH ADD requirements.txt /app/requirements.txt RUN pip install -r /app/requirements.txt ADD . /app CMD gunicorn -b 0.0.0.0:$PORT quiz:app

Open the backend/Dockerfile file and copy and paste the following code:

FROM gcr.io/google_appengine/python RUN virtualenv -p python3.7 /env ENV VIRTUAL_ENV /env ENV PATH /env/bin:$PATH ADD requirements.txt /app/requirements.txt RUN pip install -r /app/requirements.txt ADD . /app CMD python -m quiz.console.worker

Build Docker images with Container Builder

  1. In Cloud Shell, make sure you are in the start folder:

cd ~/kubernetesengine/start
  1. Run the following command to build the frontend Docker image:

gcloud builds submit -t gcr.io/$DEVSHELL_PROJECT_ID/quiz-frontend ./frontend/

The files are staged into Cloud Storage, and a Docker image is built and stored in the Container Registry. It takes a few minutes.

Ignore any incompatibility messages you see in the output messages.

  1. Now run the following command to build the backend Docker image:

gcloud builds submit -t gcr.io/$DEVSHELL_PROJECT_ID/quiz-backend ./backend/

When the backend Docker image is ready you see these last messages:

DONE ----------------------------------------------------------------------------------------------------------------------- ID CREATE_TIME DURATION SOURCE IMAGES STATUS be0326f4-3f6f-42d6-850f-547e260dd4d7 2018-06-13T22:20:16+00:00 50S gs://qwiklabs-gcp-3f89d0745056ee31_cloudbuil d/source/1528928414.79-4914d2a972f74e188f40ced135662b7d.tgz gcr.io/qwiklabs-gcp-3f89d0745056ee31/quiz-backend (+1 more ) SUCCESS
  1. In the Cloud Platform Console, from the Navigation menu menu, click Container Registry. You should see two pods: quiz-frontend and quiz-backend.

backend_frontend.png

  1. Click quiz-frontend.

container-image.png

Click Check my progress to verify the objective. Build Docker Images using Container Builder

Create Kubernetes Deployment and Service Resources

In this section, you will modify the 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.
  1. Replace the placeholders in the frontend-deployment.yaml file using the following values:

Placeholder Name

Value

[GCLOUD_PROJECT]

Project ID(Display the Project ID by enteringecho $GCLOUD_PROJECT in Cloud Shell)

[GCLOUD_BUCKET]

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

[FRONTEND_IMAGE_IDENTIFIER]

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

  1. Save the file.
  2. Replace the placeholders in the backend-deployment.yaml file using the following values:

Placeholder Name

Value

[GCLOUD_PROJECT]

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

  1. Save the file.
  2. Review the contents of the frontend-service.yaml file.

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

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

Test 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, select Navigation menu > Kubernetes Engine.
  2. Click Workloads in the left menu.
  1. Click quiz-frontend. In the Managed pods section, there are three quiz-frontend pods.

  2. In the Exposing services section near the bottom, find the Endpoints section and copy the the IP address and paste it into the URL field of a new browser tab or window:

app.png

  1. This opens the Quiz application, which means you successfully deployed the application! You can end your lab here or use the remainder of the time to build some quizzes.

Congratulations!

This concludes the self-paced lab, App Dev: Deploying the Application into Kubernetes Engine - Python. You leveraged Google Cloud resources and Kubenetes resources to deploy a Quiz application.

completion_badge_Application_Development_-_Python-135.png completion_badge_Cloud_Development-135.png

Finish your Quest

This self-paced lab is part of the Application Development - Python and Cloud Development Quests. A Quest is a series of related labs that form a learning path. Completing this Quest earns you the badge above, to recognize your achievement. You can make your badge (or badges) public and link to them in your online resume or social media account. Enroll in a Quest and get immediate completion credit if you've taken this lab. See other available Qwiklabs Quests.

Next steps / learn more

Learn more about Kubernetes Engine.

Manual last updated October 18, 2021
Lab last tested July 19, 2021

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.