Developing Serverless Applications With Quarkus
Learn more about developing serverless applications with Quarkus.
Join the DZone community and get the full member experience.Join For Free
in the first part of this article, i will explain both quarkus and knative. in the second part, i describe how to write simple microservices with quarkus in visual studio code in just a few minutes. and finally, in the last part of this post, i walk you through how to deploy and run microservices as serverless applications on kubernetes via knative.
let's get started!
you may also like: thoughts on quarkus
quarkus is an open-source project led by red hat and a java framework used to build microservices, which often require little memory and start very fast. in other words, quarkus is a great technology for developing efficient containers.
from the outset, quarkus has been designed around a container-first philosophy. what this means, in real terms, is that quarkus is optimized for low memory usage and fast startup.
quarkus is not a full jakarta ee application server but comes with a lot of similar functionality. the big difference is that quarkus optimizes for container workloads by doing as much processing as possible at build-time rather than at run-time. this means that functionality like reflection is not supported.
quarkus comes in two flavors. you can run it with classic jvms like hotspot or you can translate the java code in binary code via graalvm — when using binary code memory usage is even less and startup time is even shorter. here are the measurements from the quarkus homepage.
in this article, i focus on classic jvms. rather than using hotspot, i’ve used openj9 , which requires only roughly half of the memory compared to hotspot. in my little test, my simple microservice with a rest endpoint required only 34 mb, which is similar to the node.js node:8-alpine image. until recently, serverless applications were primarily developed with less resource-intensive technologies like node.js.
i think that, with quarkus, more enterprise developers will leverage their existing java skills to build container workloads, for example, serverless applications. in the cloud where you pay by 1. memory usage and 2. durations the code runs, frameworks like quarkus make a huge difference.
knative is an open-source project initiated by google with contributions from over 50 companies, for example, red hat and ibm. knative allows running containers on kubernetes in a serverless fashion so that containers only run when actually needed.
run serverless containers on kubernetes with ease. knative takes care of the details of networking, autoscaling (even to zero), and revision tracking. you just have to focus on your core logic.
i think knative’s key functionality is that it supports ‘scale to zero’. when containers are not needed anymore, they are shut down automatically so that they don’t consume compute resources. in other words, you can run more containers in a cluster, just not all of them at the same time.
the big challenge for all serverless platforms is how to handle ‘cold starts’ of containers. the first time endpoints of containers are invoked, the containers need to be started first. since knative shuts down containers after a certain duration of inactivity, the same ‘cold starts’ occur the next time endpoints are invoked.
when using knative, the container with the microservice is not the only container in the pod. additionally, an istio proxy and a knative queue proxy are running. a fourth istio container initiates the pod. these other three containers need to be started in addition to the microservice container. even though quarkus containers start in less than a second, the overall startup time of the pod is much longer. in my little test, it took 16 seconds to get responses from my quarkus container. so until there are better ways to handle the cold starts, knative shouldn’t be used for all kinds of workloads.
i think the best usage of knative are scenarios where a lot of compute is required infrequently and responses in a few milliseconds are not required. a good example is the processing of large data sets of massive scale, which can be handled with serverless technologies running in parallel.
development of microservices with quarkus
after the introduction of quarkus and knative, let’s now move on and take a closer look at how these technologies can be used. in this part, i describe how to build your first microservice with quarkus.
i think quarkus provides an awesome developer experience. there is an extension for visual studio code to create projects very easily. quarkus also supports a development mode for hot code replace and built-in debugging functionality.
to create a new project open the palette (on mac via ⇧⌘p) and choose “>quarkus: generate a maven project”.
when you accept all defaults, a simple project is created with a greeting resource.
after this, you can open a terminal and invoke “./mvnw compile quarkus:dev” to use the development mode. the endpoint will be accessible via the url “http://localhost:8080/hello”. try out the hot replace functionality! very nice.
additionally, the vs code extension supports debugging:
this screenshot shows the debugger in action:
building quarkus images
before microservices can be deployed to kubernetes, the java applications and the docker images have to be built.
when running maven via “ ./mvnw package ”, two jar files are created in the /target directory.
- getting-started-1.0-snapshot.jar : classes and resources of the projects
- getting-started-1.0-snapshot-runner.jar : executable jar. dependencies are needed additionally (in /target/lib)
the vs code extension also creates a dockerfile to run the quarkus application with hotspot. however, i’ve used openj9 since it consumes only half of the memory . to use openj9, create the file ‘ src/main/docker/dockerfile.jvm-j9 ’ with the following content.
from adoptopenjdk:8-jre-openj9 run mkdir /opt/app copy target/lib/* /opt/app/lib/ copy target/*-runner.jar /opt/app/app.jar cmd ["java", "-jar", "/opt/app/app.jar"]
to build the images and to run the container locally, run the following commands.
$ docker build -f src/main/docker/dockerfile.jvm-j9 -t quarkus/quarkus-getting-started-jvm-j9 . $ docker run -i --rm -p 8080:8080 quarkus/quarkus-getting-started-jvm-j9 $ curl http://localhost:8080/hello
the quarkus application starts in around 0.7 seconds.
the application requires 32 mb of memory.
next, push the image to dockerhub. replace ‘nheidloff’ with your dockerhub name.
docker tag quarkus/quarkus-getting-started-jvm-j9 nheidloff/quarkus-getting-started-jvm-j9 docker login docker push nheidloff/quarkus-getting-started-jvm-j9
deploying quarkus images as serverless applications
in order to deploy quarkus microservices as a serverless application, you need to install knative on kubernetes. one option is to do this locally with minikube.
before deploying the application, log in to the ibm cloud.
$ ibmcloud login -a cloud.ibm.com -r eu-de -g default $ ibmcloud ks cluster config --cluster <cluster-name-or-id> $ export kubeconfig=<file-from-previous-command> $ kubectl get pods
one option to deploy the application is to use yaml files with the custom resources provided by knative. another option is to use the knative cli (command-line interface)
kn . in that case, you only have to invoke one command.
$ kn service create quarkus-getting-started --image nheidloff/quarkus-getting-started-jvm-j9 $ kn service list
the screenshot shows the three containers including the quarkus container running in the same pod. invocations of the greeting endpoint take between 0.06 and 0.2 seconds dependent on the network.
to find out memory usage, invoke the following command:
$ kubectl get pods $ kubectl top pod quarkus-getting-started-<your-pod-id> --containers
the quarkus container with openj9 consumes 34 mb memory. the first time the endpoint was invoked (cold start) took 17 seconds, after this only 0.7 seconds.
i hope you enjoyed this article! to find out more about these technologies, check out the following resources.
Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.