Deploy Maven Apps to Kubernetes With JKube Kubernetes Maven Plugin
In this article you’ll learn how to containerize your Maven application into Docker Images and automatically generate and deploy Kubernetes Manifests with the help of Eclipse JKube’s Kubernetes Maven Plugin.
A Brief Introduction to Kubernetes
Kubernetes is an open source project, originally developed by Google, which is now managed by Cloud Native Computing Foundation. It surged in popularity in 2017 among people who were already using container technologies introduced by Docker. It’s an orchestration engine that underlies how operations staff deploy and manage containers at scale. For more detailed information about Kubernetes, you can checkout these articles:
DZone: A Beginner’s guide to Kubernetes
DZone: A Complete introduction to Kubernetes
Eclipse JKube - Java Tooling for Kubernetes:
All right, now that we know about Kubernetes. We can now take a look at the project which we’ll be talking about in this blogpost: Eclipse JKube. It is a project which you can use to package your applications into Docker Images and deploy to Kubernetes smoothly. It is the successor to famous Fabric8 Maven Plugin.
At Eclipse Cloud Tooling we have been focused on making Java developers’ experience smoother when it comes to creating, building, deploying and managing microservices on top of Kubernetes for some time. Most of the Java developers are used to application servers, creating deployment jars/wars, deploying then and running them in web application containers. They mostly use their build tools like Maven/Gradle or their IDE to do most of the work.
Hence we thought about providing a developer experience in which working on top of Kubernetes would look just like working on an application server to a Java developer. So building and deploying applications from maven to Kubernetes would look exactly like it would with other maven plugins like spring-boot, tomcat, jetty, wildfly, quarkus etc. This can get Java developers get started quickly with Kubernetes since they can treat it like an application server.
We’ll now be taking a look at Eclipse JKube Kubernetes Maven Plugin and how it provides a similar developer experience on top of Kubernetes like you usually have with your application frameworks like Spring Boot, Quarkus, Vert.x etc.
XML
x
1
<plugin>
2
<groupId>org.eclipse.jkube</groupId>
3
<artifactId>kubernetes-maven-plugin</artifactId>
4
<version>${jkube.version}</version>
5
</plugin>
|
Setting Up Your Maven Project:
Do you have an existing maven project for your Java application? Maybe it’s a Quarkus or Spring Boot application; or a flat classpath, fat jar? If you don’t have a maven application, you can simply visit code.quarkus.io or start.spring.io and create one. You can also pick up a quickstart from Eclipse JKube quickstarts.
You can find latest version of Eclipse JKube on Maven Central as usual. To add Eclipse JKube Kubernetes Maven Plugin in your project you can simply add this section in your pom.xml like this:
<plugin>
<groupId>org.eclipse.jkube</groupId>
<artifactId>kubernetes-maven-plugin</artifactId>
<version>1.0.2</version>
</plugin>
Note that all Eclipse JKube Quickstarts already have Kubernetes Maven Plugin enabled so you can simply skip above step.
Now that we have Kubernetes Maven Plugin integrated into our project we can do something interesting stuff. Let’s take a short look the the goals this plugin provides. We’ll be taking a closer look at each of these goals later:
Goal Name |
Description |
K8s:build |
Containerize your application into an image |
K8s:push | Push the Image built to a container registry |
K8s:resource | Generate Kubernetes Manifests |
K8s:deploy | Apply these manifests on top of Kubernetes |
K8s:log | View logs of your application running in Kubernetes Cluster |
K8s:debug | Debug your application running into Kubernetes |
All right, let’s take a look at each of these goals in detail one by one:
1. Containerizing Your Application:
Kubernetes Maven Plugin makes containerizing your Maven applications extremely easy. It allows building Docker images without providing any configuration with the help of it’s opinionated defaults. Of course, you can customize this very easily by allowing customization of image via XML Configuration or by providing your own Dockerfile.
To build Docker image of your application, just run the k8s:build
goal(Default strategy for building images Is Docker so It requires access to a docker daemon):
xxxxxxxxxx
mvn k8s:build
This will create a Docker Image in the daemon you performed this goal against. You can check the image build using docker images
command. If you don’t have access to docker daemon, you can change build strategy to JIB like this:
xxxxxxxxxx
mvn k8s:build -Djkube.build.strategy=jib
It will generate a tar file for your image which can be copied and loaded in any other docker daemon.
2. Pushing Image to a Registry:
Once you’ve build an image, you might want to push it to a remote registry like DockerHub or Quay. You can easily achieve this with Kubernetes Maven Plugin. Before doing it, you need to make sure that the image you’ve build is correct ${registry}/${user}/${image-name}:${tag}
format. If you’re providing your own XML Configuration for building image, you can simply change image name by changing name section like this:
xxxxxxxxxx
<plugin>
<groupId>org.eclipse.jkube</groupId>
<artifactId>kubernetes-maven-plugin</artifactId>
<version>${jkube.version}</version>
<configuration>
<images>
<image>
<!-- Registry: quay.io -->
<!-- Username: rohankanojia -->
<name>quay.io/rohankanojia/helloapp:${project.version}</name>
<alias>hello-world</alias>
<build>
<from>openjdk:latest</from>
<cmd>java -jar maven/${project.artifactId}-${project.version}.jar</cmd>
</build>
</image>
</images>
</configuration>
</plugin>
If you’re using Kubernetes Maven Plugin’s Zero Configuration, you need to change default image name generated by Kubernetes Maven Plugin by providing this property:
xxxxxxxxxx
<!-- Registry: quay.io -->
<!-- Username: rohankanojia -->
<jkube.generator.name>quay.io/rohankanojia/helloapp:${project.version}</jkube.generator.name>
For registry authentication, Kubernetes Maven Plugin automatically detects credentials from these locations:
~/.docker/config.json
(docker login config file)~/.m2/settings.xml
(store container registry credentials like maven registry)- In
<auth>
section in plugin configuration
You can check these in detail in Kubernetes Maven Plugin Authentication Docs.
In order to push image to specified registry, you can go ahead and use k8s:push
goal:
xxxxxxxxxx
mvn k8s:push
Or if you had built your image with JIB build strategy, you can do:
xxxxxxxxxx
mvn k8s:push -Djkube.build.strategy=jib
3. Generate Kubernetes Manifests:
Kubernetes Maven Plugin also automatically generates opinionated Kubernetes Manifests by inspecting your project’s dependencies. Of course, it can easily be customized by providing your own manifests or small portions of YAML in src/main/jkube
directory, For more information check out plugin documentation about Resource Fragments.
In order to generate Kubernetes Manifests, you just need to run k8s:resource
goal:
xxxxxxxxxx
mvn k8s:resource
This would generate Kubernetes manifests which would be available in target/
directory, as shown in listing below:
xxxxxxxxxx
eclipse-jkube-demo-project : $ tree target/classes/META-INF/jkube/
target/classes/META-INF/jkube/
├── kubernetes
│ ├── random-generator-deployment.yml
│ └── random-generator-service.yml
└── kubernetes.yml
1 directory, 3 files
4. Deploying To Kubernetes:
You can use Kubernetes Maven Plugin’s deploy goal in order to apply the Kubernetes YAML manifests generated in previous step on top of Kubernetes Cluster. You just need to be logged into a Kubernetes cluster. If you want to make whole deployment to Kubernetes a single step operation you can bind various goals like this:
xxxxxxxxxx
<plugin>
<groupId>org.eclipse.jkube</groupId>
<artifactId>kubernetes-maven-plugin</artifactId>
<version>${jkube.version}</version>
<!-- Connect k8s:resource, k8s:build to lifecycle phases -->
<executions>
<execution>
<id>jkube</id>
<goals>
<goal>resource</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
Then you can deploy your application in just one step using k8s:deploy
goal:
xxxxxxxxxx
mvn k8s:deploy
This will build your java code and run your unit tests, generate the docker image, create the kubernetes manifest and deploy them into kubernetes.
To remove your application from kubernetes just use the k8s:undeploy
goal:
xxxxxxxxxx
mvn k8s:undeploy
5. Inspect Logs:
Once your application has been deployed into Kubernetes using Kubernetes Maven Plugin; you might want to inspect its logs. You can use k8s:log
goal which would output logs for your application running inside Kubernetes:
xxxxxxxxxx
mvn k8s:log
Use Ctrl+C to stop tailing the log
If you wish to get the log of the app and then terminate immediately then use this option:
xxxxxxxxxx
mvn k8s:log -Djkube.log.follow=false
6. Live Debugging App Running Inside Kubernetes:
To debug your application deployed using `k8s:deploy` goal, just run the k8s:debug
goal:
xxxxxxxxxx
mvn k8s:debug
This will use the default Java remote debugging port of 5005. You can also specify a different port if you want:
xxxxxxxxxx
mvn k8s:debug -Djkube.debug.port=8000
Once goal is up and running, a debug port would be opened on your laptop(localhost) which then connects using Kubernetes Pod Port forwarding to your latest application Pod.
You can add a debug execution in IntelliJ like this:
This concludes the overview of common Kubernetes Maven Plugin goals. There are other goals too like k8s:helm
and k8s:watch
But we won't be covering them in this blog.
Demo:
You can watch these short YouTube videos in order to see this plugin in action:
You can also checkout our Getting started course on Katakoda: https://katacoda.com/jkubeio
Conclusion
Please try out Kubernetes Maven Plugin and let us know via Community or via Issue Tracker .Are you interested in joining us to make this project better? Don’t be shy about joining our welcoming community:
- Provide feedback on GitHub
- Craft code and push a pull request to the Eclipse JKube repository.
- Interact with the Eclipse JKube team on Gitter and the JKube mailing list .
- Ask questions and get answers on Stack Overflow .