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 Cloud Native Tools. 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 multi-cluster support.

Prerequisites #

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 Cloud Native (GCN) Tools and its dependencies in VS Code.

2. Create a Java Application Using GCN Tools #

Create a Java application to containerize and deploy to Google Kubernetes Engine (GKE) using GCN Tools.

  1. Go to View, Command Palette, search for “gcn”, and invoke the OCI for GCN: Create GCN Project quick action:

    gcn-vscode-actions.png

  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)
    • Pick test framework: JUnit
    • Pick cloud platform: GCP. Click OK to confirm
  3. 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.

  4. (Required only for macOS AArch64) At the beginning of the configuration file, gcp/build.gradle, add the following dependency to use the appropriate version of JNA:

     buildscript {
         dependencies {
             classpath("com.github.docker-java:docker-java-transport-httpclient5:3.2.13") {
                 because("macOS AArch64 needs a specific version of JNA")
             }
         }
     }
    
  5. (Required only for macOS AArch64) At the end of the configuration file, gcp/build.gradle, add the following code so that Gradle builds a container image using the openjdk:17-slim base image (it defaults to openjdk:17-alpine which is not multiplatform).

     tasks.withType(com.bmuschko.gradle.docker.tasks.image.Dockerfile) {
         baseImage = 'openjdk:17-slim'
     }
    
  6. Open a new Terminal window in VS Code (Terminal > New Terminal). Use the following command to build and run the application 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 gcn-to-gke-gcp container image is the one you will deploy to GKE.

     docker images
    

    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:

    Container Repository Created

  5. Tag your container image:

     docker tag <dockerImageName/ID> <regionName>-docker.pkg.dev/<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>
     --member=serviceAccount:<PROJECT_NUMBER><-compute@developer.gserviceaccount.com>  
     --role="roles/artifactregistry.reader"
    
  7. Run your container image locally, and access it from the browser at localhost:8080:

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

    Access a Running App from Browser

4. Push a Container Image #

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

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

     docker push <regionName>-docker.pkg.dev/<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 GKE Cluster #

  1. Create a Kubernetes cluster that will manage your nodes/pods using the Google Cloud CLI:

     gcloud config set compute/region <regionName>
    
     gcloud container clusters create-auto <clusterName> --region=<regionName>
    
  2. If you are 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.

  3. Check if you are connected to a GKE cluster:

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

     kubectl create deployment <deploymentName> --image=<regionName>-docker.pkg.dev/<PROJECT_ID>/<repoName>/<appName>:<tag>
    

    Create a Kubernetes Deployment

  5. Set baseline number of deployment replicas to 3:

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

    kubectl autoscale deployment <deploymentName> --cpu-percent=80 --min=1 --max=5
    
  7. Run the next command to check the status of the pods:

     kubectl get pods
    

    Pods Status

  8. 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
    

    Expose Port

  9. List the services:

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

    App Running from GKE

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 Cloud Native tooling.