With microservices-based architectures in the cloud, organizations can leverage the cloud’s full potential with cloud-native tools like managed Kubernetes, container registries, and serverless deployments. Google Cloud Platform (GCP) provides multiple hosting options for those who want to modernize their applications through containers—be it a managed Kubernetes service through Google Kubernetes Engine (GKE) or a serverless deployment through GCP’s serverless container deployment service, Cloud Run.
Part 1 of this series explored Cloud Run, as well as the underlying Knative technology that enables it. To recap, Knative is the open-source platform that abstracts away the complexities of infrastructure and helps deploy serverless applications on Kubernetes (K8s). Cloud Run is the implementation of Knative in GCP. It delivers a subset of Knative features while integrating seamlessly with the GCP container ecosystem.
This post will look at a hands-on deployment showing how to get started with Cloud Run. In part 3 we show how to deploy your serverless workloads via Anthos running on your managed GKE cluster.
Let’s take a look at how you can deploy serverless containers using a fully managed Cloud Run service. Though Cloud Run uses Knative in the backend, it enables developers to completely focus on the application. The infrastructure layer is abstracted away and fully managed by the platform.
Here’s how to build and deploy a sample Go-language application to Cloud Run.
To deploy the sample Go language, you’ll need to build the Go sample application from source code, package it to a container image, and upload it to Artifact Registry before deploying the image to Cloud Run. Ensure that you have owner or editor roles in the GCP project or the necessary permissions assigned for the associated services: Cloud Run Deployment, Cloud Build, Artifact Registry.
Open the Cloud Console API library. Then search for Cloud Build API and enable the service.

Note: These steps should be executed from Google Cloud Shell, as it has Go-language development features preinstalled.
mkdir sampleapp
cd sampleapp
Create a go.mod file and add the below content to declare the Go module:
module github.com/GoogleCloudPlatform/golang-samples/run/helloworld
go 1.19
main.go file and copy the following content (refer to the Google Cloud GitHub sample repo for the latest code):// Sample run-helloworld is a minimal Cloud Run service.
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
log.Print("starting server...")
http.HandleFunc("/", handler)
// Determine port for HTTP service.
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("defaulting to port %s", port)
}
// Start HTTP server.
log.Printf("listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
name := os.Getenv("NAME")
if name == "" {
name = "World"
}
fmt.Fprintf(w, "Hello %s!\n", name)
}
gcloud run deploy --source .
When prompted, press enter to accept the default service name for the deployment.
user@cloudshell:~/sampleapp (ambient-nuance-382122)$ gcloud run deploy --source .
Service name (sampleapp):
If you receive a prompt to authorize the API call from Cloud Shell, click on Authorize.
You will be prompted to select the region of the deployment. Provide the number associated with the relevant region as input. In the sample below, the number 16 (corresponding to the region europe-west3) is selected. If prompted to enable Artifact Registry API, respond by typing y.
Please specify a region:
[1] asia-east1
[2] asia-east2
[3] asia-northeast1
[4] asia-northeast2
[5] asia-northeast3
[6] asia-south1
[7] asia-south2
[8] asia-southeast1
[9] asia-southeast2
[10] australia-southeast1
[11] australia-southeast2
[12] europe-central2
[13] europe-north1
[14] europe-southwest1
[15] europe-west1
[16] europe-west12
[17] europe-west2
[18] europe-west3
[19] europe-west4
[20] europe-west6
[21] europe-west8
[22] europe-west9
[23] me-central1
[24] me-west1
[25] northamerica-northeast1
[26] northamerica-northeast2
[27] southamerica-east1
[28] southamerica-west1
[29] us-central1
[30] us-east1
[31] us-east4
[32] us-east5
[33] us-south1
[34] us-west1
[35] us-west2
[36] us-west3
[37] us-west4
[38] cancel
Please enter numeric choice or text value (must exactly match list item): 15
To make this the default region, run `gcloud config set run/region europe-west1`.
Deploying from source requires an Artifact Registry Docker repository to store built containers. A repository named [cloud-run-source-deploy] in region [europe-west1] will be created.
Do you want to continue (Y/n)? Y
allow unauthenticated access to the app. Respond by typing y, as it’s a sample application.This command is equivalent to running `gcloud builds submit --pack image=[IMAGE] .` and `gcloud run deploy sampleapp --image [IMAGE]`
Allow unauthenticated invocations to [sampleapp] (y/N)? y
Building using Buildpacks and deploying container to Cloud Run service [sampleapp] in project [ambient-nuance-382122] region [europe-west1]
OK Building and deploying new service... Done.
OK Creating Container Repository...
OK Uploading sources...
OK Building Container... Logs are available at [https://console.cloud.google.com/cloud-build/builds/2a6a2f8f-93cc-42e6-97c6-c0a8de8e1e31?project=581120152173].
OK Creating Revision... Creating Service.
OK Routing traffic...
OK Setting IAM Policy...
Done.
Service [sampleapp] revision [sampleapp-00001-zom] has been deployed and is serving 100 percent of traffic.
Service URL: https://sampleapp-4cnh6hogwa-ew.a.run.app

Cloud Run is an excellent choice for developers looking to build and deploy serverless containers in the Google Cloud Platform. With its simple commands and fully managed service, Cloud Run makes it easy to focus on developing applications rather than managing infrastructure. The platform's seamless integration with Knative also ensures a consistent experience across different systems and eliminates vendor lock-in. Additionally, the pay-per-use model means that you only pay for the resources you use, making it a cost-effective option for applications of any size.