Deploy a Quarkus application in Cloud Run
Quarkus is a microservice development framework designed for the cloud and containers.
It is designed to have reduced memory usage and the shortest possible startup time.
It is mainly based on standards (Jakarta EE, Eclipse MicroProfile, etc.) and allows the use of mature and widely used Java libraries via its extensions (Hibernate, RESTeasy, Vert.X, Kafka, etc.).
Quarkus was designed for the cloud from the beginning, enabling the development of Cloud Ready applications (as defined by the 12 Factors principle) and Cloud Native applications (using the capabilities of public clouds to build your applications).
Google Cloud Run is a Container As A Service solution that allows you to run interface and backend services, batch jobs, hosted LLMs, and queue processing workloads without having to manage the infrastructure.
Quarkus is therefore a good solution for developing an application to deploy to Cloud Run.
Create a Quarkus application
First step, create a Quarkus application:
quarkus create app quarkus-cloud-run
The project will be generated in a quarkus-cloud-run directory. Move to the root of this directory and launch the application via quarkus dev.
You can test the application via curl localhost:8080/hello.
A Quarkus application can run in a JVM or as a native application. Here, we will choose a native image deployment, which is more suitable for cloud deployments.
To build the application as a native image, you can use the following command:
cp src/main/docker/Dockerfile.native Dockerfile quarkus build --native
Deploy to Google Cloud Run
By default, gcloud will send all files of your application except those defined in your .gitignore to package your application. You can add directories or files to ignore in a .gcloudignore file, for example, the application directory: src/.
To create a container, we will use the Dockerfile provided by Quakus in src/main/docker/Dockerfile.native. Simply copy it to the root directory so that it is used by default:
cp src/main/docker/Dockerfile.native Dockerfile
Warning: this Dockerfile contains a directive that is not compatible with Cloud Build. You must modify the COPY directive to remove the --chown and --chmod options:
COPY target/*-runner /work/application
Cloud Build can package your application and make it available to Cloud Run in the Google Cloud container registry. You can use the following command, which will use the Dockerfile located at the root of your application by default:
gcloud builds submit --tag gcr.io/PROJECT-ID/quarkus-cloud-run
Once the build is complete, you can launch your application via Cloud Run using the following command, which will use the container previously created by Cloud Build:
gcloud run deploy --image gcr.io/PROJECT-ID/quarkus-cloud-run
Once the application is deployed, gcloud will display the Service URL in its logs, which allows us to access the application.
You can test the application via curl SERVICE_URL/hello.
Add an Health Check
Cloud Run supports the use of Health Checks in the form of startup probes, liveness probes, and readiness probes.
Quarkus supports Health Checks via the SmallRye Health extension, which you can add to your application using:
quarkus extension add smallrye-health
You must then rebuild the application:
quarkus build --native gcloud builds submit --tag gcr.io/PROJECT-ID/quarkus-cloud-run
During deployment, you must specify the application’s health checks to Cloud Run. Here, I have only specified a startup probe:
gcloud run deploy --image gcr.io/PROJECT-ID/quarkus-cloud-run \ --startup-probe httpGet.path=/q/health/started,httpGet.port=8080
Quarkus exposes different endpoints for health checks:
- /q/health/started
- /q/health/live
- /q/health/ready
You can use them for the different health checks supported by Cloud Run. More information on Cloud Run health checks and how to declare them can be found here.
If you call the /q/health/started endpoint used by Cloud Run as a startup probe, you should get a response like this:
{
"status": "UP",
"checks": [
]
}