DEV Community

Dave Nugent 🌉
Dave Nugent 🌉

Posted on • Edited on

Deploying Scalable Java Microservices to Enterprise Kubernetes using Red Hat OpenShift

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, 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

ℹī¸ 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

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

Alt Text

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

Alt Text

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

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
Enter fullscreen mode Exit fullscreen mode

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

Alt Text

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

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
Enter fullscreen mode Exit fullscreen mode

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, this kafka.yaml file is different than in the Open Liberty tutorial.

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: {}
Enter fullscreen mode Exit fullscreen mode

ℹī¸ 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
Enter fullscreen mode Exit fullscreen mode

5. Building the Microservices

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

mvn -pl models clean install
mvn clean package
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 -
Enter fullscreen mode Exit fullscreen mode

Finally, let's trigger the builds:

oc start-build system-buildconfig --from-dir=system/.
oc start-build inventory-buildconfig --from-dir=inventory/.
Enter fullscreen mode Exit fullscreen mode

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]"
Enter fullscreen mode Exit fullscreen mode

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"}'
Enter fullscreen mode Exit fullscreen mode

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

Next, deploy the application by running

oc apply -f deploy.yaml
Enter fullscreen mode Exit fullscreen mode

7. Accessing the Live URL

In order to get the public route, run:

oc get routes
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

Top comments (0)