org.eclipse.jkube/kubernetes-maven-plugin (2022)

When working with kubernetes-maven-plugin, you’ll probably be facing similar situations and followingthe same patterns other users do. These are some of the most common scenarios andconfiguration modes:

2.1. Minikube

This is an example of how you can use the JKube zero configuration to build and deploy your Java application with Minikube. This is using a Quarkus project, but it could be changed to any supported Java framework.

Prerequisites

You will need the following for the scenario:

  • minikube installed and running on your computer

  • minikube ingress addon enabled

    $ minikube addons enable ingress
  • Use the docker daemon installed in minikube

    $ eval $(minikube -p minikube docker-env)

Zero configuration

  1. Start by generating a new Gradle Quarkus project from https://code.quarkus.io. Make sure maven is selected. Download the generated project and extract the content.

2.Open the pom.xml file and add the plugin in the <plugins> section.

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

3.Run the command:

$ ./mvnw package k8s:build k8s:resource k8s:apply

The goal k8s:push is not required as we are using the Minikube internal container image registry.

At this point, the Quarkus app has been built, containerized, configured for kubernetes and deployed to the minikube cluster. But there is no external endpoint to access to it:

$ kubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.96.0.1 <none> 443/TCP 14hquarkus ClusterIP 10.101.31.90 <none> 8080/TCP 13h$ kubectl get ingressNo resources found in default namespace.

External URL

To make this service available publicly, change the settings in project properties.

  1. Retrieve the minikube ip address

    $ minikube ip192.168.99.102
  2. Add the following in the properties. For the domain, we are using nip.io service that dynamically maps custom hostname and IP address and avoid editing the /etc/hosts file.

    # Enable Creating External Urlsjkube.createExternalUrls=true# Configure host domain suffix for Ingressjkube.domain=192.168.99.102.nip.io
  3. Re-generate and apply the kubernetes resource

    $ ./mvnw k8s:resource k8s:apply
  4. Make sure the ingress resource has been created

    $ kubectl get ingressNAME CLASS HOSTS ADDRESS PORTS AGEquarkus <none> quarkus.192.168.99.102.nip.io 192.168.99.102 80 30m
  5. Give it a try:

    $ curl http://quarkus.192.168.99.102.nip.io/helloHello RESTEasy

2.2. Vanilla Kubernetes Cluster

This is an example of how you can use the kubernetes-maven-plugin to build and deploy your Java application to any Kubernetes Cluster.

Prerequisites

You will need the following for the scenario:

Adding kubernetes-maven-plugin to project

You would need to add kubernetes-maven-plugin to your project in order to use it:

Open the pom.xml file and add the plugin in the <plugins> section.

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

Building and pushing image to Container registry

Once you’ve set up your project and tested it, You can create a container image for your application using kubernetes-maven-plugin. If you’ve access to a docker daemon try running this command:

$ ./mvnw k8s:build

If you don’t have access to any docker daemon, you can configure kubernetes-maven-plugin to use JIB mode as well:

$ ./mvnw k8s:build -Djkube.build.strategy=jib

After running this command you’ll see kubernetes-maven-plugin created a container image with opinionated defaults by inspecting your project dependencies. However, you would want to configure the name of the container image.

Let’s say you want to push your image to Quay.io with username foo. You can configure image name by running this command:

$ ./mvnw k8s:build -Djkube.generator.name="quay.io/foo/%a:%l"

Once you’ve created a container image of your application, you need to push it to some container registry. Make sure you’ve already created account on some public/private container registry. You can provide your credentials either via environment variables or plugin configuration. kubernetes-maven-plugin also tries to read ~/.docker/config.json that gets created after docker login. You can read more about this in Authentication section.

Log into your container registry:

$ docker login quay.io

Run this command to instruct kubernetes-maven-plugin to push container image you built in previous step to your container registry:

Generating & applying Kubernetes manifests

