Deploying Scalable Java Microservices to Enterprise Kubernetes using Red Hat OpenShift
Tags: java, kubernetes, microservices, devops
Published: 2021-06-04 16:47:38

Three of the fastest-growing architectural and deployment models in software today are microservices, Kubernetes and hybrid cloud. In this lab, we'll walk through building a hybrid cloud microservice application using Java and Open Liberty, deploying that application to a Kubernetes cluster running Red Hat OpenShift. We'll do this using free (!) and trial resources on IBM Cloud.

ℹ️ This lab uses parts of the [Open Liberty OpenShift Operators Lab](https://openliberty.io/guides/cloud-openshift-operator.html), but consolidated and updated with the latest syntax and lab versions.

# 1. Create your Account

In order to get your free cluster assigned, you'll need to 
[Sign Up for IBM Cloud](https://ibm.biz/BdfSGs)

ℹ️ Issues activating your account? Make sure to check your spam folder for the activation email, and if your free email account doesn't work, try another email address, e.g. your work email. For some reason, outlook.com accounts often won't register.

# 2. Grab your Red Hat OpenShift Cluster

Visit <https://olsolab.mybluemix.net> and remember to use the following **Key: oslab**

(For the email address, use the same email address you used to register your IBM Cloud account.)

Now, you'll be able to access your Red Hat OpenShift Cluster through the IBM Cloud dashboard. Visit <https://cloud.ibm.com/resources>:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/94ycmjen48qfwoxxjv5x.png)

Now, click on your cluster name to access the cluster details page on IBM Cloud:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m95ti3t0e3o3r2vvznom.png)

In the upper right corner, pull down the **Actions** menu and select **Connect with CLI**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9cb8oo9ljbii6d9vci57.png)

After clicking **Request Token**, you'll see a custom command created under **Log in with this token**. Copy that to the clipboard, we'll use it in a second!

Back to your IBM Cloud dashboard, click on the **IBM Cloud Shell icon**.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ag5kbb3fum2a9i4h3x5c.png)

The shell will take a few seconds to spin up. Once it's live, you can **paste** the `oc login ...` command that we copied a few steps earlier. Voila, you're now logged in to your OpenShift cluster from the IBM Cloud Shell, and you can run commands using `oc`!

# 3. Installing your Red Hat OpenShift Operators

First, let's create a new project. I'll call my project **guide**. You can do this through the Red Hat OpenShift UI, or through the CLI:

```
oc new-project guide
```

Next, we'll install two operators: **Strimzi** for Kafka and **Open Liberty**. For this install we'll be using the latest Open Liberty operator, but specifically we'll use version **0.22.1** of Strimzi.

First, click the OpenShift Web Console link on your IBM Cloud cluster page:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g94dayzdp1o9p3a40yzu.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6as87ry4sqshe1u2yjor.png)

From the left menu, click **Operators >> Operator Hub**.

Then make sure you change to the appropriate project, which we've called **guide**.

ℹ️ You can use any project name, just make sure to be consistent.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7tqfx4w7v7s7rh7y2xk.png)

Now that we have the correct project selected, let's install the **latest** version of **Open Liberty** and version **0.22.1** of **Strimzi**.

# 3. Clone the code!

Let's utilize the example code provided by the Open Liberty project in order to deploy some simple microservices. We'll clone the code using these commands:

```
git clone https://github.com/openliberty/guide-cloud-openshift-operator.git
cd guide-cloud-openshift-operator
cd start
```

We'll modify the files in the `start` directory in this lab. Eventually it will resemble the `finish` directory, but you can skip ahead to `finish` if you want to :D

# 4. Create kafka.yaml

You can configure the specifics of the Strimzi Operator-controlled Kafka deployment with a YAML configuration file.

Ensure that you are in the start directory, and create the file `kafka.yaml` in your favorite editor.

