arrow_back

App Dev - Deploying the Application into Kubernetes Engine: Java

Accedi Partecipa
Metti alla prova le tue conoscenze e condividile con la nostra community.
done
Accedi a oltre 700 lab pratici, badge delle competenze e corsi

App Dev - Deploying the Application into Kubernetes Engine: Java

Lab 2 ore universal_currency_alt 5 crediti show_chart Introduttivi
info Questo lab potrebbe incorporare strumenti di AI a supporto del tuo apprendimento.
Metti alla prova le tue conoscenze e condividile con la nostra community.
done
Accedi a oltre 700 lab pratici, badge delle competenze e corsi

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.

  1. Sign in to Qwiklabs 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. 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

  1. Enter the following command in Cloud Shell to clone the repository for this lab:
git clone --depth=1 https://github.com/GoogleCloudPlatform/training-data-analyst
  1. Create a soft link to your working directory:
ln -s ~/training-data-analyst/courses/developingapps/v1.3/java/kubernetesengine ~/kubernetesengine

Configure the Quiz application

  1. Change to the directory that contains the working files:
cd ~/kubernetesengine/start
  1. Update the App Engine region using the sed command.
export APP_REGION={{{project_0.startup_script.app_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 frontend application:
. prepare_environment.sh Note: This script file:

  • Creates a Google App Engine application.
  • Exports environment variables GCLOUD_PROJECT and GCLOUD_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.

  1. Launch the Cloud Shell code editor. From Cloud Shell, click Open Editor (looks like a pencil) to launch the code editor.

Cloud shell editor icon

Note: The code editor replaces the terminal. You can return later to terminal with Open Terminal.
  1. Examine the code and folder structure. In Cloud Shell code editor, navigate to training-data-analyst/courses/developingapps/v1.3/java/kubernetesengine/start.
Note: In the `kubernetesengine` folder, notice the `end` folder. The `end` folder contains the same files as the `start` folder, but each file in the `end` folder contains the complete code required to perform this lab.

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 the frontend and backend folders: configuration files for Docker. Currently empty.
  • *.yaml: configuration file for the Kubernetes Engine.
  1. In Cloud Shell, return to the terminal, then use the following command to copy the output jar for the frontend application to the frontend folder:

    cp ./target/quiz-frontend-0.0.1.jar ./frontend/
  2. Configure the Quiz backend application:

    mvn package -f pom-backend.xml
  3. 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.

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

  2. Then click Create.

  3. Click on SWITCH TO STANDARD CLUSTER and click again to confirm.

  4. 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
  5. Expand default-pool.

  6. In Security select Allow full access to all Cloud APIs.

  7. Click Create. The cluster will take a couple of minutes to provision.

Connect to the cluster

  1. After the cluster is ready, click on the 3 vertical dots and select Connect.

    Expanded More Options dropdown menu with the Connect option highlighted

  2. In Connect to the cluster, copy the first command to the clipboard.

  3. Click OK to close the Connect to the cluster window.

The command is in the form: gcloud container clusters get-credentials quiz-cluster --zone --project .

  1. Paste the command into Cloud Shell and press Enter.

    Or just click Run in Cloud Shell then Enter.

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

  1. In the Cloud Shell code editor, Open Editor, open frontend/Dockerfile.

  2. Copy and paste the following content into frontend/Dockerfile:

FROM gcr.io/google_appengine/jetty9 VOLUME /tmp ADD ./quiz-frontend-0.0.1.jar /app.jar CMD java -jar /app.jar
  1. 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.

  1. In the Cloud Shell code editor, open backend/Dockerfile.

  2. Copy and paste the following content into backend/Dockerfile:

FROM gcr.io/google_appengine/jetty9 VOLUME /tmp ADD ./quiz-backend-0.0.1.jar /app.jar CMD java -jar /app.jar
  1. Save the file.

Task 4. Build Docker images with Cloud Build

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

  2. Build the backend Docker image:

    gcloud builds submit -t gcr.io/$DEVSHELL_PROJECT_ID/quiz-backend ./backend/
  3. In the console, Click Navigation menu > Artifact Registry. You should see two folders: quiz-frontend and quiz-backend.

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

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

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

  3. Save the file.

  4. Open the backend-deployment.yamlfile and replace the placeholders in the backend-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.

  5. Save the file.

  6. 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
  2. Provision the quiz backend deployment:

    kubectl create -f ./backend-deployment.yaml
  3. 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

  1. In the console, click Navigation menu > Kubernetes Engine > Workloads. 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.

  2. Click quiz-frontend to see an overview of quiz-frontend.

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

  4. In Exposing services, under Endpoints, select the IP address and open in the new browser tab.

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

Questi contenuti non sono al momento disponibili

Ti invieremo una notifica via email quando sarà disponibile

Bene.

Ti contatteremo via email non appena sarà disponibile