# Java Microservice on Google Kubernetes Engine (GKE) Cluster

## Introduction

Java microservices remain as hot topic in 2022.

In this article, I would like to show you the steps to create a Java Microservice, and deploy it to Google Kubernetes Engine (GKE).

## Prerequisites

### Install Google Cloud SDK

Follow the instructions in [Install Cloud SDK](https://cloud.google.com/sdk/docs/install).

### Install Cloud Code Plugin for IntelliJ

Install Cloud Code Plugin for IntelliJ from Preference -&gt; Plugins. Search by "Cloud Code" in the "Marketplace" tab.

![Install Cloud Code](https://cdn.hashnode.com/res/hashnode/image/upload/v1642461519135/U9CEG6H-s.png align="left")

## Configure Google Cloud

### Create a Google Cloud Project

1. In Google Cloud Shell, perform the following steps:
    
    i. Lists credentialed accounts and make sure the desired account is set active.
    
    ```shell
    gcloud auth list
    ```
    
    ii. Create a project.
    
    ```shell
    gcloud projects create PROJECT_ID
    ```
    
    * *Replace PROJECT\_ID with your desired project ID.*
        
    
    iii. Set the default project.
    
    ```shell
    gcloud config set project PROJECT_ID
    ```
    
    * *Replace PROJECT\_ID with your project ID.*
        
    
    iv. Print the project ID.
    
    ```shell
    echo $GOOGLE_CLOUD_PROJECT
    ```
    
    v. Set the default [zone](https://cloud.google.com/compute/docs/regions-zones):
    
    ```shell
    gcloud config set compute/zone COMPUTE_ZONE
    ```
    
    * *Replace COMPUTE\_ZONE with your compute zone, such as europe-west2-c.*
        
    
    vi. Set the default [region](https://cloud.google.com/compute/docs/regions-zones):
    
    ```shell
    gcloud config set compute/region COMPUTE_REGION
    ```
    
    * *Replace COMPUTE\_REGION with your compute region, such as europe-west2.*
        
    
    vii. Show the configuration.
    
    ```shell
    gcloud config list
    ```
    
2. Enable Billing in Google Cloud Console.
    
3. Enable APIs in Google Cloud Shell.
    
    i. Enable Artifact Registry API.
    
    ```shell
    gcloud services enable artifactregistry.googleapis.com
    ```
    
    ii. Enable Kubernetes Engine API.
    
    ```shell
    gcloud services enable container.googleapis.com
    ```
    
    iii. Enable Firestore.
    
    ```shell
    gcloud services enable firestore.googleapis.com
    ```
    

### Create an Artifact Registry

Google Cloud Artifact Registry is a successor of Container Registry, in where container images and language packages can be placed.

We will use Artifact Registry to store our container images.

1. Enable Artifact Registry, if it is not yet enabled.
    
    ```shell
    gcloud services enable artifactregistry.googleapis.com
    ```
    
2. View a list of supported locations.
    
    ```shell
    gcloud artifacts locations list
    ```
    
3. Create an artifact repository.
    
    ```shell
    gcloud artifacts repositories create REPO_NAME \
    --repository-format=docker \
    --location=REPO_REGION \
    --description=REPO_DESC \
    --version-policy=REPO_POLICY
    ```
    
    * *Replace REPO\_NAME with the desired name for your artifact repository.*
        
    * *Replace REPO\_REGION with your region, such as europe-west2.*
        
    * *Replace REPO\_DESC with the description of your artifact repository.*
        
    * *Replace REPO\_POLICY with keyword* ***snapshot*** *or* ***release****.*
        
    
    Here below is a working example:
    
    ```shell
    gcloud artifacts repositories create ms-docker-image \
    --repository-format=docker \
    --location=europe-west2 \
    --description="Snapshot repository for docker images" \
    --version-policy=snapshot
    ```
    
    The full name of the created repository will be
    
    ```shell
    $(REPO_REGION)-$(REPO_FORMAT).pkg.dev/$(PROJECT_ID)/$(REPO_NAME)
    ```
    
4. List all artifact repositories created in the project.
    
    ```shell
    gcloud artifacts repositories list
    ```
    

### Create a Google Kubernetes Engine (GKE) Cluster

1. Enable Kubernetes Engine API, if it is not yet enabled.
    
    ```shell
    gcloud services enable container.googleapis.com
    ```
    
2. Create a GKE Cluster.
    
    ```shell
    gcloud container clusters create CLUSTER_NAME \
    --num-nodes=1 \
    --region europe-west2 \
    --release-channel regular \
    --scopes=datastore,storage-rw,compute-ro
    ```
    
    * *Replace CLUSTER\_NAME with the desired name for your GKE cluster.*
        
    
    It will take several minutes to create the cluster.
    
3. After the cluster is created, get authentication credentials to interact with the cluster.
    
    ```shell
    gcloud container clusters get-credentials CLUSTER_NAME
    ```
    
    * *Replace CLUSTER\_NAME with your GKE cluster name.*
        
4. List all clusters.
    
    ```shell
    gcloud container clusters list
    ```
    
5. We can also check whether the cluster is created or not in Google Cloud Console -&gt; Kubernetes Clusters.
    
    ![GKE Cluster](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462542796/KZ-zkusjS.png align="left")
    

## Code the Java Microservice

Spring Boot enables quick development of Java microservices with various dependency and plugin support to Google Cloud technologies.

[Complete Source Code](https://github.com/adafycheng/ms-client-account-gcp) can be found in my GitHub repositories.

### Write the application codes

1. Generate a Spring Boot application.
    
    To create a Spring Boot application, use the [Spring Initializr](https://start.spring.io) to generate a simple Spring Boot . Add the following dependencies:
    
    * Spring Web
        
    * Spring Boot Actuator
        
    * Lombok
        
    
    Spring Boot Actuator provides end points for health check of the application.
    
    Lombok is a Java annotation library that reduces boilerplate code by using annotations.
    
    Click the "Generate" button to download the ZIP file.
    
    Unzip the file to the project root directory.
    
    ![Spring Initializr](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462117062/8DCZfyf9i.png align="left")
    
2. Modify pom.xml to add dependencies for GCP.
    
    1. Versions
        
    
    ```xml
    <properties>
        <spring-cloud-gcp.version>1.2.8.RELEASE</spring-cloud-gcp.version>
        <spring-cloud.version>2021.0.0</spring-cloud.version>
    </properties>
    ```
    
    1. Dependencies
        
    
    ```xml
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-data-datastore</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-data-datastore</artifactId>
        </dependency>
    </dependencies>
    ```
    
    1. Dependency Management
        
    
    ```xml
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-gcp-dependencies</artifactId>
                <version>${spring-cloud-gcp.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    ```
    
3. Create a Java model class.
    
    * The Lombok @Data annotation saves the time of writing the getter and setter functions.
        
    * The Lombok @AllArgsConstructor annotation saves the time of writing the constructor.
        
    * The @Entity annotation specifies that it is a Firestore entity in datastore mode.
        
    
    ```java
    import org.springframework.cloud.gcp.data.datastore.core.mapping.Entity;
    import org.springframework.data.annotation.Id;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    
    @Entity
    @Data
    @AllArgsConstructor
    public class CustomerAccount {
    
        @Id
        private Long accountId;
    
        private String lastName;
    
        private String firstName;
    
    }
    ```
    
4. Create a Spring Repository interface for Google Datastore.
    
    ```java
    import org.springframework.cloud.gcp.data.datastore.repository.DatastoreRepository;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public interface CustomerAccountRepository extends DatastoreRepository<CustomerAccount, Long> {
    
        List<CustomerAccount> findByLastName(String name);
    
    }
    ```
    
5. Create a Java Controller class for REST endpoints.
    

* The Lombok @RequiredArgsConstructor annotation saves the time of writing the constructor.
    
* The Lombok @Slf4j annotation facilitates SLF4J for logging with the least configurations.
    
    ```java
    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    
    @RestController
    @Slf4j
    @RequiredArgsConstructor
    public class CustomerAccountController {
    
        private final CustomerAccountRepository customerAccountRepository;
    
        @GetMapping("/api/customerAccounts")
        public Iterable<CustomerAccount> getAllCustomerAccounts() {
            log.debug("->getAllCustomerAccounts");
            return customerAccountRepository.findAll();
        }
    
        @GetMapping("/api/customerAccount/{lastName}")
        public List<CustomerAccount> findByLastName(@PathVariable("lastName") String name) {
            log.debug("->findByLastName");
            return customerAccountRepository.findByLastName(name);
        }
    
        @PostMapping("/api/customerAccounts")
        public CustomerAccount saveCustomerAccount(@RequestBody CustomerAccount customerAccount) {
            log.debug("->saveCustomerAccount {}", customerAccount);
            return customerAccountRepository.save(customerAccount);
        }
    }
    ```
    

1. Enable Spring Boot Actuator in application.properties.
    
    ```yaml
    management.endpoint.health.enabled=true
    ```
    

### Test the application functions Locally

In IntelliJ Terminal,

1. Lists credentialed accounts and make sure the desired account is set active.
    
    ```shell
    gcloud auth list
    ```
    
2. Set the default project.
    
    ```shell
    gcloud config set project PROJECT_ID
    ```
    
    * *Replace PROJECT\_ID with your project ID.*
        
3. Show the configuration.
    
    ```shell
    gcloud config list
    ```
    
4. Start the Spring Boot application.
    
    ```shell
    ./mvnw spring-boot:run
    ```
    

Use a REST client, like Postman, to perform following tests:

1. Perform health check.
    
    ![Health Check Local Test](https://cdn.hashnode.com/res/hashnode/image/upload/v1642461587055/bLOLXkXnP.png align="left")
    
2. Test application function: Create new Customer Account.
    
    ![Local Test: Create new Customer Account](https://cdn.hashnode.com/res/hashnode/image/upload/v1642461620621/KTr0CpoW4.png align="left")
    
3. Test application function: Find by Last Name.
    
    ![Local Test: Find by Last Name](https://cdn.hashnode.com/res/hashnode/image/upload/v1642461657998/xQSPbrb61.png align="left")
    
4. Test application function: List all Customer Accounts.
    
    ![Local Test: List all Customer Accounts](https://cdn.hashnode.com/res/hashnode/image/upload/v1642461688378/UTQ-gdBYi.png align="left")
    

## Containerize the Microservice

In general, we need to write a Dockerfile to dockerize the microservice.

However, in this exercise, we will use Google Cloud's Jib Maven Plugin to build the container images without writing a Dockerfile.

1. Add Jib Maven plugin in pom.xml.
    
    ```xml
    <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>jib-maven-plugin</artifactId>
        <version>3.1.4</version>
        <configuration>
            <to>
                <image>europe-west2-docker.pkg.dev/java-microservice-2022/ms-containers/${project.artifactId}</image>
            </to>
        </configuration>
    </plugin>
    ```
    
    * *Replace with your Full Container Name inside the &lt;image&gt; tag.*
        
2. At the project root, execute the following command:
    
    ```shell
    ./mvnw install jib:build
    ```
    
    This will build the docker image and publish it to the Artifact Registry.
    
    Use the following command to list docker images in the Artifact Registry.
    
    ```shell
    gcloud artifacts docker images list FULL_CONTAINER_NAME
    ```
    
    * *Replace FULL\_CONTAINER\_NAME with your Full Container Name.*
        
    
    Or you can check the image in Google Cloud Console.
    
    ![Images in Artifact Registry](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462620563/Jafix40Vu.png align="left")
    

## Deploy the Microservice to GKE

### Configure a Run/Debug Configuration for Cloud Code: Kubernetes

In this process, we need to write manifest files in YAML format. One is deployment.yaml and the other is service.yaml.

1. Write deployment.yaml and put it in the project root directory.
    
    ```yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: account-service-deployment
    spec:
      selector:
        matchLabels:
          app: account-service-pod
      replicas: 1
      template:
        metadata:
          labels:
            app: account-service-pod
        spec:
          containers:
            - name: account-service-container
              image: europe-west2-docker.pkg.dev/java-microservice-2022/ms-containers/ms.client.account.gcp
              ports:
                - containerPort: 8080
              livenessProbe:
                initialDelaySeconds: 20
                httpGet:
                  port: 8080
                  path: /actuator/health
              readinessProbe:
                initialDelaySeconds: 30
                httpGet:
                  port: 8080
                  path: /actuator/health
    ```
    
    * Replace *account-service-deployment* with the name of your deployment.
        
    * Replace *account-service-pod* with the name of your pod.
        
    * Replace *account-service-container* with the name of your container.
        
2. Write server.yaml and put it in the project root directory.
    
    ```yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: account-service
    spec:
      type: NodePort
      selector:
        app: account-service-pod
      ports:
        - port: 8080
          targetPort: 8080
    ```
    
    * Replace *account-service* with the name of your service.
        
    * Replace *account-service-pod* with the name of your pod.
        
3. Add a new Run/Debug Configuration for *Cloud Code: Kubernetes*.
    
    ![New configuration for Cloud Code: Kubernetes](https://cdn.hashnode.com/res/hashnode/image/upload/v1642461750152/YKR559WLh.png align="left")
    
4. Change the name of the Configuration.
    
    ![New configuration for Cloud Code: Kubernetes](https://cdn.hashnode.com/res/hashnode/image/upload/v1642461805155/CznaFZW6H.png align="left")
    
5. Click the "Initialize" button in the "Build/Deploy" tab.
    
    ![Configuration for Cloud Code: Kubernetes](https://cdn.hashnode.com/res/hashnode/image/upload/v1642461833900/pASE2_FhO.png align="left")
    
6. Click the "pen" icon under *Build Settings*.
    
    ![Configuration for Cloud Code: Kubernetes](https://cdn.hashnode.com/res/hashnode/image/upload/v1642461867063/Dnq2lC-Hy.png align="left")
    
7. Select *Jib Maven Pluin* from the *Builder* dropdown list and click the "OK" button.
    
    ![Configuration for Cloud Code: Kubernetes](https://cdn.hashnode.com/res/hashnode/image/upload/v1642461893543/bmdDtdxRS.png align="left")
    
8. Click the "Initialize" button.
    
    ![Configuration for Cloud Code: Kubernetes](https://cdn.hashnode.com/res/hashnode/image/upload/v1642461920433/E9PUAnIKa.png align="left")
    
    ![Configuration for Cloud Code: Kubernetes](https://cdn.hashnode.com/res/hashnode/image/upload/v1642461995462/5yVKE6CoYQ.png align="left")
    
9. A *skaffold.yaml* file will be generated at the Project Root Directory.
    
    ![Configuration for Cloud Code: Kubernetes](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462032617/S03CUVV5Y.png align="left")
    
10. Click the "Run" tab.
    
    ![Run Configuration for Cloud Code: Kubernetes](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462060300/ZOeGh1jzm.png align="left")
    
    An alert message "Current Kubernetes context unknown. Please ensure you have Kubernetes clusters configured on your machine (such as minikube or GKE)." in red in shown.
    
    To fix this problem, get authentication credentials to interact with the cluster by typing the following command in *Terminal* of InteglliJ.
    
    ```shell
    gcloud container clusters get-credentials CLUSTER_NAME
    ```
    
    * *Replace CLUSTER\_NAME with your GKE cluster name.*
        
    
    The problem should then be solved.
    
    ![Run Configuration for Cloud Code: Kubernetes](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462156340/g2awxSSOe.png align="left")
    
    Please note that the granted credentials are valid in this session only. If your session to Google Cloud is lost, you have to get the credentials again.
    
    At last, make sure the option "*Delete deployments when finished*" is unchecked.
    
    ![Run Configuration for Cloud Code: Kubernetes](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462195533/-ZWcDgH4_.png align="left")
    
    Click the "OK" button to finish the configuration.
    
11. In case of any changes of the deployment.yaml and service.yaml files, delete skaffold.yaml file generated and repeat steps 5-9 to generate the skaffold.yaml file again.
    

### Deploy to GKE

1. Click the "Run" button of the *Cloud Code: Kubernetes* configuration just created.
    
    ![Cloud Code: Kubernetes Toolbar](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462226407/T0JxJwLhk.png align="left")
    
2. The deployment process should be started.
    
    ![Cloud Code: Kubernetes Toolbar](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462257968/7QheC60My.png align="left")
    
3. Once the deployment is finished, test locally using the health check endpoint provided by Spring Actuator.
    
    `http://localhost:8080/actuator/health`
    
    ![Health Check](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462283549/FRrUzoDYU.png align="left")
    
4. In IntelligJ Terminal, type the following command to list the services.
    
    ```shell
    kubectl get service
    ```
    
    ![Health Check](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462305714/0I-OpzY96.png align="left")
    
    The following command lists the service details in yaml format.
    
    ```shell
    kubectl get service account-service --output yaml
    ```
    
5. Expose with a target of 8080 using the following command.
    
    ```shell
    kubectl expose deployment {deployment-name}  --name={load-balancer-service-name} --type=LoadBalancer --port 80 --target-port 8080
    ```
    
    Here is the actual command I used in this exmaple.
    
    ```shell
    kubectl expose deployment account-service-deployment --name=lb-account-service-gcp --type=LoadBalancer --port 80 --target-port 8080
    ```
    
    Use the following command to check the progress:
    
    ```shell
    kubectl get service
    ```
    
    It may take several minutes to bind the external IP address.
    
    ![Health Check](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462334727/MUuy4xxT2.png align="left")
    
    Keep trying until an external IP address is bound.
    
    ![Health Check](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462356519/3Tr0uThP3.png align="left")
    
    With the external IP, we can now perform tests.
    

### Test the application functions in GKE

1. Health Check using external IP:
    
    ![Health Check](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462386558/wwesijisr.png align="left")
    
2. Create new Customer Account using external IP:
    
    ![Health Check](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462409971/B3cwIyL9V.png align="left")
    
3. List all Customer Accounts using external IP:
    
    ![Health Check](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462432611/Ud-_d_s_O.png align="left")
    
4. Find Customer Account by Last Name using external IP:
    
    ![Health Check](https://cdn.hashnode.com/res/hashnode/image/upload/v1642462453758/BhajJzHQw.png align="left")
    

## Clean up

Remember to clean up in order to avoid incurring cost.

```shell
kubectl delete service account-service
gcloud container clusters delete ms-cluster --region europe-west2-c
```

## Conclusion

Hoorays!

We had created a Java Microservice, which interface with Google Cloud's NoSQL database. We then created a GKE cluster and deployed our microservice to it.

## References

1. [Complete Source Code](https://github.com/adafycheng/ms-client-account-gcp)
    
2. [Introduction to SLF4J](https://www.baeldung.com/slf4j-with-log4j2-logback)