Just like container image, kubernetes-maven-plugin can generate opinionated Kubernetes manifests. Run this command to automatically generate and apply manifests onto currently logged in Kubernetes cluster.

$ ./mvnw k8s:resource k8s:apply

After running these goals, you can also check Kubernetes manifests generated by kubernetes-maven-plugin in target/classes/META-INF/jkube/ directory.

Cleanup applied Kubernetes resources after testing:

$ ./mvnw k8s:undeploy
(Video) Containerize and deploy into Kubernetes your Gradle Java project with Eclipse JKube

2.3. Spring Boot

kubernetes-maven-plugin works with any Spring Boot project without any configuration. It automatically detects your project dependencies and generated opinionated container image and Kubernetes manifests.

Adding kubernetes-maven-plugin to project

You would need to add kubernetes-maven-plugin to your project in order to use it:

Open the pom.xml file and add the plugin in the <plugins> section.

<plugin> <groupId>org.eclipse.jkube</groupId> <artifactId>kubernetes-maven-plugin</artifactId> <version>1.8.0</version></plugin>
(Video) Deploy your Java applications to the Cloud using Eclipse JKube

Building and pushing image to Container registry

Once you’ve set up your project and tested it, You can create a container image for your application using kubernetes-maven-plugin. If you’ve access to a docker daemon try running this command:

$ ./mvnw k8s:build

If you don’t have access to any docker daemon, you can configure kubernetes-maven-plugin to use JIB mode as well:

$ ./mvnw k8s:build -Djkube.build.strategy=jib

After running this command you’ll see kubernetes-maven-plugin created a container image with opinionated defaults by inspecting your project dependencies. However, you would want to configure the name of the container image.

Let’s say you want to push your image to Quay.io with username foo. You can configure image name by running this command:

$ ./mvnw k8s:build -Djkube.generator.name="quay.io/foo/%a:%l"

Once you’ve created a container image of your application, you need to push it to some container registry. Make sure you’ve already created account on some public/private container registry. You can provide your credentials either via environment variables or plugin configuration. kubernetes-maven-plugin also tries to read ~/.docker/config.json that gets created after docker login. You can read more about this in Authentication section.

Log into your container registry:

$ docker login quay.io

Run this command to instruct kubernetes-maven-plugin to push container image you built in previous step to your container registry:

$ ./mvnw k8s:push -Djkube.generator.name="quay.io/foo/%a:%l"

Generating & applying Kubernetes manifests

Just like container image, kubernetes-maven-plugin can generate opinionated Kubernetes manifests. Run this command to automatically generate and apply manifests onto currently logged in Kubernetes cluster.

$ ./mvnw k8s:resource k8s:apply

After running these goals, you can also check Kubernetes manifests generated by kubernetes-maven-plugin in target/classes/META-INF/jkube/ directory.

Cleanup applied Kubernetes resources after testing:

$ ./mvnw k8s:undeploy

How to add a liveness and readiness probe?

kubernetes-maven-plugin automatically adds Kubernetes liveness and readiness probes in generated Kubernetes manifests in presence of Spring Boot Actuator dependency.

To add actuator to your project, add the following dependency:

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency></dependencies>

Once you run k8s:resource goal again, you should be able to see liveness and readiness probes added in generated manifests.

2.4. Vert.x

You can easily get started with using kubernetes-maven-plugin on an Eclipse Vert.x without providing any explicit configuration. kubernetes-maven-plugin would generate an opinionated container image and manifests by inspecting your project configuration.

Adding kubernetes-maven-plugin to project

You would need to add kubernetes-maven-plugin to your project in order to use it:

Open the pom.xml file and add the plugin in the <plugins> section.

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

Building and pushing image to Container registry

Once you’ve set up your project and tested it, You can create a container image for your application using kubernetes-maven-plugin. If you’ve access to a docker daemon try running this command:

$ ./mvnw k8s:build

