Micronaut & GraalVM with Google Cloud Run

3 minuti di lettura

Micronaut & GraalVM

In the previous article Micronaut & GraalVM: together for Cloud Native we have seen how is easy to create a web service with Micronaut and compile it to a native image with Docker.

In this article I want to show you the next step, that is the deploy to the Cloud by using Google Cloud Run, the serverless platform offered by Google.

1 - The Docker container

Let's start with the container. If you followed all the steps of the previous article you already have a docker image named mngraal in your local registry.

# To check 
docker image ls mngraal

2 - Google Cloud Project

Before deploying the service you need to start a GCP Project. The free tier offered by Google is enough to deploy the service for free. It may be required to provide a credit card before enabling the GCP Cloud Run service.

3 - Deploy e Test

The service deploy is rally easy. But you need to have the *Cloud SDK installed in your system. So from here to the next step you are supposed to have a fully environment ready for the gcloud command.

So let's start by adding the Google credentials to the Docker local engine. They are required to push the image inside your project Google Registry.

gcloud auth configure-docker

Let's tag the image now, and push it to the Google registry. I use the latest tag but please use a proper version when you ship things to production.

docker image tag mngraal:latest eu.gcr.io/your_project_id/mngraal:latest
docker image push eu.gcr.io/your_project_id/mngraal:latest

Then to create the Cloud Run service you can use the Google graphical console (web app), or use the command line. I prefer the latter, so for example to deploy a public service with the managed Cloud Run setup you can type:

gcloud run deploy mngraal --project your_project_id --allow-unauthenticated \
--image eu.gcr.io/your_project_id/mngraal:latest \
--platform managed --region europe-west1

Deploying container to Cloud Run service [mngraal] in project [***] region [europe-west1]
✓ Deploying... Done.
  ✓ Creating Revision...
  ✓ Routing traffic...
  ✓ Setting IAM Policy...
Service [mngraal] revision [mngraal-00004-mux] has been deployed and is serving 100 percent of traffic.
Service URL: https://mngraal-****-ew.a.run.app

Ok, let's test the service now. I usually use curl, printing also the time_starttransfer in order to check how long it takes to start the container during the ColdStart:

curl -s  -w  '\n\n%{time_starttransfer}-%{time_total}\n' https://mngraal-****-ew.a.run.app/

Hello World!


When the service is already up the response is 10 times faster. But anyway even if the Cold Start occurs, for a lot of REST APIS wait one or two seconds is pretty good.

curl -s  -w  '\n\n%{time_starttransfer}-%{time_total}\n' https://mngraal-****-ew.a.run.app/

Hello World!



With this setup, you can actually deploy, test and debug by using the JVM in our local environment. Then you can choose the deployment platform according to your needs. For example you could use Kubernetes (with the JVM or native container) if you want an always up service. Or you can choose a Serverless solution, like Cloud Run if you want a fully managed platform or if the service requests are not so frequent. It is up to you, but practically you can do anything.