Deploying a Java Application to Google Kubernetes Engine in VS Code

This guide shows you how to create a Java application, containerize, and deploy it to Google Kubernetes Engine (GKE) from within VS Code using the Graal Development Kit for Micronaut Extension Pack. The Micronaut® Kubernetes module provides seamless integration with GKE.

Google Kubernetes Engine (GKE) is managed Kubernetes service with full Kubernetes API, four-way autoscaling, release channels, and multicluster support.

Prerequisites #

  • A Java Development Kit (JDK) installation (JDK 17 or later).
  • A Google Cloud Platform (GCP) account. Create an account at
  • The Google Cloud CLI to interact with Google Cloud services from the terminal. (The CLI is part of the Google Cloud SDK; for more information, see Cloud SDK.)
  • A Docker-API compatible container runtime such as Rancher Desktop or Docker.

Note: This guide uses paid services; you may need to enable billing in Google Cloud to complete some steps in this guide.

1. Install Extensions #

Follow this guide to install the Graal Development Kit for Micronaut Extension Pack and its dependencies in VS Code.

2. Create a Java Application Using Graal Development Kit for Micronaut Launcher #

Create a Java application, using the Graal Development Kit for Micronaut Launcher, that you will later containerize and deploy to Google Cloud Run. You can use the GDK Launcher, or the built-in project creation wizard in VS Code.

  1. Go to View, Command Palette, search for “Graal Dev Kit”, and invoke the Graal Dev Kit: Create New Project quick action:

    Create New Project Action

  2. Follow the command prompts to generate a new Maven or Gradle project. For example, to create a simple Java application:
    • Pick Micronaut version: Default
    • Pick application type: Application
    • Pick project Java: GraalVM or Other Java
    • Provide a project name: gcp-k8s-demo
    • Provide base package: com.example
    • Pick project services: none. Click OK to confirm
    • Pick build tool: Gradle (Groovy) or Maven
    • Pick test framework: JUnit
    • Pick cloud platform: GCP. Click OK to confirm _ Select the destination directory on your local disk and whether to open the project in a new window or add it to the current workspace. The wizard creates a directory named gcp-k8s-demo containing an application in a package named com.example.
  3. (Required only for macOS AArch64.) Add the following GCP module configuration:

    tasks.named("dockerfile") {
             instruction """FROM --platform=linux/amd64 eclipse-temurin:17-jre-focal
                 WORKDIR /home/app
                 COPY layers/libs /home/app/libs
                 COPY layers/classes /home/app/classes
                 COPY layers/resources /home/app/resources
                 COPY layers/application.jar /home/app/application.jar

    This configuration that Gradle/Maven tools build a container image using the openjdk:17-slim base image (it defaults to openjdk:17-alpine which is not multiplatform).

  4. Open a new Terminal window in VS Code (Terminal > New Terminal). Build the project and run the application from a JAR file:

     ./gradlew :gcp:run

3. Build a Container Image and Create a Container Repository #

  1. Build a container image for your Java application:

     ./gradlew dockerBuild
  2. Use the docker images command to ensure that the container image is created. The gdk-to-gke-gcp container image is the one you will deploy to GKE.

     docker images
  3. Create a container repository in the Google Cloud Artifact Registry using the Google Cloud CLI:

     gcloud artifacts repositories create <repoName> --repository-format=docker --location=<regionName> --description=<shortDescription>
  4. Login to the Google Cloud console and check the creation of the repository.

  5. Tag your container image:

     docker tag <dockerImageName/ID> <regionName><PROJECT_ID>/<repoName>/<appName>:<tagName>
  6. Add the IAM policy binding using the Google Cloud CLI:

     gcloud artifacts repositories add-iam-policy-binding
     <repoName> --location=<regionName>
  7. Run your container image locally, and access it from the browser at localhost:8080:

     docker run --rm -p 8080:8080 <regionName><PROJECT_ID>/<repositoryName/ID>/<appName>:<tagName>

4. Push a Container Image #

  1. Authenticate to the Google Cloud Artifact Registry using the Google Cloud CLI:

     gcloud auth configure-docker <regionName>
  2. Push your container image to the container repository you created earlier in the Google Cloud Artifact Registry:

     docker push <regionName><PROJECT_ID>/<repositoryName/ID>/<appName>:<tagName>

Your container image is now stored in Google Cloud Artifact Registry, and you can deploy it to the Google Kubernetes Engine (GKE) cluster.

5. Deploy a Java Application to a GKE Cluster #

  1. Set your compute region in Google Cloud CLI with:
     gcloud config set compute/region <REGION>`
  2. Create an auto-scaling Kubernetes cluster that will manage your nodes/pods using the Google Cloud CLI:

     gcloud container clusters create-auto <clusterName> --region=<regionName>
  3. Follow any additional instructions if prompted. For example, you might be asked to install the gcp-auth plugin, follow these instructions gke-gcloud-auth-plugin. This plugin automatically and dynamically configures pods to use your credentials, allowing the applications to access Google Cloud services as if they were running within Google Cloud.

  4. Establish a connection to your GKE cluster:

     gcloud container clusters get-credentials <ClusterName> --region <regionName>
  5. Create a Kubernetes deployment:

     kubectl create deployment <deploymentName> --image=<regionName><PROJECT_ID>/<repoName>/<appName>:<tag>
  6. Set a baseline number of deployment replicas to 3 for autoscaling:

     kubectl scale deployment <deploymentName> --replicas=3
  7. Then create a Horizontal Pod Autoscaler resource for your deployment:

     kubectl autoscale deployment <deploymentName> --cpu-percent=80 --min=1 --max=5
  8. Run the next command to list the created pods:

     kubectl get pods
  9. Expose the port so your application will be accessible from your cluster:

     kubectl expose deployment <deploymentName> --name=<serviceName> --type=LoadBalancer --port 80 --target-port 8080
  10. List the services:

    kubectl get services
  11. Copy the external IP and open it in a browser.

Summary #

This guide demonstrated how to easily containerize and deploy a Java microservice application to Google Kubernetes Engine from within VS Code using the Graal Development Kit for Micronaut Extension Pack.