This demo will walk you through the processes for deploying Native Image applications onto the Google Cloud Run platform. In this demo, you will deploy a simple "Hello World" HTTP application and have the ability to see details about its performance.
Ensure that you have the following installed and follow the linked instructions for any that you are missing:
- A Docker-API compatible container runtime such as Rancher Desktop or Docker
- Ensure that the daemon is actively running before beginning the demo
- Google Cloud CLI: https://cloud.google.com/sdk/docs/install#linux
- GraalVM: https://www.graalvm.org/downloads/
COMPATIBILITY: Please note that this demo must be performed on an x86-based platform in order to properly function. Working through this demo on an ARM-based platform will result in the generation of a native executable that is not compatible with the platform.
Download or clone the GraalVM demos repository:
git clone https://github.com/graalvm/graalvm-demos
The code provided in this demo is a simple "Hello World" REST application created using the Micronaut ® framework. To understand what the code is doing, take a look at the Application.java and HelloController.java files:
Application.java
package example.micronaut;
import io.micronaut.runtime.Micronaut;
public class Application {
public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
}
This is the location of the main() function and entry point for the application.
HelloController.java
package example.micronaut;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;
@Controller("/hello")
public class HelloController {
@Get
@Produces(MediaType.TEXT_PLAIN)
public String index() {
return "Hello World";
}
}
This code implements the actual RESTful "Hello World" functionality. It produces the "Hello World" string when a GET request is made to the /hello URL.
- Navigate to the directory for this demo:
cd graalvm-demos/native-google-cloud-run
- Login to your Google account using the Google Cloud CLI:
gcloud auth login
- Run the following command to configure Docker credentials for the CLI:
gcloud auth configure-docker
- Create a new project using the following command, where "xxxxxx" denotes the 6-digit identifier you chose for your project:
gcloud projects create graal-demo-xxxxxx
- Set your newly created project to be currently selected:
gcloud config set project graal-demo-xxxxxx
To see a list of all projects use:
gcloud projects list
- Use a browser to login to the Google Cloud dashboard and navigate to the Billing tab
- Ensure that you have a billing account set up and enable billing for the newly created project by clicking the ellipsis beside the project name and selecting Change billing
- Return to your terminal and activate the required project APIs using the following command:
gcloud services enable run.googleapis.com container.googleapis.com
OPTIONAL: In the next step you will use a single command to build the application into a container image and deploy it to the repository you have created; if you would like to first view the Docker file that will be used to create the image, run the following command:
./mvnw mn:dockerfile -Dpackaging=docker-native
The newly created Dockerfile will be automatically stored in the target directory
- Push the application image to Google Cloud Container Registry (once again replacing "xxxxxx" as appropriate):
./mvnw deploy -Dpackaging=docker-native -Djib.to.image=gcr.io/graal-demo-xxxxxx/graaldemo:latest
- Deploy the application to Google Cloud Run:
gcloud run deploy --image=gcr.io/graal-demo-xxxxxx/graaldemo:latest --platform managed --allow-unauthenticated
- Once the application is successfully deployed, a Service URL will be outputted. Use that URL in the following command to test the application - a success will return the string "Hello World":
curl SERVICE_URL/hello
- To view detailed information about the application performance such as build and response times, visit Google Cloud Logging
Once you are finished with the demo, follow these steps to delete the created resources:
- Use a browser to login to the Google Cloud dashboard and navigate to the Resource Manager
- Check the box next to the name of the project you wish to delete
- Click DELETE on the top bar and follow the on-screen instructions