mn create-app example.micronaut.micronautguide \
--features=crac \
--build=gradle \
--lang=groovy \
--test=spock
Table of Contents
Deploy CRaC Docker Image to Google Cloud Run
Deploy a Docker Image containing a CRaC enabled JDK and a pre-warmed, checkpointed application to Google Cloud Run - a fully managed serverless platform for containerized applications.
Authors: Sergio del Amo
Micronaut Version: 4.6.3
1. Getting Started
In this guide, we will create a Micronaut application written in Groovy.
You are going to deploy a Docker Image containing a CRaC enabled JDK and a pre-warmed, checkpointed Micronaut application to Google Cloud Run.
2. Costs
This guide uses paid services; you may need to enable Billing in Google Cloud to complete some steps in this guide. |
3. What you will need
To complete this guide, you will need the following:
-
Some time on your hands
-
A decent text editor or IDE (e.g. IntelliJ IDEA)
-
JDK 21 or greater installed with
JAVA_HOME
configured appropriately -
You need a Google Cloud Platform (GCP) account and a GCP project.
-
A computer with x86_64 architecture.
The CRaC JDK currently requires that you are using a machine with x86_64 architecture. If you are using a different architecture, you cannot currently build a CRaC checkpointed application. |
4. Solution
We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.
-
Download and unzip the source
5. Writing the Application
Create an application using the Micronaut Command Line Interface or with Micronaut Launch.
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool. If you don’t specify the --lang argument, Java is used as the language.If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.
|
The previous command creates a Micronaut application with the default package example.micronaut
in a directory named micronautguide
.
If you use Micronaut Launch, select Micronaut Application as application type and add crac
features.
If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features, and apply those changes to your application. |
6. Controller
Add a controller which responds with the JSON payload in the root route.
{"message":"Hello World"}
package example.micronaut
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
@Controller (1)
class HelloController {
@Get (2)
Map<String, Object> index() {
[message: "Hello World"] (3)
}
}
1 | The class is defined as a controller with the @Controller annotation mapped to the path / . |
2 | The @Get annotation maps the method to an HTTP GET request. |
3 | The Micronaut framework will automatically convert it to JSON before sending it. |
package example.micronaut
import io.micronaut.context.ApplicationContext
import io.micronaut.crac.test.CheckpointSimulator
import io.micronaut.http.client.BlockingHttpClient
import io.micronaut.http.client.HttpClient
import io.micronaut.runtime.server.EmbeddedServer
import spock.lang.Specification
class HelloControllerSpec extends Specification {
void "test hello endpoint works after checkpoint"() throws IOException {
given:
EmbeddedServer server = ApplicationContext.run(EmbeddedServer)
BlockingHttpClient client = createHttpClient(server)
when:
String body = client.retrieve("/")
then:
body == '{"message":"Hello World"}'
when:
CheckpointSimulator checkpointSimulator = server.applicationContext.getBean(CheckpointSimulator)
checkpointSimulator.runBeforeCheckpoint()
then:
!server.running
when:
client.close()
checkpointSimulator.runAfterRestore()
then:
server.running
when:
client = createHttpClient(server)
body = client.retrieve("/")
then:
body == '{"message":"Hello World"}'
cleanup:
client.close()
}
private static BlockingHttpClient createHttpClient(EmbeddedServer embeddedServer) {
String url = "http://localhost:${embeddedServer.port}"
return embeddedServer.applicationContext.createBean(HttpClient, url).toBlocking();
}
}
7. Google Cloud Platform
Signup for the Google Cloud Platform
7.1. Cloud SDK
Install the Cloud SDK CLI for your operating system.
Cloud SDK includes the gcloud
command-line tool. Run the init
command in your terminal:
gcloud init
Log in to your Google Cloud Platform:
gcloud auth login
7.2. Google Cloud Platform Project
Create a new project with a unique name (replace xxxxxx
with alphanumeric characters of your choice):
gcloud projects create micronaut-guides-xxxxxx
In GCP, project ids are globally unique, so the id you used above is the one you should use in the rest of this guide. |
Change your project:
gcloud config set project micronaut-guides-xxxxxx
If you forget the project id, you can list all projects:
gcloud projects list
7.3. Enable billing
Ensure you have a billing account created, and if not create one via the Google Cloud Platform Console.
To get a list of your billing accounts, run:
❯ gcloud beta billing accounts list
ACCOUNT_ID NAME OPEN MASTER_ACCOUNT_ID
XXXXXX-XXXXXX-XXXXXX My Billing Account True
You can then attach a billing account to your project:
> gcloud beta billing projects link micronaut-guides-xxxxxxx --billing-account=XXXXXX-XXXXXX-XXXXXX
billingAccountName: billingAccounts/XXXXXX-XXXXXX-XXXXXX
billingEnabled: true
name: projects/micronaut-guides-xxxxxxx/billingInfo
projectId: micronaut-guides-xxxxxxx
7.4. Enable the Google Cloud Container Registry API
We need somewhere to store our docker images, so we need to enable the Google Cloud Container Registry API for your project via the Google Cloud CLI:
gcloud services enable containerregistry.googleapis.com
7.5. Configure Google Cloud Docker
Run auth configure-docker
via the Google Cloud CLI:
> gcloud auth configure-docker
Adding credentials for all GCR repositories.
WARNING: A long list of credential helpers may cause delays running 'docker build'. We recommend passing the registry name to configure only the registry you are using.
After update, the following will be written to your Docker config file located at [~/.docker/config.json]:
{
"credHelpers": {
"gcr.io": "gcloud",
"us.gcr.io": "gcloud",
"eu.gcr.io": "gcloud",
"asia.gcr.io": "gcloud",
"staging-k8s.gcr.io": "gcloud",
"marketplace.gcr.io": "gcloud"
}
}
8. CRaC Docker Image
Generate a Docker Image containing a CRaC enabled JDK and a pre-warmed, checkpointed application:
./gradlew dockerBuildCrac
...
Successfully built ba78619b8d86
Successfully tagged micronautguide:latest
Created image with ID 'ba78619b8d86'.
BUILD SUCCESSFUL in 1m 41s
13 actionable tasks: 9 executed, 4 up-to-date
9. Docker Push
Push the Docker image of your application to Google Cloud Container Registry or to Google Cloud Artifact Registry.
Modify your Gradle build and configure the image name for the dockerBuildCrac
task:
tasks.named("dockerBuildCrac") {
images = ["gcr.io/micronaut-guides-xxxxxxx/micronautguide:latest"]
}
./gradlew dockerPushCrac
In the image name above, micronaut-guides-xxxxxxx should be replaced by your project name you created earlier in the guide.
|
You get an output such as:
> Task :dockerPushCrac
Pushing image 'gcr.io/micronaut-guides-xxxxxxx/micronautguide:latest'.
10. Google Cloud Run Deploy
You can deploy to Google Cloud Run via the CLI. Use the value you configured in your build as the image argument’s value.
gcloud run deploy \
--image=gcr.io/micronaut-guides-xxxxxx/micronautguide:latest \
--platform managed \
--execution-environment=gen2 \
--allow-unauthenticated
You will see an output such as:
Service name (micronautguide):
Please specify a region:
...
[22] us-central1
[23] us-east1
[24] us-east4
...
[29] cancel
Please enter your numeric choice: 23
To make this the default region, run `gcloud config set run/region us-east1`.
Deploying container to Cloud Run service [micronautguide] in project [micronaut-guides-xxxxxx] region [us-east1]
✓ Deploying... Done.
✓ Creating Revision...
✓ Routing traffic...
✓ Setting IAM Policy...
Done.
Service [micronautguide] revision [micronautguide-00002-fat] has been deployed and is serving 100 percent of traffic.
Service URL: https://micronautguide-63kwrzytgq-ue.a.run.app
11. Running the Application
curl -i https://micronautguide-63kwrzytgq-ue.a.run.app
HTTP/2 200
content-type: application/json
x-cloud-trace-context: c3845249bc01f6c6a5bb7c01c4500bb8;o=1
date: Thu, 13 Apr 2023 11:09:58 GMT
server: Google Frontend
content-length: 25
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
{"message":"Hello World"}
12. Cleanup
12.1. Deleting the Cloud Run Service
Use the command gcloud run service delete to delete the service.
gcloud run services delete micronautguide
12.2. Deleting the project
The easiest way to eliminate billing is to delete the project you created for the tutorial.
Deleting a project has the following consequences:
|
12.2.1. Via the CLI
To delete the project using the Cloud SDK, run the following command, replacing YOUR_PROJECT_ID
with the project ID:
gcloud projects delete YOUR_PROJECT_ID
12.2.2. Via the Cloud Platform Console
In the Cloud Platform Console, go to the Projects page.
In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click Delete project
In the dialog, type the project ID, and then click Shut down to delete the project.
Deleting or turning off specific resources
You can individually delete or turn off some of the resources that you created during the tutorial.
13. Next Steps
14. Help with the Micronaut Framework
The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.
15. License
All guides are released with an Apache license 2.0 license for the code and a Creative Commons Attribution 4.0 license for the writing and media (images…). |