So, you wrote your Quarkus application, and now you want to deploy it to a Kubernetes cluster. Good news: Deploying a Quarkus application to a Kubernetes cluster is easy. Before you do this, though, you need to straighten out your application's properties. After all, your app probably has to connect with a database, call other services, and so on. These settings are already defined in your application.properties
file, but the values match the ones for your local environment and won't work once deployed onto your cluster.
So, how do you easily solve this problem? Let's walk through an example.
Create the example Quarkus application
Instead of using a complex example, let's take a simple use case that explains the concept well. Start by creating a new Quarkus app:
$ mvn io.quarkus:quarkus-maven-plugin:1.1.1.Final:create
You can keep all of the default values while creating the new application. In this example, the application is named hello-app
. Now, open the HelloResource.java
file and refactor it to look like this:
@Path("/hello") public class HelloResource { @ConfigProperty(name = "greeting.message") String message; @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return "hello " + message; } }
In your application.properties
file, now add greeting.message=localhost
. The @ConfigProperty
annotation is not in the scope of this article, but here we can see how easy it is to inject properties inside our code using this annotation.
Now, let's start our application to see if it works as expected:
$ mvn compile quarkus:dev
Browse to http://localhost:8080/hello
, which should output hello localhost
. That's it for the Quarkus app. It's ready to go.
Deploy the application to the Kubernetes cluster
The idea here is to deploy this application to our Kubernetes cluster and replace the value of our greeting
property with one that will work on the cluster. It is important to know here that all of the properties from application.properties
are exposed, and thus can be overridden with environment variables. The convention is to convert the name of the property to uppercase and replace every dot (.
) with an underscore (_
). So, for instance, our greeting.message
will become GREETING_MESSAGE
.
At this point, we are almost ready to deploy our app to Kubernetes, but we need to do three more things:
- Create a Docker image of your application and push it to a repository that your cluster can access.
- Define a ConfgMap resource.
- Generate the Kubernetes resources for our application.
To create the Docker image, simply execute this command:
$ docker build -f src/main/docker/Dockerfile.jvm -t quarkus/hello-app .
Be sure to set the right Docker username and to also push to an image registry, like docker-hub
or quay
. If you are not able to push an image, you can use sebi2706/hello-app:latest
.
Next, create the file config-hello.yml
:
apiVersion: v1 data: greeting: "Kubernetes" kind: ConfigMap metadata: name: hello-config
Make sure that you are connected to a cluster and apply this file:
$ kubectl apply -f config-hello.yml
Quarkus comes with a useful extension, quarkus-kubernetes
, that generates the Kubernetes resources for you. You can even tweak the generated resources by providing extra properties—for more details, check out this guide.
After installing the extension, add these properties to our application.properties
file so it generates extra configuration arguments for our containers specification:
kubernetes.group=yourDockerUsername kubernetes.env-vars[0].name=GREETING_MESSAGE kubernetes.env-vars[0].value=greeting kubernetes.env-vars[0].configmap=hello-config
Run mvn package
and view the generated resources in target/kubernetes
. The interesting part is in spec.containers.env
:
- name: "GREETING_MESSAGE" valueFrom: configMapKeyRef: key: "greeting" name: "hello-config"
Here, we see how to pass an environment variable to our container with a value coming from a ConfigMap. Now, simply apply the resources:
$ kubectl apply -f target/kubernetes/kubernetes.yml
Expose your service:
kubectl expose deployment hello --type=NodePort
Then, browse to the public URL or do a curl. For instance, with Minikube:
$ curl $(minikube service hello-app --url)/hello
This command should output: hello Kubernetes
.
Conclusion
Now you know how to use a ConfigMap in combination with environment variables and your Quarkus's application.properties
. As we said in the introduction, this technique is particularly useful when defining a DB connection's URL (like QUARKUS_DATASOURCE_URL
) or when using the quarkus-rest-client
(ORG_SEBI_OTHERSERVICE_MP_REST_URL
).