App Dev - Deploying the Application into Kubernetes Engine: Java
- Overview
- Setup and requirements
- Task 1. Prepare the Quiz application
- Task 2. Creating a Kubernetes Engine cluster
- Task 3. Build Docker images using Cloud Build
- Task 4. Build Docker images with Cloud Build
- Task 5. Create a Kubernetes Deployment and Service resources
- Task 6. Test the Quiz application
- End your lab
Overview
Google Kubernetes Engine provides a managed environment for deploying, managing, and scaling your containerized applications using Google infrastructure. The environment that Kubernetes Engine provides consists of multiple machines (specifically, Google 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 Platform resources, including Cloud Build and Artifact 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 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.
-
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.
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.
-
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.
- You can list the active account name with this command:
Output:
Example output:
- You can list the project ID with this command:
Output:
Example output:
Task 1. Prepare the Quiz application
In this section, you clone the git repository containing the Quiz application, configure environment variables, and run the application.
Clone source code in Cloud Shell
- Enter the following command in Cloud Shell to clone the repository for this lab:
- Create a soft link to your working directory:
Configure the Quiz application
- Change to the directory that contains the working files:
- Update the App Engine region using the
sed
command.
- Configure the Quiz frontend application:
- Creates a Google App Engine application.
- Exports environment variables
GCLOUD_PROJECT
andGCLOUD_BUCKET.
- Runs
mvn clean install.
- Creates entities in Google Cloud Datastore.
- Creates a Google Cloud Pub/Sub topic.
- Creates a Cloud Spanner Instance, Database, and Table.
Review the code
Next review and update the Quiz application code in a code editor. You can use the Cloud Shell code editor or the shell editors that are installed on Cloud Shell, such as nano
or vim
. This lab uses the Cloud Shell code editor.
- Launch the Cloud Shell code editor. From Cloud Shell, click Open Editor (looks like a pencil) to launch the code editor.
- Examine the code and folder structure. In Cloud Shell code editor, navigate to
training-data-analyst/courses/developingapps/v1.3/java/kubernetesengine/start
.
The folder structure for the Quiz application now reflects how it's deployed in Kubernetes Engine:
- The
frontend
folder: contains the packaged output for the web application. - The
backend
folder: contains the packaged output for the console application. -
Dockerfile
in thefrontend
andbackend
folders: configuration files for Docker. Currently empty. -
*.yaml
: configuration file for the Kubernetes Engine.
-
In Cloud Shell, return to the terminal, then use the following command to copy the output
jar
for the frontend application to thefrontend
folder:cp ./target/quiz-frontend-0.0.1.jar ./frontend/ -
Configure the Quiz backend application:
mvn package -f pom-backend.xml -
Copy the output jar for the backend application to the
backend
folder:cp ./target/quiz-backend-0.0.1.jar ./backend/
Task 2. Creating a Kubernetes Engine cluster
In this section you create a Google Kubernetes Engine cluster to host the Quiz application.
-
In the Console, click Navigation menu > Kubernetes Engine > Clusters.
-
Then click Create.
-
Click on SWITCH TO STANDARD CLUSTER and click again to confirm.
-
To configure the cluster, use the specified values for the properties listed in the following table; leave the properties not listed at the default values:
In Cluster basics:
Property Value Name quiz-cluster
Zone -
Expand default-pool.
-
In Security select Allow full access to all Cloud APIs.
-
Click Create. The cluster will take a couple of minutes to provision.
Connect to the cluster
-
After the cluster is ready, click on the 3 vertical dots and select Connect.
-
In Connect to the cluster, copy the first command to the clipboard.
-
Click OK to close the Connect to the cluster window.
The command is in the form: gcloud container clusters get-credentials quiz-cluster --zone
.
-
Paste the command into Cloud Shell and press Enter.
Or just click Run in Cloud Shell then Enter.
-
List the pods in the cluster:
kubectl get pods
The response should indicate that there are no pods in the cluster.
This response confirms that you have configured security to allow the kubectl
command-line tool to perform operations against the cluster.
Task 3. Build 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 Artifact Registry.
Create the Dockerfile for the frontend
-
In the Cloud Shell code editor, Open Editor, open
frontend/Dockerfile
. -
Copy and paste the following content into
frontend/Dockerfile
:
- Save the file.
What this script does:
This script is a series of Dockerfile commands.
-
The first command,
FROM gcr.io/google_appengine/jetty9
, initializes the creation of a custom Docker image using the Google App Engine Jetty 9 image,gcr.io/google_appengine/jetty9
as the starting point. -
This second command,
VOLUME /tmp
, creates a volume in the container's file system with the path of/tmp
. -
The third command,
ADD ./quiz-frontend-0.0.1.jar /app.jar
, adds the Jar file,uiz-frontend-0.0.1.jar
for the frontend generated by the Maven packaging process as part of the build process. -
This fourth and last command,
CMD java -jar /app.jar
, executes when the container runs.
Create the Dockerfile for the backend
You create the dockerfile for the backend the same way you created the frontend, except the jar
file is added to the backend.
-
In the Cloud Shell code editor, open
backend/Dockerfile
. -
Copy and paste the following content into
backend/Dockerfile
:
- Save the file.
Task 4. Build Docker images with Cloud Build
-
In Cloud Shell, Open Terminal, enter 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 will be built and stored in the Artifact Registry. It will take a few seconds.
-
Build the backend Docker image:
gcloud builds submit -t gcr.io/$DEVSHELL_PROJECT_ID/quiz-backend ./backend/ -
In the console, Click Navigation menu > Artifact Registry. You should see two folders: quiz-frontend and quiz-backend.
-
Click quiz-frontend. You should see the image name (a hash), tags (latest), and other details.
Task 5. Create a Kubernetes Deployment and Service resources
In this section you will 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
-
In the code editor, open the
frontend-deployment.yaml
file. The file skeleton has been created for you. Your job is to replace placeholders with values specific to your project. -
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 ID for the media bucket in your project. The bucket ID is [GCP_Project_ID]-media
.[FRONTEND_IMAGE_IDENTIFIER]
The frontend image identified in the form gcr.io/[GCP_Project_ID]/quiz-frontend
Only replace values surrounded with
[]
.The result yaml file looks something like this:
apiVersion: apps/v1beta1 kind: Deployment metadata: name: quiz-frontend labels: app: quiz-app spec: replicas: 3 template: metadata: labels: app: quiz-app tier: frontend spec: containers: - name: quiz-frontend image: gcr.io/qwiklabs-gcp-04-3ad1f39f2114/quiz-frontend imagePullPolicy: Always ports: - name: http-server containerPort: 8080 env: - name: GCLOUD_PROJECT value: qwiklabs-gcp-04-3ad1f39f2114 - name: GCLOUD_BUCKET value: qwiklabs-gcp-04-3ad1f39f2114-media The quiz-frontend deployment provisions three replicas of the frontend Docker image in Kubernetes pods, distributed across the three nodes of the Kubernetes Engine cluster.
-
Save the file.
-
Open the
backend-deployment.yaml
file and replace the placeholders in thebackend-deployment.yaml
file using the following values:Placeholder Name Value [GCLOUD_PROJECT] GCP Project ID (or use echo $GCLOUD_PROJECT
)[GCLOUD_BUCKET] Cloud Storage bucket ID for the media bucket in your project. This will be the same bucket used in frontend-deployment.yaml
. (The bucket ID is[GCP_Project_ID]-media
.)[BACKEND_IMAGE_IDENTIFIER]
The backend image identified in the form gcr.io/[GCP_Project_ID]/quiz-backend
The quiz-backend deployment provisions one replica of the backend Docker image in Kubernetes pods, placed on one of the three nodes of the Kubernetes Engine cluster.
-
Save the file.
-
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
-
In Cloud Shell, provision the quiz frontend deployment:
kubectl create -f ./frontend-deployment.yaml -
Provision the quiz backend deployment:
kubectl create -f ./backend-deployment.yaml -
Provision the quiz frontend Service:
kubectl create -f ./frontend-service.yaml
Each command provisions resources in Kubernetes Engine. It will take a few minutes to complete the process.
Task 6. Test the Quiz application
In this section review the deployed Pods and Service and navigate to the Quiz application.
Review the deployed resources
-
In the console, click Navigation menu > Kubernetes Engine > Workloads. You should see two items:
quiz-frontend
andquiz-backend
.You may see that the pod status is OK or in the process of being created.
-
Click quiz-frontend to see an overview of
quiz-frontend
. -
Scroll down to see Managed Pods.
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.
-
In Exposing services, under Endpoints, select the IP address and open in the new browser tab.
-
Take a test to verify the application works as expected.
End your lab
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:
- 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 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.