JKube

Eclipse JKube is a set of plugins and tools designed to simplify the process of building and deploying Java applications on Kubernetes and OpenShift. 

It provides Maven and Gradle plugins that handle tasks like building container images (using Docker, JIB or S2I build strategies) by examining java application information, generating Kubernetes manifests (like deployment.yaml, service.yaml files) or OpenShift manifests, and deploying applications onto these platforms.

It integrates directly with Java build systems like Maven and Gradle, allowing developers to add a plugin to their pom.xml or build.gradle files. These plugins automate the steps necessary to make applications cloud-native, such as image building, pushing to a registry, generating Kubernetes/OpenShift resources, and deploying the app on a Kubernetes cluster. 

It also provides a set of tools such as watch, debug, log, etc. to improve developer experience.

Key features

  • Containerization: It automatically builds Docker images for your java applications, handling all the necessary configurations.
  • Deployment: It deploys applications directly to Kubernetes or OpenShift clusters.
  • Integration with CI/CD: JKube integrates well with continuous integration and continuous deployment pipelines.
  • Maven/Gradle Plugins: JKube provides plugins for Maven and Gradle to enable developers to build and deploy applications easily from their build tools.
  • Customization: It allows customization of Dockerfiles, Kubernetes manifests, and other deployment aspects.

Example: Create, containerize and deploy a simple spring boot application into kubernetes cluster (minikube) using the JKube with zero configuration

Here zero configuration means we don't create dockerfile, kubernetes manifests such as deployment.yml, service.yml files manually. 

Prerequisite installation for this example: java, maven, docker, minikube, kubectl


Step1: Create a spring boot project using spring initializr and add a basic /hello RESTful API.

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello from JKube";
    }
}


Step2: Add JKube maven plugin under the plugins section to pom.xml

<plugin>
    <groupId>org.eclipse.jkube</groupId>
    <artifactId>kubernetes-maven-plugin</artifactId>
    <version>1.17.0</version>
</plugin>


👉 Finally build the project with command mvn install

👉 You can find an example code on my github page


Step3: Start your kubernetes cluster (minikube)

minikube start 


If get permission issue than change the permission of following docker file 

sudo chmod 776 /var/run/docker.sock


Step4: Build a docker image of your application inside kubernetes cluster

👉 To create docker image inside the kubernetes cluster, first access the docker daemon installed in kubernetes cluster with following command

eval $(minikube -p minikube docker-env)

If you won't run above command first than the application docker image will be built on host machine and not inside the kubernetes cluster and in that case you can still copy docker image from host machine to kubernetes cluster using commands like "docker save" and "docker load"

If you notice when we fire "docker images" command before "eval.." command list available docker images on host system and after "eval.." command list docker images available inside kubernetes cluster

mvn package k8s:build

Verify created docker image inside the kubernetes cluster

docker images

You can also see running docker containers inside the kubernetes cluster

docker ps


Step5: Generate resources

Note: JKube will generate the necessary kubernetes manifests (like deployment.yaml, service.yaml) automatically

mvn package k8s:resource


You can check kubernetes manifests (deployment.yaml, service.yaml files) generated by kubernetes-maven-plugin in target/classes/META-INF/jkube/ directory of the application.


Step6: Deploy your application docker container in kubernetes cluster (minikube) by applying created resource.

mvn package k8s:apply


Verify the deployment status using kubectl

kubectl get pods


It says there is one pod running and having one service container deployed in it

kubectl get services

You can see service (application docker container) details like which cluster IP is assigned to it and on which port it is running on. Using this IP and port you can access this service (application docker container)

Note: As you know the docker container of your application is deployed in the kubernetes cluster (minikube) so by default it won't be accessible to public as there is no external IP provided to it.

There are ways to access your service (application docker container)

  • Minikube provide an URI to access deployed service and you can get URI with this command "minikube service <SERVICE_NAME>". You can access that URI from browser
  • minikube service springboot-jkube-example



  • Or you can SSH to kubernetes cluster and fire the curl command from there
    • minikube ssh
    • curl <service-cluster-ip>:<service-port>/hello
      you can get <service-cluster-ip>:<service-port> with command "kubectl get services"

 

You can login and push application docker image to your docker hub account

  • docker login
  • mvn package k8s:push


Reference

https://eclipse.dev/jkube/docs/kubernetes-maven-plugin/#getting-started