Hello Cloud Run for Anthos Codelab

6a5cf23c8e20491f.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 for Anthos.

The goal of this codelab is for you to setup Cloud Run targeting an Anthos GKE cluster and deploy a container as a service.

Self-paced environment setup

  1. Sign in to Cloud Console and create a new project or reuse an existing one. (If you don't already have a Gmail or G Suite account, you must create one.)

dMbN6g9RawQj_VXCSYpdYncY-DbaRzr2GbnwoV7jFf1u3avxJtmGPmKpMYgiaMH-qu80a_NJ9p2IIXFppYk8x3wyymZXavjglNLJJhuXieCem56H30hwXtd8PvXGpXJO9gEUDu3cZw

ci9Oe6PgnbNuSYlMyvbXF1JdQyiHoEgnhl4PlV_MFagm2ppzhueRkqX4eLjJllZco_2zCp0V0bpTupUSKji9KkQyWqj11pqit1K1faS1V6aFxLGQdkuzGp4rsQTan7F01iePL5DtqQ

8-tA_Lheyo8SscAVKrGii2coplQp2_D1Iosb2ViABY0UUO1A8cimXUu6Wf1R9zJIRExL5OB2j946aIiFtyKTzxDcNnuznmR45vZ2HMoK3o67jxuoUJCAnqvEX6NgPGFjCVNgASc-lg

Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in this codelab as PROJECT_ID.

  1. Next, you'll need to enable billing in Cloud Console in order to use Google Cloud resources.

Running through this codelab shouldn't cost much, if anything at all. Be sure to to follow any instructions in the "Cleaning up" section which advises you how to shut down resources so you don't incur billing beyond this tutorial. New users of Google Cloud are eligible for the $300USD Free Trial program.

Google Cloud Shell

While Google Cloud can be operated remotely from your laptop, in this codelab we will be using Google Cloud Shell, a command line environment running in the Cloud.

This Debian-based virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory and runs in Google Cloud, greatly enhancing network performance and authentication. This means that all you will need for this codelab is a browser (yes, it works on a Chromebook).

  1. To activate Cloud Shell from the Cloud Console, simply click Activate Cloud Shell ** fEbHefbRynwXpq1vj2wJw6Dr17O0np8l-WOekxAZYlZQIORsWQE_xJl-cNhogjATLn-YxLVz8CgLvIW1Ncc0yXKJsfzJGMYgUeLsVB7zSwz7p6ItNgx4tXqQjag7BfWPcZN5kP-X3Q **(it should only take a few moments to provision and connect to the environment).

I5aEsuNurCxHoDFjZRZrKBdarPPKPoKuExYpdagmdaOLKe7eig3DAKJitIKyuOpuwmrMAyZhp5AXpmD_k66cBuc1aUnWlJeSfo_aTKPY9aNMurhfegg1CYaE11jdpSTYNNIYARe01A

Screen Shot 2017-06-14 at 10.13.43 PM.png

Once connected to Cloud Shell, you should see that you are already authenticated and that the project is already set to your PROJECT_ID.

gcloud auth list

Command output

Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)
gcloud config list project

Command output

[core]
project = <PROJECT_ID>

If, for some reason, the project is not set, simply issue the following command:

gcloud config set project <PROJECT_ID>

Looking for your PROJECT_ID? Check out what ID you used in the setup steps or look it up in the Cloud Console dashboard:

R7chO4PKQfLC3bvFBNZJALLTUiCgyLEq_67ECX7ohs_0ZnSjC7GxDNxWrJJUaoM53LnqABYamrBJhCuXF-J9XBzuUgaz7VvaxNrkP2TAn93Drxccyj2-5zz4AxL-G3hzxZ4PsM5HHQ

Cloud Shell also sets some environment variables by default, which may be useful as you run future commands.

echo $GOOGLE_CLOUD_PROJECT

Command output

<PROJECT_ID>
  1. Finally, set the default zone and project configuration.gcloud config set compute/zone us-central1-f

You can choose a variety of different zones. For more information, see Regions & Zones.

Using your browser, navigate to the Google Kubernetes Engine (GKE) section in Google Cloud Console: console.cloud.google.com/kubernetes.

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

In the Cluster Basics page, name your cluster "run-gke", select Zonal Location Type and choose your Zone (this lab is using us-central1-a).

anthos-cluster.png

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

k8s-features.png

Finally click Create to create and provision the Anthos GKE cluster.

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

To deploy a container to the cluster you have just created, go to the Cloud Run section ( console.cloud.google.com/run) and click Create service:

  • Use gcr.io/cloudrun/hello as the sample container image
  • Choose Cloud Run for Anthos and select the cluster you have just created from the Available Anthos GKE clusters dropdown
  • Use the default namespace
  • Give the service a name, for instance hello-run-gke
  • Keep "External" as the Connectivity choice
  • Click Create and wait for the deployment to finish

7a95bdcfb6f97b7c.png

Congratulations, you have just created the service and deployed it to Cloud Run for Anthos:

528a976797fe1d72.png

Note the URL listed at the top. This will be needed to test the deployed service in the next and final step.

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

To avoid having to setup DNS, we'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 sent 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 Cloud Console, go back to the GKE section ( console.cloud.google.com/kubernetes) and click Services & Ingress in the left navigation panel to display the list of services.

Scroll down to the istio-ingressgateway service and copy the IP address shown next to the load balancer. Ignore the other values under that IP address.

56a40f741e97b8d7.png

From Cloud Shell, use curl to access the service :

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

Replace [INGRESS-GATEWAY-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.

The response should be HTTP 200 along with the default "Congratulations | Cloud Run" HTML content :

* Rebuilt URL to: http://173.255.129.211:80/
*   Trying 173.255.129.211...
* TCP_NODELAY set
* Connected to 173.255.129.211 (173.255.129.211) port 80 (#0)
> GET / HTTP/1.1
> Host: hello-run-gke.default.example.com
> User-Agent: curl/7.52.1
> Accept: */*
> 
< HTTP/1.1 200 OK
< content-type: text/html; charset=utf-8

<...>

<title>Congratulations | Cloud Run</title>

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

To delete the cluster, go to the GKE section in the console, select the cluster and click Delete.

20c12aef02258c42.png

If you haven't already done so, a good next step would be to Build your own container and Deploy to Cloud Run (the fully managed version, without Anthos GKE).

For more information on building a container from code source and pushing to Container Registry, see: