arrow_back

Cloud Run for Anthos

Join Sign in

Cloud Run for Anthos

1 hour 5 Credits

GSP601

Google Cloud selp-paced labs logo

894762ebb681671c.png

Cloud Run is a managed compute platform that enables you to run stateless containers that are invocable via HTTP requests. Cloud Run is serverless: it abstracts away all infrastructure management, so you can focus on what matters most — building great applications.

Cloud Run is built from Knative, letting you choose to run your containers either fully managed with Cloud Run, or in your Google Kubernetes Engine cluster with Cloud Run on GKE.

The goal of this lab is for you to setup Cloud Run on a GKE cluster and deploy a container as a service.

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 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 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 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.
  3. If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.

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

Note: You can view the menu with a list of Google Cloud Products and Services by clicking 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. In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.

Cloud Shell icon

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

Your Cloud Platform project in this session is set to YOUR_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

(Output)

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net 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_ID>

(Example output)

[core] project = qwiklabs-gcp-44776a13dea667a6 For full documentation of gcloud, in Google Cloud, Cloud SDK documentation, see the gcloud command-line tool overview.

Prerequisites

In Cloud Shell, enable Cloud Run for Anthos in your Anthos Fleet:

gcloud container hub cloudrun enable

Press y to enable GKE Hub when prompted as well.

Create a GKE cluster with Cloud Run enabled

Create your cluster in either the Cloud Console or the Cloud Shell CLI. Pick one way to create the cluster and continue.

Cloud Console

In the Cloud Console navigate to Navigation menu > Kubernetes Engine, then click Create.

For this lab, you will use a standard Kubernetes cluster.

Click Configure next to GKE Standard.

In the Cluster Basics page, name your cluster run-gke.

Select Zonal Location Type and choose us-central1-a.

In the left menu, click on Features and check the box to Enable Cloud Run for Anthos.

Click Make Changes if prompted.

Click Create.

Cloud Shell CLI

In Cloud Shell, run the following to create a cluster:

gcloud container clusters create run-gke \ --addons=HttpLoadBalancing,CloudRun \ --machine-type=n1-standard-2 \ --enable-stackdriver-kubernetes \ --zone=us-central1-a

You are creating a cluster with the following values:

  • Name of custer: run-gke
  • Location type: zonal
  • This lab is using us-central1-a zone, but you can choose a zone that is near you.

You are creating a cluster with this configuration:

  • Cloud Run for Anthos enabled
  • Kubernetes version, latest in the Regular channel
  • Zonal cluster in us-central1-a zone. You can use regional as well for the location type.
  • Nodes with 6 vCPU (3 nodes)
  • Scopes to access cloud-platform, write to Logging, write to Monitoring

Although these instructions don't enable cluster autoscaling to resize clusters for demand, Cloud Run for Anthos on Google Cloud automatically scales instances within the cluster.

Creating the cluster will take a few moments. Please wait for the cluster to be ready before moving to the next step.

When the cluster is ready you will see a green check mark in the Cloud Console, or see the following output in Cloud Shell:

(Output)

NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS run-gke us-central1-a 1.15.12-gke.2 104.155.172.65 n1-standard-2 1.15.12-gke.2 3 RUNNING

Click Check my progress to verify the objective. Create a GKE cluster with Cloud Run enabled.

in Cloud Shell, set gcloud defaults to use your new cluster and cluster location, to avoid having to specify these when you use the gcloud command line:

gcloud config set run/cluster run-gke gcloud config set run/cluster_location us-central1-a

Add Cluster to Anthos Fleet

  1. To register your cluster, go to Navigation Menu > Anthos > Clusters. If required, click Enable to enable the Anthos API.

  2. Click Clusters and then, click on Register to see the unregistered clusters.

  3. You should see your run-gke cluster listed as an unregistered cluster from this project.

  4. Click register next to your run-gke cluster.

  5. Wait for the registration to complete before moving forward.

Deploy to Cloud Run on GKE

To deploy a container to the cluster you just created, go to Navigation menu > Anthos > Cloud Run for Anthos.

Click Create service.

Use these settings for your Cloud Run service:

  • Select Cloud Run for Anthos in Deployment platform, and use the dropdown menu to select the cluster you just created
  • Use the default Namespace
  • Give the service a name: hello-run-gke
  • Click Next
  • Enter gcr.io/cloudrun/hello as the sample container image
  • Click Next
  • Keep External as the Connectivity choice
  • Click Create and wait for the deployment to finish

anthos_cloud_run_service.png

When you see the green checkmark you have successfully created the service and deployed it to Cloud Run on GKE:

Click on the Service name. The URL is listed on the top of the page. This will be needed to test the deployed service in the next and final step.

Click Check my progress to verify the objective. Deploy to Cloud Run on GKE.

Accessing your deployed service

Once the service is deployed you can use curl to send a request and verify the service is working, using the cluster's IP address.

To avoid having to set up DNS, you'll test the deployed service by sending a request to the Istio ingress gateway (Knative is built using Istio) with the target host that should handle the request as an HTTP header. That hostname should be the URL listed in the previous deployment step and of the form: http://*service-name*.*namespace*.example.com

From the Console, go back to Kubernetes Engine section and click Services & Ingress in the left navigation panel to display the list of services.

Scroll down to the istio-ingress service and copy the IP address for the load balancer which uses port :80.

k8s-ingress.png

Use the istio-ingress IP that uses port 80. You may have to click the down arrow on the service to see it.

From Cloud Shell, use curl to access the service. Replace [YOUR-IP] with the IP address you obtained in the previous step, and if you used a service name other than "hello-run-gke" you'll need to replace that as well.

curl -v -H "Host: hello-run-gke.default.example.com" http://[YOUR-IP]

Within the top section of the response should be HTTP 200 along with the default "Congratulations | Cloud Run" HTML content :

* Trying 34.121.234.160... * TCP_NODELAY set * Expire in 200 ms for 4 (transfer 0x55bde0a2bfb0) * Connected to 34.121.234.160 (34.121.234.160) port 80 (#0) > GET / HTTP/1.1 > Host: hello-run-gke.default.example.com > User-Agent: curl/7.64.0 > Accept: */* > < HTTP/1.1 200 OK < content-type: text/html; charset=utf-8 <...> <title>Congratulations | Cloud Run</title> ...

Click Check my progress to verify the objective. Accessing your deployed service.

Clean up

Qwiklabs will take care of shutting down all the resources we've used so far, but here's what you would need to do on your own environment to save on cost and to be a good cloud citizen.

While Cloud Run for Anthos is in beta, you must delete the cluster if you wish to stop the Cloud Run on GKE components from running. This will permanently delete workloads in the cluster and all other cluster state.

To delete the cluster, go to Kubernetes Engine in the console, select the cluster and click Delete. When prompted, type your cluster name to confirm deletion and click Delete.

delete_cluster.png

Congratulations

Next Steps / Learn More

For more information on using Cloud Run and building a stateless HTTP container suitable for Cloud Run from code source and pushing it to Container Registry, see:

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 April 07, 2022
Lab Last Tested April 07, 2022

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.