DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Auto-Scaling a Spring Boot Native App With Nomad
  • How to Use Shipa to Make Kubernetes Adoption Easier for Developers
  • Tips for Managing Multi-Cluster Kubernetes Deployment With High Efficiencies
  • Deploy an ASP.NET Core Application in the IBM Cloud Code Engine

Trending

  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • Performing and Managing Incremental Backups Using pg_basebackup in PostgreSQL 17
  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Containerize Gradle Apps and Deploy to Kubernetes With JKube Kubernetes Gradle Plugin

Containerize Gradle Apps and Deploy to Kubernetes With JKube Kubernetes Gradle Plugin

Eclipse JKube now supports Gradle!

By 
Rohan Kumar user avatar
Rohan Kumar
·
Jan. 16, 22 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
10.0K Views

Join the DZone community and get the full member experience.

Join For Free

This article is a follow-up to my previous post, Deploy Maven Apps to Kubernetes With JKube Kubernetes Maven Plugin.

Eclipse JKube is a collection of tools and libraries which ease a Java developer's workflow working with Docker and Kubernetes. It is the successor to the famous Fabric8 Maven Plugin.

Eclipse JKube brings a set of tools focused on bridging the gap between traditional Java development and modern cloud-native development. Till v1.4.0, Eclipse JKube only supported Maven Plugins; but recently their team also has added Gradle Plugins.

We will be taking a look at Eclipse JKube Kubernetes Gradle Plugin to see how it provides a similar developer experience while working with Docker and Kubernetes like you usually have with your popular application frameworks:

Setting Up Your Gradle Project

If you already have access to a grade, project you can skip this step. If not, you can create a Spring Boot-based project from start.spring.io or use one of the Eclipse JKube Gradle Quickstarts.

You can find latest version of Eclipse JKube on Maven Central. You'll need to specify mavenCentral() in your settings.gradle and build.gradle to tell gradle to look into maven central repositories while searching for plugin:

settings.gradle:

Groovy
 
pluginManagement {
    repositories {
        gradlePluginPortal()
        mavenCentral()
    }
}

build.gradle:

Groovy
 
repositories {
    mavenCentral()
}


To add Eclipse JKube Kubernetes Gradle Plugin to your project, you can simply add it to your build.gradle like this:

Groovy
 
plugins {
    id 'org.eclipse.jkube.kubernetes' version '1.5.1'
}

Let's take a look at tasks available with Kubernetes Gradle Plugin:


Task Name Description
k8sBuild Build Container Images
k8sResource Generate Kubernetes Manifests
k8sApply Apply generated Manifests to Kubernetes
k8sPush Push Container Image to container registry
k8sHelm Generate Helm Charts
k8sDebug Debug application in Kubernetes Cluster
k8sLog Get Logs of the application running in Kubernetes
k8sUndeploy Cleanup: Delete all resources deployed in Kubernetes


1. Containerizing Your Application

Kubernetes Gradle Plugin makes containerizing your Gradle 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 Groovy DSL or by providing your own Dockerfile.

To build Docker image of your application, just run the k8sBuild task (Default strategy for building images Is Docker so It requires access to a docker daemon):

Shell
 
gradle k8sBuild

This will create a Docker Image in the daemon you performed this task 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:

Shell
 
gradle k8sBuild -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 Container Registry

Once you’ve built an image, you might want to push it to a remote registry like DockerHub or Quay. You can easily achieve this with Kubernetes Gradle Plugin. Before doing it, you need to make sure that the image you’ve built is in the correct ${registry}/${user}/${image-name}:${tag} format. If you’re providing your own Groovy DSL Configuration for building image, you can simply change the image name by changing the name section like this: 

Groovy
 
kubernetes {
  images {
    name = 'quay.io/rohankanojia/helloapp:1.0.0'
    alias = 'hello-world'
    build {
      from = 'openjdk:latest'
      cmd {
        exec = ["java", "-jar", "/deployments/${project.name}-${project.version}.jar"]
      }
    }
  }
}

If you’re using Kubernetes Gradle Plugin’s Zero Configuration, you need to change default image name generated by Kubernetes Gradle Plugin by providing this property:

Properties files
 
jkube.generator.name = quay.io/rohankanojia/helloapp:1.0.0

For registry authentication, Kubernetes Gradle Plugin automatically detects credentials from these locations:

  • ~/.docker/config.json(docker login config file)
  • In auth section in plugin configuration

You can check these in detail in Kubernetes Gradle Plugin Authentication Docs.

In order to push image to specified registry, you can go ahead and use k8sPush task:

Shell
 
gradle k8sPush

Or if you had built your image with JIB build strategy, you can do:

Shell
 
gradle k8sPush  -Djkube.build.strategy=jib

3. Generate Kubernetes Manifests

Kubernetes Gradle 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 k8sResource task:

Shell
 
gradle k8sResource

This would generate Kubernetes manifests which would be available in build/ directory, as shown in listing below:

Shell
 
$ tree build/classes/java/main/META-INF/jkube/
build/classes/java/main/META-INF/jkube/
├── kubernetes
│   ├── eclipse-jkube-demo-project-gradle-deployment.yml
│   └── eclipse-jkube-demo-project-gradle-service.yml
└── kubernetes.yml

1 directory, 3 files

4. Deploying To Kubernetes

You can use Kubernetes Gradle Plugin’s apply task 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.

Then you can deploy your application in just one step using k8sApply task:

Shell
 
gradle k8sApply

To remove your application from Kubernetes, just use the k8sUndeploy task:

Shell
 
gradle k8sUndeploy

5. Inspect Logs

Once your application has been deployed into Kubernetes using Kubernetes Gradle Plugin; you might want to inspect its logs. You can use k8sLog task which would output logs for your application running inside Kubernetes:

 
gradle k8sLog

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:

Shell
 
gradle k8sLog  -Djkube.log.follow=false

6. Live Debugging App Running Inside Kubernetes

To debug your application deployed using the k8sApply task, just run the k8sDebug task:

Shell
 
gradle k8sDebug

This will use the default Java remote debugging port of 5005. You can also specify a different port if you want:

Shell
 
gradle k8sDebug -Djkube.debug.port=8000

Once task 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:

7. Generating Helm Charts

If you're familiar with helm, Eclipse JKube also provides support for generating and publishing helm charts. You can generate helm charts using the k8sHelm task provided by Kubernetes Gradle Plugin. This helm chart would include all the resources generated by k8sResource task.

Shell
 
gradle k8sHelm

Note that it's required to run k8sResource before running this task.

After executing the task, you can apply this helm chart via helm CLI like this:

Shell
 
$ helm install build/jkube/helm/eclipse-jkube-demo-project-gradle/kubernetes/ --generate-name
NAME: kubernetes-1635508429
LAST DEPLOYED: Fri Oct 29 17:23:49 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
$ helm list
NAME                 	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART                                           	APP VERSION
kubernetes-1635508429	default  	1       	2021-10-29 17:23:49.888358544 +0530 IST	deployed	eclipse-jkube-demo-project-gradle-0.0.1-SNAPSHOT	 

Conclusion

Please try out the Kubernetes Gradle 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.


Kubernetes Gradle Docker (software) app application Task (computing) shell Eclipse Manifest (transportation) Groovy (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Auto-Scaling a Spring Boot Native App With Nomad
  • How to Use Shipa to Make Kubernetes Adoption Easier for Developers
  • Tips for Managing Multi-Cluster Kubernetes Deployment With High Efficiencies
  • Deploy an ASP.NET Core Application in the IBM Cloud Code Engine

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!