Deploy the cluster and required resources using Terraform
Cloud Operations for GKE
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.
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 here. We encourage any and all to contribute to our assets!
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.
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).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud Console
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
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.
If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.
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.
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.
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.
- In the Cloud Console, in the top right toolbar, click the Activate 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. The output contains a line that declares the PROJECT_ID for this session:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
(Optional) You can list the active account name with this command:
(Optional) You can list the project ID with this command:
gcloud, in Google Cloud, Cloud SDK documentation, see the gcloud command-line tool overview.
Permission deniedwhen 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 listhas the necessary permissions to create resources. If it does, regenerate the application default credentials using
gcloud auth application-default login.
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.
Run the following to set a region and zone for your lab (you can use the region/zone that's best for you):
Copy the resources needed for this lab by running:
Go into the directory of the demo:
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
Now 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 > 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:
./terraform/terraform.tfvarsfor Terraform variables
./manifests/prometheus-service-sed.yamlfor the Prometheus policy to be created in Cloud Monitoring
terraform initto prepare Terraform to create the infrastructure
terraform applyto 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 (do not copy)
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.
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.
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. You can click on Kubernetes Engine to monitor the progress.
In order to validate that resources are installed and working correctly, run:
Example Output (do not copy)
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.
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.
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. Click on the Incident to see the details.
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:
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 (do not copy)
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.
Finish Your Quest
This self-paced lab is part of the Qwiklabs Google Cloud's Operations Suite on GKE Quest. 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 this Quest and get immediate completion credit if you've taken this lab. See other available Qwiklabs Quests.
Take your next lab
Continue your Quest with Using Cloud Trace on Kubernetes Engine, or check out these suggestions:
Next Steps / Learn More
Google Cloud Training & 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 December 8, 2021
Lab Last Tested December 8, 2021
Copyright 2022 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.