If you don’t have access to any docker daemon, you can configure kubernetes-maven-plugin to use JIB mode as well:

$ ./mvnw k8s:build -Djkube.build.strategy=jib
(Video) Local Java development on top Of Kubernetes JKube

After running this command you’ll see kubernetes-maven-plugin created a container image with opinionated defaults by inspecting your project dependencies. However, you would want to configure the name of the container image.

Let’s say you want to push your image to Quay.io with username foo. You can configure image name by running this command:

$ ./mvnw k8s:build -Djkube.generator.name="quay.io/foo/%a:%l"

Once you’ve created a container image of your application, you need to push it to some container registry. Make sure you’ve already created account on some public/private container registry. You can provide your credentials either via environment variables or plugin configuration. kubernetes-maven-plugin also tries to read ~/.docker/config.json that gets created after docker login. You can read more about this in Authentication section.

Log into your container registry:

$ docker login quay.io

Run this command to instruct kubernetes-maven-plugin to push container image you built in previous step to your container registry:

$ ./mvnw k8s:push -Djkube.generator.name="quay.io/foo/%a:%l"

Generating & applying Kubernetes manifests

Just like container image, kubernetes-maven-plugin can generate opinionated Kubernetes manifests. Run this command to automatically generate and apply manifests onto currently logged in Kubernetes cluster.

After running these goals, you can also check Kubernetes manifests generated by kubernetes-maven-plugin in target/classes/META-INF/jkube/ directory.

Cleanup applied Kubernetes resources after testing:

$ ./mvnw k8s:undeploy

How to set Service Port?

By default, in Vert.x applications, application port value is 8888. kubernetes-maven-plugin opinionated defaults use port 8080. If you want to change this, you’ll need to configure kubernetes-maven-plugin to generate image with desired port:

<configuration> <generator> <config> <vertx> <webPort>8080</webPort> </vertx> </config> </generator></configuration>

Once configured, you can go ahead and deploy application to Kubernetes.

How to add Kubernetes Readiness Liveness probes?

kubernetes-maven-plugin doesn’t add any Kubernetes liveness and readiness probes by default. However, it does provide a rich set of configuration options to add health checks. Read Vert.x Healthchecks section for more details.

2.5. Quarkus

You can easily get started with using kubernetes-maven-plugin on a Quarkus project without providing any explicit configuration. kubernetes-maven-plugin would generate an opinionated container image and manifests by inspecting your project configuration.

Zero Configuration

Adding kubernetes-maven-plugin to project

You would need to add kubernetes-maven-plugin to your project in order to use it:

Open the pom.xml file and add the plugin in the <plugins> section.

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

Building and pushing image to Container registry

Once you’ve set up your project and tested it, You can create a container image for your application using kubernetes-maven-plugin. If you’ve access to a docker daemon try running this command:

$ ./mvnw k8s:build

If you don’t have access to any docker daemon, you can configure kubernetes-maven-plugin to use JIB mode as well:

$ ./mvnw k8s:build -Djkube.build.strategy=jib

After running this command you’ll see kubernetes-maven-plugin created a container image with opinionated defaults by inspecting your project dependencies. However, you would want to configure the name of the container image.

Let’s say you want to push your image to Quay.io with username foo. You can configure image name by running this command:

$ ./mvnw k8s:build -Djkube.generator.name="quay.io/foo/%a:%l"
(Video) Java Development on Kubernetes using Eclipse JKube

Once you’ve created a container image of your application, you need to push it to some container registry. Make sure you’ve already created account on some public/private container registry. You can provide your credentials either via environment variables or plugin configuration. kubernetes-maven-plugin also tries to read ~/.docker/config.json that gets created after docker login. You can read more about this in Authentication section.

Log into your container registry:

$ docker login quay.io

Run this command to instruct kubernetes-maven-plugin to push container image you built in previous step to your container registry:

$ ./mvnw k8s:push -Djkube.generator.name="quay.io/foo/%a:%l"

