arrow_back

Cloud Operations for GKE

Join Sign in
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

Cloud Operations for GKE

Lab 1 hour universal_currency_alt 1 Credit show_chart Introductory
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

GKE logo

GSP497

Google Cloud self-paced labs logo

Overview

Kubernetes Engine Monitoring is a new Cloud Monitoring feature that more tightly integrates with GKE to better show you key stats about your cluster and the workloads and services running in it. Included in the new feature is functionality to import, as native Cloud Monitoring metrics, metrics from pods with Prometheus endpoints. This allows you to use Cloud Monitoring native alerting functionality with your Prometheus metrics without any additional workload.

In this lab you will set up Monitoring and visualizing metrics from a Kubernetes Engine cluster. It makes use of Terraform, a declarative Infrastructure as Code tool that enables configuration files to be used to automate the deployment and evolution of infrastructure in the cloud. The logs from the Kubernetes Engine cluster will be leveraged to walk through the monitoring capabilities of Cloud Monitoring.

Note: The setup of the Cloud Monitoring workspace is not automated with a script because it is currently not supported through Terraform or via the gcloud command line tool.

This lab was created by GKE Helmsman engineers to help you grasp a better understanding of Cloud Monitoring with Kubernetes Engine. You can view this demo on Github. We encourage any and all to contribute to our assets!

Architecture

This lab will create a Kubernetes Engine cluster that has a sample application deployed to it. The logging and metrics for the cluster are loaded into Cloud Logging by default. In the tutorial, a Cloud Monitoring account will be set up to view the metrics captured.

Monitoring Architecture

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.

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 Cloud 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 Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    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 below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab Details panel.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab Details panel.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. 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 Google Cloud console opens in this tab.

Note: To view a menu with a list of Google Cloud products and services, click 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. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

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 {{{project_0.project_id | "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
  1. Click Authorize.

Output:

ACTIVE: * ACCOUNT: {{{user_0.username | "ACCOUNT"}}} 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_0.project_id | "PROJECT_ID"}}} Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide. Troubleshooting: In a production environment, if the install script fails with a Permission denied when running Terraform, it's probably because the credentials that Terraform is using do not provide the necessary permissions to create resources in the selected project. Ensure that the account listed in gcloud config list has the necessary permissions to create resources. If it does, regenerate the application default credentials using gcloud auth application-default login.

Task 1. Set region/zone

Set your region and zone

Certain Compute Engine resources live in regions and zones. A region is a specific geographical location where you can run your resources. Each region has one or more zones.

Learn more about regions and zones and see a complete list in Regions & Zones documentation.

