Google cloud run is a fully managed container execution environment. It is an environment specifically for request-driven workloads. It provides autoscaling, scaling down to zero, pretty fast deployments, automatic HTTPS support, global names and more. Google Cloud Run doesn’t have language runtime restrictions as soon as the language runtime is supported on gVisor. It only requires the deployments to expose an HTTP server on a port.
This guide assumes that you are familiar with cloud console and cloud shell environment.
In this guide, we will deploy a minimal helloworld HTTP server. The container runtime is expecting the server to listen on $PORT:
Related contents:
- How to use Terraform to create a vpc network and a GKE in GCP
- How to create and manage Secrets in GCP Secret Manager using Terraform
- How to Create a Service Account for Terraform in GCP
- How to use Terraform to create a Redis instance in GCP
- How to use Terraform to create a vpc network and a Cloud SQL in GCP
Google cloud Authentication and Enabling required APIs
Since we will deploy our code to google cloud, we will need to authenticate to it. In this guide we will use the command line to achieve most of what we want. Google provides a command line utility (gcloud) that allows you to perform most of the operations with the google cloud.
You will need to have gcloud
installed. If not, consult the gcloud installation page. Confirm that it is working as expected by checking the version installed:
|
|
Since we will be using cli to perform out operations, we need to authenticate to google cloud. Use this gcloud command:
|
|
Then do application login to generate credentials for client libraries:
|
|
Once logged in, we need to enable the required APIs. In our case, we will use cloudbuild so enable it with this command:
|
|
That is it with google authentication, we are now set to create out application and deploy to cloud build.
Creating a Golang Application
Before checking this section, ensure that you have golang installed and working as expected. Checkout the go downloads page if you need any assistance with that.
Once installed, confirm that it is working as expected by checking the version. This is the output on my machine:
|
|
Now we can set up a directory and add out code. Create a new application directory and initialize it as a Go application using this command:
|
|
Inside the directory, we will create a file main.go
and add the following content
|
|
The above code will spin up a web server listening on port 8080
that responds with Hello world on GET
request to /
. To test that it is working as expected, run it with the following command:
|
|
On a separate terminal window, you can make request to our server using curl. You should see something similar to this:
|
|
Dockerize the app
Cloud run expects a container. That means for out application to work it needs to be dockerized. As a prerequisite, you need to have docker installed and working in your machine. If not, checkout How to install and configure docker in Rocky Linux/Alma Linux 9.
Confirm that docker is working as expected by checking the version installed:
|
|
Next we will dockerize the app. Create a file named Dockerfile
in the application directory and add the following content:
|
|
To test that this will build as expected use this command:
|
|
Pushing the image to Google Cloud Registry
Once we have our application container image built, we need to push it to GCR for cloud run to use.
If you want to use the docker commandline, authenticate to docker registry:
|
|
Build and push the image to Google Container Registry using the following commands:
Build the docker image
|
|
Push the docker image to container registry
|
|
The other alternative is using gcloud builds submit specifying your image as shown in this command:
|
|
This command builds a container with your code and puts it in the Container Registry of your project. You can see the container if you click: Navigation menu > Container Registry. If you don’t see goapp
, click Refresh.
Deploying our application to cloud build
We can use the earlier image to deploy our application on cloud build:
|
|
You can also build and deploy from current directory
|
|
Advanced deploy with environment variables
|
|
On success you will see output similar to this:
|
|
To confirm that the application is working as expected, initiate a request to the given URL:
|
|
The above shows that it is working as expected.
Checking logs
|
|
Inspecting Cloud run services
Now that the service is up and running, we can use these gcloud commands to manage them.
List cloud run services:
|
|
Describe the service to get its details:
|
|
If you no longer need the service you can delete using this command:
|
|
Conclusion
Google Cloud Run is specifically optimized for request-driven HTTP/HTTPS workloads. The containers can be preempted and migrated, so it is not great if you are planning to use it for long running services. There is no support other than HTTP/HTTPS for now either.