Skip to content

Latest commit

 

History

History

jkube-helm-gradle

Automatically Generate & Package Helm Charts for your Java application (Gradle)

This is a demo repository used in one of Red Hat Developer Sandbox Article

Contents

Prerequisites

You’d need the following things in order to do this activity:

Preparing Application

This project is generated from code.quarkus.io. We basically select required dependencies for a typical REST microservice from web interface.

How to Build?

$ ./gradlew clean build

How to Run Locally?

$  java -jar build/quarkus-app/quarkus-run.jar

In another terminal, do a CURL to this URL:

$ curl localhost:8080/hello
Hello RESTEasy

Provision your free Red Hat OpenShift Development Cluster

Red Hat OpenShift Developer Sandbox is a free OpenShift cluster that gives you the experience of working with an actual Kubernetes Cluster and learning more about it. Unlike other cloud platforms, it doesn’t require any credit card (since it’s targeted towards Developers) . You just need to create a Red Hat Account and use that to provision your OpenShift Cluster.

Once you’ve created an account and logged into your OpenShift cluster.

You can also connect to this OpenShift Cluster from your terminal. This would require oc CLI binary to be installed on your machine. You can read more about this in this blog post here:

Access your Developer Sandbox for Red Hat OpenShift from the command line

To authenticate your laptop to the cluster, oc login is used but it is not mandatory. We can also specify OpenShift Developer Sandbox Cluster configuration via properties.

After clicking on the copy login command option, use Devsandbox user and click Display Token. Copy command and paste it in your terminal:

$ oc login --token=sha256~%TOKEN% --server=https://%SERVER%:6443
Logged into "https://%SERVER%:6443" as "%USERNAME%" using the token provided.

You have access to the following projects and can switch between them with 'oc project <projectname>':

  * %USERNAME%-dev
    %USERNAME%-stage

Using project "%USERNAME-dev".
Welcome! See 'oc help' to get started.

Building Image and Generating Helm Chart of your application:

We’d be using Eclipse JKube to package and deploy this application to Red Hat OpenShift. Since we’re using the maven project we’ll use the OpenShift Maven Plugin. If you’re a Gradle user, you can also use the OpenShift Gradle Plugin. You can find more information about Eclipse JKube in the following articles:

OpenShift Maven Plugin is already added in this project. You can go ahead and use Eclipse JKube OpenShift Maven Plugin goals to build a container image, generate YAML manifests, and package them into a Helm Chart.

$ ./gradlew ocBuild ocResource ocHelm

These OpenShift Maven Plugin goals will do the following things:

  • ocBuild : Creates a container image for your Java Maven application using Source to Image (S2I) build strategy. Image is built in a pod and pushed to OpenShift’s internal container registry.
  • ocResource : Generates opinionated YAML manifests (e.g. DeploymentConfig, Service, Route etc) for your Java Maven application. You can view them in the build/classes/java/main/META-INF/jkube/openshift directory.
  • ocHelm : Package generated resources into Helm Chart

Publishing Helm Chart to a Helm Registry

Once you've generated Helm Chart, you can push it to some Helm regitry using oc:helm-push goal.

You can provide Helm registry configuration like this:

openshift {
    helm {
        snapshotRepository {
            name = 'ChartMuseum'
            url = 'http://localhost:8080/api/charts'
            type = 'CHARTMUSEUM'
            username = 'user1'
        }
    }
}

You can run oc:helm-push task after that:

$ ./gradlew ocHelmPush -Pjkube.helm.snapshotRepository.password=secret

Deploying Published Helm Chart via Helm CLI

Once you’ve pushed Helm Chart to a helm registry, you can deploy your application by pulling it and installing the chart via Helm CLI.

List all Helm registries:

$ helm repo list

Get the latest chart updates from registries:

$ helm repo update 

Search the chart we pushed in the previous step in helm registry (Note that --devel option is because our chart has -SNAPSHOT version)

$ helm search repo chartmusuem --devel
NAME                         	CHART VERSION 	APP VERSION	DESCRIPTION
chartmusuem/jkube-helm-gradle	1.0.0-SNAPSHOT	           	           

Install chart

$ helm install --generate-name chartmusuem/jkube-helm-gradle --devel
I0301 22:23:38.971633   37999 request.go:665] Waited for 1.198529433s due to client-side throttling, not priority and fairness, request: GET:https://api.sandbox.openshiftapps.com:6443/apis/security.openshift.io/v1?timeout=32s
NAME: jkube-helm-maven-1646153615
LAST DEPLOYED: Tue Mar  1 22:23:40 2022
NAMESPACE: rokumar-dev
STATUS: deployed
REVISION: 1
TEST SUITE: None

This would deploy all OpenShift resources contained in this Helm chart to the OpenShift cluster. Go to Red Hat OpenShift Developer Sandbox Console. You should see the application deployed in the Topology section:

Click on Open URL and you should be redirected to your application’s page:

Cleanup

Once you’ve done testing it. You can undeploy your application using Helm uninstall command:

$ helm list
NAME                        	NAMESPACE  	REVISION	UPDATED                                	STATUS  	CHART                           	APP VERSION
jkube-helm-gradle-1646283099	rokumar-dev	1       	2022-03-03 10:21:43.993030016 +0530 IST	deployed	jkube-helm-gradle-1.0.0-SNAPSHOT
$ helm uninstall jkube-helm-gradle-1646283099