Kubernetes is an open-source project for automating deployment, scaling, and management of containers. It has rapidly become the standard to run production workloads and the community around it is just great!

But developing in Kubernetes presents some challenges. The typical development workflow looks like this: write code, build a Docker image, push it to the registry, redeploy, validate your changes, rinse and repeat. This flow is not only slow, but it also prevents us from benefiting from standard features of Java tools such as fast incremental builds, automatic hot reloads or powerful debuggers.

Okteto was created to solve this problem. On this blog post, we will show you how Okteto improves the developer experience in Kubernetes for Java developers. You will be able to take full advantage of tools like Maven, Gradle, dependency caching, popular frameworks hot reloads (Spring Boot, Quarkus, Micronaut, …) or IDE debuggers (Eclipse, IntelliJ, VS Code, …) while developing your application directly in Kubernetes.

Step 1: Deploy the Java Sample App

Get a local version of the Java Sample App by executing the following commands:

Maven 1

2

git clone https://github.com/okteto/java-maven-getting-started

cd java-maven-getting-started



Gradle 1

2

git clone https://github.com/okteto/java-gradle-getting-started

cd java-gradle-getting-started



The k8s.yml file contains the Kubernetes manifests to deploy the Java Sample App. Run the application by executing:

You can deploy to your own Kubernetes cluster or give Okteto Cloud a try. Okteto Cloud is a development platform for Kubernetes applications. Sign up today to get a free developer account with 4 CPUs and 8GB of RAM.

1

kubectl apply -f k8s.yml



1

2

deployment.apps "hello-world" created

service "hello-world" created



This is cool! You typed one command and a dev version of your application just runs 😎.

Step 2: Install the Okteto CLI

The Okteto CLI is an open-source project that lets you develop your applications directly in Kubernetes while taking advantage of well-known local tooling . We will use it to speed up our development cycle instead of using the typical development workflow based on building docker images and redeploying containers.

Install the Okteto CLI:

MacOS / Linux 1

curl https://get.okteto.com -sSfL | sh



Windows Download https://downloads.okteto.com/cli/okteto.exe and add it to your `$PATH`.

Step 3: Activate your development container

With the Java Sample Application deployed, run the following command:

1

okteto up



1

2

3

4

5

6

7

8

✓ Development container activated

✓ Files synchronized

Namespace: pchico83

Name: hello-world

Forward: 8080 -> 8080

8088 -> 8088



...



The okteto up command starts a development container, which means:

The Java Sample App container is updated with the Docker image okteto/hello-world:java-maven-dev or okteto/hello-world:java-gradle-dev , depending on the git repo you have cloned. This image contains the required dev tools to build, test, debug and run a Java application. Check the maven and gradle Dockerfiles to see how they are generated.

or , depending on the git repo you have cloned. This image contains the required dev tools to build, test, debug and run a Java application. Check the maven and gradle Dockerfiles to see how they are generated. A file synchronization service is created to keep your changes up-to-date between your local filesystem and your development container.

A volume is attached to persist the Maven/Gradle cache in your development container.

Container ports 8080 (the application) and 8088 (the debugger) are forwarded to localhost.

All of this (and more) can be customized via the okteto.yml manifest file:

Maven 1

2

3

4

5

6

7

name: hello-world

image: okteto/hello-world:java-maven-dev

command: ["mvn", "spring-boot:run" ]

workdir: /code

forward:

- 8080 :8080

- 8088 :8088



Gradle 1

2

3

4

5

6

7

name: hello-world

image: okteto/hello-world:java-gradle-dev

command: [ "gradle" , "bootRun" ]

workdir: /code

forward:

- 8080 :8080

- 8088 :8088



You can also use the file .stignore to skip files from file synchronization. This is useful to avoid synchronizing build artifacts or git metadata.

The first time you run the application, Maven/Gradle will compile your application. Wait for this process to finish.

Okteto automatically forwards port 8080 from your local computer to the development container, making it accessible via localhost . Test your application by running the command below in a local shell:

1

curl localhost:8080



1

Hello world!



Step 4: Develop directly in Kubernetes

Open src/main/java/com/okteto/helloworld/RestHelloWorld.java in your favorite local IDE and modify the response message on line 11 to be Hello world from the cluster!. Save your changes.

1

2

3

4

5

6

7

8

9

10

11

12

13

package com.okteto.helloworld;



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

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





public class RestHelloWorld {



( "/" )

public String sayHello () {

return "Hello world from the cluster!" ;

}

}



Your IDE will auto compile only the necessary *.class files that will be synchronized by Okteto to your application in Kubernetes. Take a look at the development container shell and notice how the changes are detected by Spring Boot and automatically hot reloaded. To enable Spring Boot hot reloads you need to import the spring-boot-devtools dependency in your application:

Maven 1

2

3

4

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

</dependency>



Gradle 1

2

3

4

5

dependencies {

...

dev("org.springframework.boot:spring-boot-devtools")

...

}



Call your application to validate the changes:

1

curl localhost:8080



1

Hello world from the cluster!



Cool! Your code changes were instantly applied to Kubernetes. No commit, build or push required 😎!

Step 5: Debug directly in Kubernetes

Okteto enables you to debug your applications directly from your favorite IDE. Let’s take a look at how that works in Eclipse, one of the most popular IDEs for Java development. To enable debug mode, define the following JVM arguments in your Gradle/Maven configuration files:

Maven 1

2

3

4

5

<configuration>

<jvmArguments>

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8088

</jvmArguments>

</configuration>



Gradle 1

2

3

4

bootRun {

jvmArgs = ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8088"]

...

}



Open the Debug configuration dialog, add a new Remote Java Application debug configuration, and point it to localhost:8088 :

Click the Debug button to start a debugging session. Add a breakpoint on src/main/java/es/okteto/helloworld/RestHelloWorld.java , line 11, and call your application by running the command below from your local shell.

1

curl localhost:8080



The execution will halt at your breakpoint. You can then inspect the request, the available variables, etc…

Conclusions

Kubernetes has the potential to be a great development platform, providing replicable, resource-efficient and production-like development environments. We have shown you how to use Okteto to create a development workflow that also lets you take advantage of features like incremental builds, debuggers or hot reloads while developing your application directly in Kubernetes.

Accelerate your development and start developing directly in Kubernetes today. Let us know what you think about it on Twitter, or in our #okteto channel in the Kubernetes community Slack.