Run the following to set a region and zone for your lab (you can use the region/zone that's best for you):

gcloud config set compute/region {{{ project_0.default_region | REGION }}} gcloud config set compute/zone {{{ project_0.default_zone | ZONE }}}

Task 2. Clone demo

  1. Copy the resources needed for this lab by running:
gsutil cp gs://spls/gsp497/gke-monitoring-tutorial.zip . unzip gke-monitoring-tutorial.zip
  1. Go into the directory of the demo:
cd gke-monitoring-tutorial

Configure authentication

The Terraform configuration will execute against your Google Cloud environment and create a Kubernetes Engine cluster running a simple application. The configuration will use your personal account to build out these resources. In this lab, you use Cloud Shell which automatically has your project and student account configured and authenticated.

Create a Monitoring Metrics Scope

Set up a Monitoring Metrics Scope that's tied to your Google Cloud Project. The following steps create a new account that has a free trial of Monitoring.

  • In the Cloud Console, click Navigation menu (Navigation menu icon) > Monitoring.

When the Monitoring Overview page opens, your metrics scope project is ready.

Deploying the cluster

  • The infrastructure and Cloud Monitoring alert policy required by this project can be deployed by executing:
make create

If you need to override any of the defaults in the Terraform variables file, simply replace the desired value(s) to the right of the equals sign(s). Be sure your replacement values are still double-quoted.

Note: The Terraform will perform the following tasks:

1. Read your project & zone configuration to generate a couple of config files:

* ./terraform/terraform.tfvars for Terraform variables.

* ./manifests/prometheus-service-sed.yaml for the Prometheus policy to be created in Cloud Monitoring.

2. Run terraform init to prepare Terraform to create the infrastructure

3. Run terraform apply to actually create the infrastructure & Cloud Monitoring alert policy

If no errors are displayed then after a few minutes you should see an output as below.

Example output:

Apply complete! Resources: 5 added, 0 changed, 0 destroyed. Outputs: cluster_name = stackdriver-monitoring-tutorial primary_location = {{{ project_0.default_zone | ZONE }}} Note: If you get deprecation warnings related to the zone variable, please ignore it and move forward in the lab.

You can verify created Kubernetes Engine cluster in the Cloud Console.

Test completed task

Click Check my progress to verify your performed task. If you have successfully deployed resources using Terraform, you will see an assessment score.

Deploy the cluster and required resources using Terraform

How does Terraform work?

Following the principles of Infrastructure as Code and Immutable Infrastructure, Terraform supports the writing of declarative descriptions of the desired state of infrastructure. When the descriptor is applied, Terraform uses Google Cloud APIs to provision and update resources to match. Terraform compares the desired state with the current state so incremental changes can be made without deleting everything and starting over. For instance, Terraform can build out Google Cloud projects and compute instances, etc., even set up a Kubernetes Engine cluster and deploy applications to it. When requirements change, the descriptor can be updated and Terraform will adjust the cloud infrastructure accordingly.

This demo script will start up a Kubernetes Engine cluster and deploy a simple sample application to it. By default, Kubernetes Engine clusters in Google Cloud are provisioned with a pre-configured Fluentd-based collector that forwards logs to Cloud Monitoring.

Using Kubernetes Engine Monitoring

While you're waiting for the cluster to finish creating, read through this guide on how to observe your cluster with the new Kubernetes Engine Monitoring UI, see Observing Your Kubernetes Clusters.

Task 3. Validation

If no errors are displayed during deployment, after a few minutes you should see your Kubernetes Engine cluster in the Cloud Console with the sample application deployed.

  1. You can click on Kubernetes Engine to monitor the progress.

  2. In order to validate that resources are installed and working correctly, run:

make validate

Example output:

Fetching cluster endpoint and auth data. kubeconfig entry generated for stackdriver-monitoring-tutorial. App is deployed.

Native Prometheus integration

The Terraform code included a Cloud Monitoring alerting policy that is watching a metric that was originally imported from a Prometheus endpoint.

  1. Still in the Cloud Monitoring window, in the left menu, click Alerting. The Policies section lists all the policies, including the alerting policy called Prometheus mem alloc.

  2. Clicking on the policy provides much more detail.

After the scripts execute it may take a few minutes for the Metrics to appear on the Overview page. Refresh your page periodically to make sure that you're seeing the latest updates.

You will eventually see an Incident when the mem alloc above 12 policy gets violated.

  1. Click on the Incident to see the details.

Task 4. Teardown

  • When you are finished, and you are ready to clean up the resources that were created, you can run the following command to remove all resources:
make teardown

This command uses the terraform destroy command to remove the infrastructure. Terraform tracks the resources it creates so it can tear them all backdown.

If no errors are displayed then after a few minutes you should see an output as below (do not move forward until you get the output as below).

Example output:

.... Destroy complete! Resources: 5 destroyed. Note: If you get deprecation warnings related to the zone variable, please ignore it.

Test completed task

Click Check my progress to verify your performed task. If you have successfully removed the infrastructure using Terraform, you will see an assessment score.

Teardown (Clean-up)

Congratulations!

Next steps / Learn more

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated September 29, 2023

Lab Last Tested September 29, 2023

Copyright 2024 Google LLC. This software is provided as-is, without warranty or representation for any use or purpose. Your use of it is subject to your agreement with Google.