Generating & applying Kubernetes manifests

Just like container image, kubernetes-maven-plugin can generate opinionated Kubernetes manifests. Run this command to automatically generate and apply manifests onto currently logged in Kubernetes cluster.

$ ./mvnw k8s:resource k8s:apply

After running these goals, you can also check Kubernetes manifests generated by kubernetes-maven-plugin in target/classes/META-INF/jkube/ directory.

Cleanup applied Kubernetes resources after testing:

$ ./mvnw k8s:undeploy

Quarkus Native Mode

While containerizing a Quarkus application under native mode, kubernetes-maven-plugin would automatically detect that it’s a native executable artifact and would select a lighter base image while containerizing application. There is no additional configuration needed by kubernetes-maven-plugin for Native Builds.

How to add Kubernetes liveness and readiness probes?

kubernetes-maven-plugin automatically adds Kubernetes liveness and readiness probes in generated Kubernetes manifests in presence of SmallRye Health dependency.

To add SmallRye to your project, add the following dependency:

<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-smallrye-health</artifactId></dependency>

Once you run k8s:resource goal again, you should be able to see liveness and readiness probes added in generated manifests.

2.6. Dockerfile

You can build a container image and deploy to Kubernetes with kubernetes-maven-plugin by just providing a Dockerfile.kubernetes-maven-plugin builds a container image based on your Dockerfile and generatesopinionated Kubernetes manifests by inspecting it.

Placing Dockerfile in project root directory

You can place the Dockerfile in the project root directory along with pom.xml.

kubernetes-maven-plugin detects it and automatically builds an image based on this Dockerfile.There is no need to provide any sort of configuration apart from Dockerfile and project root directory as docker context directory.The Image is created with an opinionated name from group, artifact and version.The name can be overridden by using the jkube.image.name property.Read Simple Dockerfile section for more details.

Placing Dockerfile in some other directory

You can choose to place your Dockerfile at some other location.By default, the plugin assumes it to be src/main/docker, but you’ll need to configure docker context directory in plugin configuration.When not specified, context directory is assumed to be Dockerfile’s parent directory.You can take a look at Docker File Provided Quickstarts for more details.

Controlling what gets copied to image

When using Dockerfile mode, every file and directory present in the Docker build context directorygets copied to the created Docker image.In case you want to ignore some files, or you want to include only a specific set of files,the kubernetes-maven-plugin provides the following options to achieve this:

  • .jkube-dockerinclude : Include only a specific set of files specified in this file

  • .jkube-dockerexclude : Exclude a certain set of files from being copied into container image

  • .jkube-dockerignore : Same as .jkube-dockerexclude, ignore certain files from being copied into container image

Using Property placeholders in Dockerfiles

You can reference properties in your Dockerfiles using standard maven property placeholders ${*}. For example, if you have a property in your gradle.properties like this:

gradle.properties

fromImage = fabric8/s2i-java

Dockerfile

FROM ${fromImage}:latest-java11
(Video) Sprint 219 - JKube Fragments for multiple environments

You can override placeholders using the filter field in image build configuration, see Build Filtering for more details.

Videos

1. Deploy your Java applications onto Kubernetes using Fabric8 Maven Plugin (Part 3)
(fabric8)
2. Sprint 201 - Generate Helm Charts with dependencies for a Spring application
(Eclipse JKube)
3. Containeriza tus microservicios facilmente con Eclipse JKube
(Barcelona Jug)
4. Sprint 199 - Generate and Publish Helm Charts for a Spring application
(Eclipse JKube)
5. Sprint 206 - OpenShift Gradle Plugin: ocBuild
(Eclipse JKube)
6. Accessing Kubernetes API from within a Pod using Fabric8 Kubernetes Java Client
(fabric8)

You might also like

Latest Posts

Article information

Author: Kerri Lueilwitz

Last Updated: 05/06/2022

Views: 6338

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.