ℹ️ Note: Due to [new api incompatibilities](https://strimzi.io/blog/2021/04/29/api-conversion/), this kafka.yaml file is different than in the Open Liberty tutorial.

```bash
apiVersion: kafka.strimzi.io/v1beta2
kind: Kafka
metadata:
  name: kafka-cluster
spec:
  kafka:
    version: 2.5.0
    replicas: 1
    listeners:
    - name: plain
      port: 9092
      type: internal
      tls: false
    config:
      offsets.topic.replication.factor: 1
      transaction.state.log.replication.factor: 1
      transaction.state.log.min.isr: 1
      log.message.format.version: 2.5
    storage:
      type: ephemeral
  zookeeper:
    replicas: 1
    storage:
      type: ephemeral
  entityOperator:
    topicOperator: {}
    userOperator: {}
```

ℹ️ Note: having issues with `vim` and indentation? Try setting `:set paste` before pasting, then `:set nopaste` after you're done.

Now, let's deploy Kafka based on this file:

```
oc apply -f kafka.yaml
```

# 5. Building the Microservices

Run the following commands to package the system and inventory microservices:

```
mvn -pl models clean install
mvn clean package
```

Now, let's create a `build.yaml` file:

```
apiVersion: v1
kind: Template
metadata:
  name: "build-template"
  annotations:
    description: "Build template for the system and inventory service"
    tags: "build"
objects:
  - apiVersion: v1
    kind: ImageStream
    metadata:
      name: "${APP_NAME}-imagestream"
      labels:
        name: "${APP_NAME}"
  - apiVersion: v1
    kind: BuildConfig
    metadata:
      name: "${APP_NAME}-buildconfig"
      labels:
        name: "${APP_NAME}"
    spec:
      source:
        type: Binary
      strategy:
        type: Docker
      output:
        to:
          kind: ImageStreamTag
          name: "${APP_NAME}-imagestream:1.0-SNAPSHOT"
parameters:
- description: The application name [system|inventory]
  name: APP_NAME
```

Run the following commands to create the objects for the system and inventory microservices:

```
oc process -f build.yaml -p APP_NAME=system | oc create -f -
oc process -f build.yaml -p APP_NAME=inventory | oc create -f -
```

Finally, let's trigger the builds:

```
oc start-build system-buildconfig --from-dir=system/.
oc start-build inventory-buildconfig --from-dir=inventory/.
```

# 6. Deploying the Microservices

Next, it's time to manage the deployment. Create a `deploy.yaml` file:

```
apiVersion: openliberty.io/v1beta1
kind: OpenLibertyApplication
metadata:
  name: system
  labels:
    name: system
spec:
  applicationImage: guide/system-imagestream:1.0-SNAPSHOT
  env:
    - name: WLP_LOGGING_MESSAGE_FORMAT
      value: "json"
    - name: WLP_LOGGING_MESSAGE_SOURCE
      value: "message,trace,accessLog,ffdc,audit"
    - name: MP_MESSAGING_CONNECTOR_LIBERTY_KAFKA_BOOTSTRAP_SERVERS
      value: "[kafka-bootstrap-address]"
---
apiVersion: openliberty.io/v1beta1
kind: OpenLibertyApplication
metadata:
  name: inventory
  labels:
    name: inventory
spec:
  applicationImage: guide/inventory-imagestream:1.0-SNAPSHOT
  service:
    port: 9085
  expose: true
  env:
    - name: WLP_LOGGING_MESSAGE_FORMAT
      value: "json"
    - name: WLP_LOGGING_MESSAGE_SOURCE
      value: "message,trace,accessLog,ffdc,audit"
    - name: MP_MESSAGING_CONNECTOR_LIBERTY_KAFKA_BOOTSTRAP_SERVERS
      value: "[kafka-bootstrap-address]"
```

You'll notice this file has a value missing, `[kafka-bootstrap-address]`. You can find this in your pod configuration on OpenShift, it should look like this: `my-cluster-kafka-bootstrap:9092`

You may be able to get the correct value by running:

```
oc get kafka kafka-cluster -o=jsonpath='{.status.listeners[?(@.type=="plain")].bootstrapServers}{"\n"}'
```

Replace `[kafka-bootstrap-address]` with the new value.

Next, deploy the application by running

```
oc apply -f deploy.yaml
```

# 7. Accessing the Live URL

In order to get the public route, run:

```
oc get routes
```

You'll see an output similar to:

```
NAME        HOST/PORT                                    PATH   SERVICES    PORT       TERMINATION   WILDCARD
inventory   inventory-guide.apps.lights.os.fyre.ibm.com         inventory   9085-tcp                 None
```

Visit the inventory microservice by going to the following URL: http://[HOST]/inventory/systems (making sure to substitute the host name with the host value from the above command.

Congratulations! You've just finished setting up and deploying an Open Liberty microservice on Red Hat OpenShift!

# Resources:
* [Future IBM Developer Crowdcasts](https://www.crowdcast.io/ibmdeveloper)
* [Mary's Twitter](https://twitter.com/mgrygles)
* [Dave's Twitter](https://twitter.com/drnugent)
* [Join Mary on Discord](https://discord.gg/RMU4Juw)