Loading...
No results found.

    08

    Logging and Monitoring in Google Cloud

    Get access to 700+ labs and courses

    Log Analytics on Google Cloud

    Lab 1 hour universal_currency_alt 1 Credit show_chart Introductory
    info This lab may incorporate AI tools to support your learning.
    Get access to 700+ labs and courses

    GSP1088

    Overview

    Cloud Logging is a fully managed service that allows you to store, search, analyze, monitor, and alert on logging data and events from Google Cloud. In this lab you learn about the features and tools provided by Cloud Logging to gain insight into your applications.

    What you'll learn

    In this lab you learn how to:

    • Use Cloud Logging effectively and get insight about applications running on Google Kubernetes Engine (GKE).
    • Effectively build and run queries using log analytics.

    The demo application used in the lab

    In this lab, you work through a scenario based on this microservices demo app deployed to a GKE cluster. This demo app has many microservices and dependencies embedded in it.

    Setup and requirements

    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 are made available to you.

    This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials 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 (recommended) or private browser window to run this lab. This prevents 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: Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that 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 dialog opens for you to select your payment method. On the left is the Lab Details pane 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 pane.

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

    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 access Google Cloud products and services, click the Navigation menu or type the service or product name in the Search field.

    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 at the top of the Google Cloud console.

    2. Click through the following windows:

      • Continue through the Cloud Shell information window.
      • Authorize Cloud Shell to use your credentials to make Google Cloud API calls.

    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.

    Task 1. Infrastructure setup

    Verify the GKE cluster

    Connect to a GKE cluster and validate that it's been created correctly.

    1. In Cloud Shell, set the zone in gcloud:
    gcloud config set compute/zone {{{project_0.default_zone|placeholder}}}
    1. Use the following command to see the cluster's status:
    gcloud container clusters list

    You should see a similar status:

    NAME: day2-ops LOCATION: us-west1 MASTER_VERSION: 1.31.5-gke.1023000 MASTER_IP: 34.169.197.173 MACHINE_TYPE: e2-standard-2 NODE_VERSION: 1.31.5-gke.1023000 NUM_NODES: 3 STATUS: RUNNING

    The cluster status should say RUNNING. If it's still PROVISIONING, wait a moment and run the command above again. Repeat until the status is RUNNING.

    You can also check the progress in the Google Cloud console. In the Navigation menu () click Kubernetes Engine > Clusters.

    1. Once your cluster has RUNNING status, get the cluster credentials:
    gcloud container clusters get-credentials day2-ops --region {{{project_0.default_region|placeholder}}}

    Your output should look like this:

    Fetching cluster endpoint and auth data. kubeconfig entry generated for day2-ops.
    1. Verify that the nodes have been created:
    kubectl get nodes

    Your output should look like this:

    NAME STATUS ROLES AGE VERSION gke-day2-ops-day2-ops-node-pool-0d9c7ef3-xc44 Ready <none> 11m v1.31.5-gke.1023000 gke-day2-ops-day2-ops-node-pool-b17ac6d6-tch7 Ready <none> 11m v1.31.5-gke.1023000 gke-day2-ops-day2-ops-node-pool-ed506ae8-wsc5 Ready <none> 11m v1.31.5-gke.1023000

    Task 2. Deploy the application

    Next, you deploy a microservices application called Online Boutique to your cluster to create an actual workload you can monitor.

    1. Run the following to clone the repo:
    git clone https://github.com/GoogleCloudPlatform/microservices-demo.git
    1. Navigate to the microservices-demo directory:
    cd microservices-demo
    1. Install the app using kubectl:
    kubectl apply -f release/kubernetes-manifests.yaml
    1. Confirm that everything is running correctly:
    kubectl get pods

    The output should look similar to the output below. Re-run the command until all pods are reporting a Running status before moving to the next step.

    NAME READY STATUS RESTARTS AGE adservice-55f94cfd9c-4lvml 1/1 Running 0 20m cartservice-6f4946f9b8-6wtff 1/1 Running 2 20m checkoutservice-5688779d8c-l6crl 1/1 Running 0 20m currencyservice-665d6f4569-b4sbm 1/1 Running 0 20m emailservice-684c89bcb8-h48sq 1/1 Running 0 20m frontend-67c8475b7d-vktsn 1/1 Running 0 20m loadgenerator-6d646566db-p422w 1/1 Running 0 20m paymentservice-858d89d64c-hmpkg 1/1 Running 0 20m productcatalogservice-bcd85cb5-d6xp4 1/1 Running 0 20m recommendationservice-685d7d6cd9-pxd9g 1/1 Running 0 20m redis-cart-9b864d47f-c9xc6 1/1 Running 0 20m shippingservice-5948f9fb5c-vndcp 1/1 Running 0 20m
    1. Run the following commands to get the external IP of the application.
    export EXTERNAL_IP=$(kubectl get service frontend-external -o jsonpath="{.status.loadBalancer.ingress[0].ip}") echo $EXTERNAL_IP

    An IP address is returned only after the service has been deployed. You may need to repeat the commands until there's an external IP address assigned.

    When the IP address is assigned, your output should be similar to this:

    35.222.235.86
    1. Finally, confirm that the app is up and running:
    curl -o /dev/null -s -w "%{http_code}\n" http://${EXTERNAL_IP}

    Your confirmation looks like this:

    200

    After the application is deployed, you can also view the status in the console.

    1. On the Kubernetes Engine page, in the left pane, click Workloads to see that all the pods are OK.

    2. In the left pane, click Gateways, Services & Ingress and then verify that all services are OK.

    Open the application

    Under Gateways, Services & Ingress, click the Endpoint IP of the service frontend-external.

    It should open a demo web page similar to the following:

    Note: You may need to click a "Continue to site" button if prompted.

    Click Check my progress to verify the objective. Deploy application

    Task 3. Manage log buckets

    There are two ways to enable Log Analytics. One way is to upgrade an existing bucket. The other is to create a new log bucket with Log Analytics enabled.

    Upgrade an existing bucket

    You can use the following steps to upgrade an existing log bucket.

    1. In the console, in the Navigation menu (), click View All Products > Observability > Logging > Logs storage. Pin the section for easier access later in the lab.

    2. Click UPGRADE for an existing bucket, for example, the Default bucket.

    3. Click UPGRADE in the confirmation dialog.

    4. Wait for the upgrade to complete.

    Initially the status changes to Not eligible for upgrade before you see the Upgraded status.

    1. Click OPEN and select the view _AllLogs.

    The Log Analytics page opens.

    Create a new Log bucket

    Alternatively, you can configure Cloud Logging to create a new log bucket with Log Analytics enabled.

    1. On the console title bar, type logging in the Search field, and then Logging in the search results.

    2. In the left pane, click Logs storage and then click CREATE LOG BUCKET at the top of the Logs Storage window.

    3. Provide a name, such as day2ops-log to the bucket.

    4. Check both Upgrade to use Log Analytics and Create a new BigQuery dataset that links to this bucket.

    5. Type in a dataset name, such as: day2ops_log

    Selecting Create a linked dataset in BigQuery creates a dataset for you in BigQuery if it does not exist. This lets you run queries in BigQuery.

    1. Finally, click Create bucket to create the log bucket.

    Click Check my progress to verify the objective. Create a Log bucket

    Write to the new Log bucket

    There are a couple ways to create a log sink to route log entries to the new log bucket:

    • From the Logs Router directly.
    • From Logs Explorer. You can run log queries to select and filter the logs you are interested in when you create a sink. The advantage to this approach is the log query is automatically copied to the sink configuration as the filter.
    1. Still in the Logging page, click Logs Explorer in the left pane.

    2. In the top-right, enable Show query and run the following query in the query field:

    resource.type="k8s_container"
    1. Above the Log list, click Actions > Create sink.

    2. Provide a name such as day2ops-sink as the sink name.

    3. Click NEXT.

    4. Select Logging bucket in the sink service dropdown list.

    5. Select the new log bucket you just created.

    6. Click NEXT.

    You should see the resource type query already in the filter.

    1. Click CREATE SINK.

    Wait about a minute and your sink should be created.

    Click Check my progress to verify the objective. Create the log sink

    Read from the new Log bucket

    Go back to the Logs Explorer (in the left pane, click Logs explorer). Notice in the Log fields list, in the Resource Type section, that there are many different resource types for the logs.

    1. To view the logs in the new log bucket, at the top left of the Logs Explorer window, click Project logs > Log view > the new log bucket (that you just created).

    2. Click APPLY.

    You see that Kubernetes Containers is now the only resource type and there are fewer log entries. That's because only filtered logs are sent to the bucket.

    Task 4. Log analysis

    • On the left pane, click Log Analytics to open the Log Analytics page.

    If your query field is empty or you forget which table to use, click the Query button to get the sample query back.

    You can run your own queries in the query field. This task provides some examples.

    Important: The log view name in the FROM clause is different for the log buckets. Be sure you use the correct view name. You can use the previous step to verify.

    Example query to find the most recent errors

    Use this query to find the most recent errors for from the containers:

    SELECT TIMESTAMP, JSON_VALUE(resource.labels.container_name) AS container, json_payload FROM `{{{project_0.project_id | "PROJECT_ID"}}}.global.day2ops-log._AllLogs` WHERE severity="ERROR" AND json_payload IS NOT NULL ORDER BY 1 DESC LIMIT 50 Note: If you don't see immediate results, wait a few moments and try again or proceed to the next query.

    Example query to find the min, max, and average latency

    Use this query to find the min, max, and average latencies in a timeframe for the frontend service:

    SELECT hour, MIN(took_ms) AS min, MAX(took_ms) AS max, AVG(took_ms) AS avg FROM ( SELECT FORMAT_TIMESTAMP("%H", timestamp) AS hour, CAST( JSON_VALUE(json_payload, '$."http.resp.took_ms"') AS INT64 ) AS took_ms FROM `{{{project_0.project_id | "PROJECT_ID"}}}.global.day2ops-log._AllLogs` WHERE timestamp > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 24 HOUR) AND json_payload IS NOT NULL AND SEARCH(labels, "frontend") AND JSON_VALUE(json_payload.message) = "request complete" ORDER BY took_ms DESC, timestamp ASC ) GROUP BY 1 ORDER BY 1

    Example query to find the number of Product page visits

    Use this query to find how many times users visit a certain product page in the past hour:

    SELECT count(*) FROM `{{{project_0.project_id | "PROJECT_ID"}}}.global.day2ops-log._AllLogs` WHERE text_payload like "GET %/product/L9ECAV7KIM %" AND timestamp > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 HOUR)

    Example query to find how many sessions end with a shopping cart checkout

    Use this query to find how many sessions end up with checkout (POST call to the /cart/checkout service):

    SELECT JSON_VALUE(json_payload.session), COUNT(*) FROM `{{{project_0.project_id | "PROJECT_ID"}}}.global.day2ops-log._AllLogs` WHERE JSON_VALUE(json_payload['http.req.method']) = "POST" AND JSON_VALUE(json_payload['http.req.path']) = "/cart/checkout" AND timestamp > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 HOUR) GROUP BY JSON_VALUE(json_payload.session)

    Congratulations!

    You now have experience using Cloud Logging to get insight about applications running on GKE, and you built and ran queries using log analytics.

    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 March 21, 2025

    Lab Last Tested February 20, 2025

    Copyright 2025 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.

    Previous Next

    Before you begin

    1. Labs create a Google Cloud project and resources for a fixed time
    2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
    3. On the top left of your screen, click Start lab to begin

    This content is not currently available

    We will notify you via email when it becomes available

    Great!

    We will contact you via email if it becomes available

    One lab at a time

    Confirm to end all existing labs and start this one

    Use private browsing to run the lab

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