Run a JAVA web application in 0.043 seconds!

At the end of this tutorial, you'll be able to run a quarkus native application in less than 0.043 seconds. On top of that, using skaffold, we'll have a continuous deployment. Meaning, you write that rest endpoint, save, and watch the results in a production environment. Cool right?


In my previous article i explained how i setup a kubernetes cluster on a laptop with only 8GB of ram. Benefiting from /etc/hosts as my local DNS server to create valid ingresses and access them from the browser. I hooked my micro data center to an Ethernet cable and left there ever since. I use it to test new tools or set of practices. You can have a similar environment too!
You can try the final project by typing:
$ docker run kyouuma/quarkus-native-development:demo 


It started in 0.008s! Quiet astonishing.
First things first, I will explain all the terms highlighted at the start of the tutorial.

QuarkusA next generation kubernetes native java framework.

Native ApplicationThis type of approach, targets the limits in the Java Virtual Machine - JVM. Java was built for a portability that now docker provides. So Redhat leverages a substitute VM. In this case GraalVM to take that generated jar and turn into a C++ Executable. You take that artifact, put it in an alpine container and execute it.Resulting in a longer build time and a faster runtime. Which is the main concern in k-native development. We need to go fast!

Skaffold:For me this is the cool part of the story. Easy and Repeatable Kubernetes Development using a command line tool. You connect it to your cluster, prepare a skaffold.yaml file to tell it what to do. You run your project locally, skaffold builds your project, push it to a docker registry, deploy it to kubernetes and keeps watching for any changes in your code to do the whole thing again in an endless cycle.

For this tutorial we'll use our private data center that we built in a previous article, Here and do things in the following order:

  1. Install sdkman.
  2. Install GraalVM Hotspot & Native Executable.
  3. Build & Run the application using skaffold. 

1. Install sdkman

 SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on most Unix based systems. I use it to manage multiple java, python and maven environments. You can switch from graalvm to adoptOpenJDK in a single command.

$ curl -s "https://get.sdkman.io" | bash
$ source "$HOME/.sdkman/bin/sdkman-init.sh"
$ sdk version 

2. Install GraalVM Hotspot & Native Executable.

 We'll use sdk manager to install graalvm. First we'll list the available java environments by typing:
$ sdk list java


For me i have three installed versions. The >>> points to the current installed version which is 19.3.1.r11-grl which is graalvm's java 8.
Now install and set graalVM by typing:
$ sdk install java 19.3.1.r11-grl 
$ java --version

Now install the native image generator using gu. A GraalVM command line utility
$ cd $USER/.sdkman/candidates/java/current/bin
$ ./gu install native-image

3. Build & Run the application using skaffold

As explained by google the creators: Skaffold is a command line tool that facilitates continuous development for Kubernetes applications. You can iterate on your application source code locally then deploy to local or remote Kubernetes clusters. Skaffold handles the workflow for building, pushing and deploying your application. It also provides building blocks and describe customizations for a CI/CD pipeline.
Clone the project repository from here. We'll have the following structure:
.
├── Dockerfile
├── k8s-resources.yaml
├── mvnw
├── mvnw.cmd
├── pom.xml
├── quarkus-native-development.iml
├── README.md
├── skaffold.yaml
└── src




So we have a Quarkus web application with no specific code in it. There's the project's Dockerfile, kubernetes resources [ a pod, service and an ingress ] and a special file called skafffold.yaml.
That's scaffold's configuration file, it's straightforward and self explanatory.
Now let's build a native executable, this will make the application supersonic:

$ mvn package -Pnative -Dquarkus.native.container-runtime=docker

The generate target/*-runner is our jar turned into an executable. Let's now do some magic:

$ skaffold dev


Notice the application's startup time 0.043s
Let me explain what happened. skaffold detected my Dockerfile, built the image, pushed it to my docker hub registry. Then created the resources in k8s-resources.yaml and started streaming the application logs.


That's really cool. If you change anything in the source code, it will automatically start the whole cycle all over again and you develop directly on the production cluster without worrying about anything but business logic.

Now to exit hit Ctrl+C and skaffold will delete the deployed resources gracefully.


I hope you find this article helpful. Till next time, stay safe

Comments

Popular Posts