A logo showing the text blog.marcnuri.com
Español
Home»Java»Develop cloud native Kubernetes Java applications on Okteto Cloud

Recent Posts

  • Fabric8 Kubernetes Client 6.5.0 is now available!
  • Eclipse JKube 1.11 is now available!
  • Fabric8 Kubernetes Client 6.4.1 is now available!
  • I bought an iPad
  • Three years at Red Hat

Categories

  • Front-end
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • December 2015
  • November 2015
  • November 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

Develop cloud native Kubernetes Java applications on Okteto Cloud

2022-01-29 in Java tagged Client / Cloud / Java / JKube / Kubernetes / OpenShift by Marc Nuri | Last updated: 2022-02-03

Introduction

In this post, I'll show you how to set up your Spring Boot Java project for inner loop development on top of Okteto Cloud Kubernetes. For this purpose, I'll be using Eclipse JKube's Kubernetes Maven Plugin to create the container image and deploy it to our development environment.

Requirements

The first and most important requirement is to have an Okteto account. If you have a GitHub account, this is as easy as logging in to cloud.okteto.com using GitHub. What I like best about this platform is that it doesn't require a Credit Card to get started.

Kubernetes Access

To be able to deploy to our development cluster, we'll need to configure the access credentials. Okteto provides a very simple mechanism for this. You just need to download the customized .kube/config file, and set up your current console environment:

MacOS/Linux

export KUBECONFIG=$HOME/Downloads/okteto-kube.config:${KUBECONFIG:-$HOME/.kube/config}

Windows

$Env:KUBECONFIG=("$HOME\Downloads\okteto-kube.config;$Env:KUBECONFIG;$HOME\.kube\config")

If you have logged in to your Okteto account, you can download the file from your dashboard.

Okteto Cloud Dashboard - Download Kube Config file
Okteto Cloud Dashboard - Download Kube Config file

Container Registry

Okteto has its own container registry. This allows every Okteto namespace to have a private registry to store its container images. Since we are setting up the project for the inner loop, it's interesting to make use of this feature.

The easiest way to authenticate without persisting the credentials in the project or the environment is by using the docker cli to log in to the registry.

docker login registry.cloud.okteto.net

The username is the email associated with your Okteto account, or your GitHub username.

For the password, you'll need to generate an Okteto Personal Access Token which you can generate from your dashboard.

Okteto Cloud Dashboard - Personal Access Token
Okteto Cloud Dashboard - Personal Access Token

Project setup

We can now easily configure our project to be Okteto friendly.

Kubernetes Maven Plugin

If your project is not using Kubernetes Maven Plugin, or even if it's already using it, you will need to add the following configuration to the plugin section:

1<plugin>
2  <groupId>org.eclipse.jkube</groupId>
3  <artifactId>kubernetes-maven-plugin</artifactId>
4  <version>1.11.0</version>
5  <configuration>
6    <resources>
7      <annotations>
8        <service>
9          <property>
10            <name>dev.okteto.com/auto-ingress</name>
11            <value>true</value>
12          </property>
13        </service>
14      </annotations>
15      <imagePullPolicy>Always</imagePullPolicy>
16    </resources>
17  </configuration>

In line 15, we are specifying an Image Pull Policy of Always. Since we are developing for the inner loop, and the project has a -SNAPSHOT version, JKube automatically tags the generated container image with latest. This configuration forces Okteto/Kubernetes to always pull a fresh version of the container image. This way, any changes you perform during your iterative development loop, will be reflected in the deployed version of the application.

In lines 7-14 we are adding an Okteto specific annotation to the service.

Following are the required changes to the properties section:

1<properties>
2  <jkube.generator.name>
3    registry.cloud.okteto.net/your-namespace/jkube-okteto-demo
4  </jkube.generator.name>
5</properties>

To be able to use Okteto's container registry, we'll need to setup a specific name for our image. In the project properties, we need to set an image name for the registry.cloud.okteto.net registry, and our namespace. You need to replace your-namespace, with the namespace for your user.

Deploying the application

We are now ready to deploy the application. Provided that you have set up your environment as specified in the Kubernetes Access section, you can now deploy the application by running:

mvn clean package k8s:build k8s:push k8s:resource k8s:apply

Your deployment should now be visible from your dashboard:

Okteto Cloud Dashboard - JKube Okteto deployment
Okteto Cloud Dashboard - JKube Okteto deployment

Once the Pod is started, we should be able to cURL the exposed endpoint:

$ curl https://jkube-okteto-your-namespace.cloud.okteto.net/
Hello Okteto!

Since we are using JKube, all of the developer specific goals such as debugging (k8s:debug), retrieving the logs (k8s:log), etc. should be available.

Conclusion

In this article, I've shown you how to set up a Java Maven project for inner loop development on Okteto Cloud Kubernetes with Eclipse JKube's Kubernetes Maven Plugin. Being able to develop and test your cloud native application in a remote cluster is fundamental these days. As you can see, JKube provides a very easy way to achieve this.

You can find the full source code for this post at GitHub.

References

  • Okteto Cloud Documentation: Download your Kubernetes credentials
  • Okteto Cloud Documentation: Okteto Registry
  • Kubernetes Maven Plugin documentation
Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation

← Eclipse JKube 1.6.0 is now available!How to check if an array contains duplicate values in JavaScript? →
© 2007 - 2023 Marc Nuri