Deploying a custom Kogito service on Kubernetes with Kogito Operator
Tags: kogito, operators, kubernetes, openshift
Published: 2020-08-07 15:58:21

[The Kogito documentation](https://docs.jboss.org/kogito/release/latest/html_single/#con-kogito-automation_kogito-docs) states that “Kogito is a cloud-native business automation technology for building cloud-ready business applications”. In other words, a developer toolkit to create a custom business automation application targeting cloud environments.

Kogito enables a developer to have a business process integrated with complex business decisions and rules. This process is converted into a microservice that can be deployed on a containerized environment, such as Kubernetes.

In the following sections you'll learn step by step how you can deploy a custom Kogito service in a Kubernetes cluster using the [Kogito Operator](https://operatorhub.io/operator/kogito-operator).

---

## Getting Started

To get started deploying your first custom Kogito service on Kubernetes you will need:

1. A Kubernetes cluster with cluster admin rights on it ([minikube](https://kubernetes.io/docs/setup/learning-environment/minikube/) is recommended). You'll need cluster admin rights because you are going to [install new Kubernetes types](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/#custom-resources) in the cluster
2. The latest Kogito Operator. The [latest released version](https://github.com/kiegroup/kogito-cloud-operator/releases) is available at the Github page
3. [kubectl installed](https://kubernetes.io/docs/tasks/tools/install-kubectl/) in your local machine
4. Java 11, git (latest), maven (3.6.x) and [podman](https://podman.io/) (or docker) installed locally

_TIP: If you're concerned about the resources consumed by the minikube instance, for this tutorial I used an instance with 2 CPUs and 4GB RAM._

In the [Kogito Examples repository](https://github.com/kiegroup/kogito-examples/), you will find a lot of examples and use cases to get started with Kogito. To keep things simple, we will deploy the [Process with Business Rules on Quarkus](https://github.com/kiegroup/kogito-examples/tree/master/process-business-rules-quarkus) example. In this example, our business process will evaluate whether we need a special handling for children or not. To express the process we use the [BPMN specification](https://www.omg.org/spec/BPMN/2.0/About-BPMN/):

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

Kogito offers a [VSCode plugin](https://marketplace.visualstudio.com/items?itemName=kie-group.vscode-extension-kogito-bundle) to author BPMN diagrams. This same editor can also be used online, try it out by [clicking here](https://kiegroup.github.io/kogito-online/?file=https://raw.githubusercontent.com/kiegroup/kogito-examples/master/process-business-rules-quarkus/src/main/resources/org/acme/travels/persons.bpmn#/editor/BPMN) to view and edit the above diagram live!

The nicest thing about BPMN is that you can use the same language to talk to business and IT personas. Plus, this diagram you are working with is about to be transformed into a microservice running on top of Kubernetes.

The image below illustrates the overall process since the creation of a new project to its deployment.

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

As a developer, you will create a new Java project having custom business rules, decisions or processes, then build and try it locally before deploying it into a Kubernetes cluster. 

On Kubernetes, your custom Kogito service will be just one more microservice in your architecture, exposing the domain through REST API and well documented with Swagger. Other services won't even be aware of your internal implementation since no Kogito API internals are exposed. [Domain data is the only thing that matters here](https://docs.jboss.org/kogito/release/latest/html_single/#_domain_specific_flexibility).

---

## Trying the Kogito service locally

The first step to try the example is to clone the Kogito Examples’s project and to run the target project with the Quarkus Maven plugin. Execute the commands below, and notice the command will keep on running:

{% gist https://gist.github.com/ricardozanini/07334615f0736c81ca59c50dc2300729 %}

_TIP: This example is using [Quarkus](https://quarkus.io/) as the target runtime, but [SpringBoot](https://github.com/kiegroup/kogito-examples/tree/stable/process-business-rules-springboot) is also an option to build Kogito services._

After running the above command, open a new terminal and start a new instance of the process by making a request to the “persons” endpoint:

{% gist https://gist.github.com/ricardozanini/f2cb70b17a16c22a5f42e8b4a5ff270e %}

_TIP: run it on a different terminal since the Quarkus maven plugin will be in the foreground._

To play around with the endpoints via the Swagger interface, go to http://localhost:8080/swagger-ui/. If you need more use cases of this service (such as how to retrieve a pending task), please refer to the [example page](https://github.com/kiegroup/kogito-examples/tree/stable/process-business-rules-quarkus#example-usage).

Notice that every generated endpoint only contains information about the domain data, in other words, it does not have Kogito specific APIs exposed.

Before heading to the next step, stop the running instance of Quarkus to release the 8080 port.

---

## Building the Kogito service Image

Having tried the custom service locally, it’s time to deploy it in your Kubernetes cluster. For this to work, you will have to build the package and the image locally and push it to a third party registry such as Quay.

To build the package, just run this command: `mvn clean package`

Packaged files will then be available into the “target” folder.

Then to build the image, use the following `Dockerfile` as reference:

{% gist https://gist.github.com/ricardozanini/1aa0f0d4b13b80448b5e68800e42ccbf %}

Copy the content above into a Dockerfile file and add it to the project’s root (process-business-rules-quarkus). This Dockerfile is based on a [special image offered by the Kogito project](https://quay.io/repository/kiegroup/kogito-quarkus-jvm-ubi8?tag=latest&tab=tags), and it has all the binaries and scripts needed by a custom Kogito service built with Quarkus to run. To build the image, execute the following command in the project’s root directory:

```shell
$ podman build — tag quay.io/<yournamespace>/process-business-rules-quarkus:latest .
```

Remember to update the _yournamespace_ tag with your [Quay.io](https://quay.io) username!

After building the image you can run it locally:

```shell
$ podman run — rm -it -p 8080:8080 quay.io/<yournamespace>/process-business-rules-quarkus:latest
```

_TIP: Don’t forget to stop the Quarkus application that you were testing before, since the ports could clash._

You can do the same requests you’ve made so far running the example locally with the Quarkus plugin. Since we’re mapping the same port used by the service on the host machine, all the requests to `localhost` should work.

After checking that the image is working, we can push it to Quay:

```
$ podman push quay.io/<yournamespace>/process-business-rules-quarkus:latest
```

Once the image has been pushed to the registry, make sure that it is available publicly. Some Quay accounts create new tags in private mode by default.

---

## Deploying with Kogito Operator

Here comes the fun part.

Having built the image, we can use the Kogito Operator to deploy it in the Kubernetes cluster. Assuming that you’ve installed and started the minikube locally (or you have cluster admin access to a remote Kubernetes cluster), let’s first install the operator.

### Installing the Kogito Operator

To install the [Kogito Operator](https://github.com/kiegroup/kogito-cloud-operator/releases) in your cluster, you can download the latest source code of the Kogito Operator and use our installation script:

{% gist https://gist.github.com/ricardozanini/7b7bf001365090f764463ceef86c34a2 %}

You should see the operator pod running in the namespace by executing:

{% gist https://gist.github.com/ricardozanini/9f2f0bf3c05d0b0115c3410aec4855f2 %}

If it is the first time you deploy the operator it could take a while to Kubernetes download and start the operator pod.

Alternatively, you can use OLM to install the operator in your cluster. If you want to give it a try, you can follow the instructions listed in the [Kogito OperatorHub](https://operatorhub.io/operator/kogito-operator) page.

### Deploying the Kogito service

To deploy the Kogito service you’ll have to create a Kubernetes custom resource `KogitoRuntime` and deploy it in the namespace which you’ve installed the operator:

{% gist https://gist.github.com/ricardozanini/737e824eda28f25f58c6cf2cb634b4af %}

After a few seconds, you should be able to access the deployed service.

Try fetching it with:

{% gist https://gist.github.com/ricardozanini/c9b4ce5310a2ef225e2a39db78659e91 %}

### Exposing the Kogito service with an Ingress

If you’re using Minikube, you can expose the service using an NGINX Ingress, to achieve that, follow the instructions in the Minikube [documentation](https://kubernetes.io/docs/tasks/access-application-cluster/ingress-minikube/#enable-the-ingress-controller) explaining how to expose a service. Then, create a new Ingress using the following example:

{% gist https://gist.github.com/ricardozanini/3ddf7173a5946fe13698ac9cb8f68cfd %}

You need to update your `hosts` file as explained in the Minikube [documentation](https://kubernetes.io/docs/tasks/access-application-cluster/ingress-minikube/#create-an-ingress-resource) to be able to access the service through http://kogito-example.com URL.

After updating the `hosts` file you can play around with the application endpoints using the same commands you used before:

{% gist https://gist.github.com/ricardozanini/1e02c046936d36c226229cca0d7aac47 %}

---

### Conclusion

In this tutorial you learned how to deploy a custom Kogito service in a Kubernetes cluster using the Kogito Operator. A Kogito service plays a crucial role in a microservice architecture that relies on complex business processes and decisions, abstracting the inner business requirements into standards, exposing only the domain data to other services.

There are a lot of examples in the Kogito Examples project structure, which you may try to deploy on Kubernetes using this tutorial as a reference.

In the next tutorial, you will approach how to deploy in Kubernetes a custom Kogito service that consumes and publishes events using Kafka. Stay tuned!