{"id":107904,"date":"2020-01-23T08:00:10","date_gmt":"2020-01-23T08:00:10","guid":{"rendered":"https:\/\/developers.redhat.com\/blog\/?p=675247"},"modified":"2020-01-23T08:00:10","modified_gmt":"2020-01-23T08:00:10","slug":"using-kubernetes-configmaps-to-define-your-quarkus-applications-properties","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/01\/23\/using-kubernetes-configmaps-to-define-your-quarkus-applications-properties\/","title":{"rendered":"Using Kubernetes ConfigMaps to define your Quarkus application\u2019s properties"},"content":{"rendered":"<p>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&#8217;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 <code>application.properties<\/code> file, but the values match the ones for your local environment and won&#8217;t work once deployed onto your cluster.<\/p>\n<p>So, how do you easily solve this problem? Let&#8217;s walk through an example.<\/p>\n<p><span id=\"more-675247\"><\/span><\/p>\n<h2>Create the example Quarkus application<\/h2>\n<p>Instead of using a complex example, let&#8217;s take a simple use case that explains the concept well. Start by creating a new Quarkus app:<\/p>\n<pre>$ mvn io.quarkus:quarkus-maven-plugin:1.1.1.Final:create<\/pre>\n<p>You can keep all of the default values while creating the new application. In this example, the application is named <code>hello-app<\/code>. Now, open the <code>HelloResource.java<\/code> file and refactor it to look like this:<\/p>\n<pre>@Path(\"\/hello\") public class HelloResource { @ConfigProperty(name = \"greeting.message\") String message; @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return \"hello \" + message; } } <\/pre>\n<p>In your <code>application.properties<\/code> file, now add <code>greeting.message=localhost<\/code>. The <code>@ConfigProperty<\/code> 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\u00a0annotation.<\/p>\n<p>Now, let&#8217;s start our application to see if it works as expected: <code><\/code><\/p>\n<pre>$ mvn compile quarkus:dev<\/pre>\n<p>Browse to <code>http:\/\/localhost:8080\/hello<\/code>, which should output <code>hello localhost<\/code>. That&#8217;s it for the Quarkus app. It&#8217;s ready to go.<\/p>\n<h2>Deploy the application to the Kubernetes cluster<\/h2>\n<p>The idea here is to deploy this application to our Kubernetes cluster and replace the value of our <code>greeting<\/code> property with one that will work on the cluster. It is important to know here that all of the properties from <code>application.properties<\/code> 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 (<code>.<\/code>) with an underscore (<code>_<\/code>). So, for instance, our <code>greeting.message<\/code> will become <code>GREETING_MESSAGE<\/code>.<\/p>\n<p>At this point, we are almost ready to deploy our app to Kubernetes, but we need to do three more things:<\/p>\n<ol>\n<li>Create a Docker image of your application and push it to a repository that your cluster can access.<\/li>\n<li>Define a ConfgMap resource.<\/li>\n<li>Generate the Kubernetes resources for our application.<\/li>\n<\/ol>\n<p>To create the Docker image, simply execute this command:<\/p>\n<pre>$ docker build -f src\/main\/docker\/Dockerfile.jvm -t quarkus\/hello-app .<\/pre>\n<p>Be sure to set the right Docker username and to also push to an image registry, like <code>docker-hub<\/code> or <code>quay<\/code>. If you are not able to push an image, you can use <code>sebi2706\/hello-app:latest<\/code>.<\/p>\n<p>Next, create the file <code>config-hello.yml<\/code>:<\/p>\n<pre>apiVersion: v1 data: greeting: \"Kubernetes\" kind: ConfigMap metadata: name: hello-config <\/pre>\n<p>Make sure that you are connected to a cluster and apply this file:<\/p>\n<pre>$ kubectl apply -f config-hello.yml<\/pre>\n<p>Quarkus comes with a useful extension, <code>quarkus-kubernetes<\/code>, that generates the Kubernetes resources for you. You can even tweak the generated resources by providing extra properties\u2014for more details, check out this <a href=\"https:\/\/quarkus.io\/guides\/kubernetes\" target=\"_blank\" rel=\"noopener noreferrer\">guide<\/a>.<\/p>\n<p>After installing the extension, add these properties to our <code>application.properties<\/code> file so it generates extra configuration arguments for our containers specification:<\/p>\n<pre>kubernetes.group=yourDockerUsername kubernetes.env-vars[0].name=GREETING_MESSAGE kubernetes.env-vars[0].value=greeting kubernetes.env-vars[0].configmap=hello-config<\/pre>\n<p>Run <code>mvn package<\/code> and view the generated resources in <code>target\/kubernetes<\/code>. The interesting part is in <code>spec.containers.env<\/code>:<\/p>\n<pre>- name: \"GREETING_MESSAGE\" \u00a0 valueFrom: \u00a0 configMapKeyRef: \u00a0\u00a0\u00a0 key: \"greeting\" \u00a0\u00a0 name: \"hello-config\"<\/pre>\n<p>Here, we see how to pass an environment variable to our container with a value coming from a ConfigMap. Now, simply apply the resources:<\/p>\n<pre>$ kubectl apply -f target\/kubernetes\/kubernetes.yml<\/pre>\n<p>Expose your service:<\/p>\n<pre class=\"highlightjs highlight\"><code class=\"language-shell hljs\" data-lang=\"shell\">kubectl expose deployment hello --type=NodePort<\/code><\/pre>\n<p>Then, browse to the public URL or do a curl. For instance, with Minikube:<\/p>\n<pre class=\"highlightjs highlight\"><code class=\"language-shell hljs\" data-lang=\"shell\">$ curl $(minikube service hello-app --url)\/hello<\/code><\/pre>\n<p>This command should output: <code>hello Kubernetes<\/code>.<\/p>\n<h2>Conclusion<\/h2>\n<p>Now you know how to use a ConfigMap in combination with environment variables and your Quarkus&#8217;s <code>application.properties<\/code>. As we said in the introduction, this technique is particularly useful when defining a DB connection&#8217;s URL (like <code>QUARKUS_DATASOURCE_URL<\/code>) or when using the <code>quarkus-rest-client<\/code> (<code>ORG_SEBI_OTHERSERVICE_MP_REST_URL<\/code>).<\/p>\n<p><a class=\"a2a_button_facebook\" href=\"https:\/\/www.addtoany.com\/add_to\/facebook?linkurl=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2020%2F01%2F23%2Fusing-kubernetes-configmaps-to-define-your-quarkus-applications-properties%2F&amp;linkname=Using%20Kubernetes%20ConfigMaps%20to%20define%20your%20Quarkus%20application%E2%80%99s%20properties\" title=\"Facebook\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><\/a><a class=\"a2a_button_twitter\" href=\"https:\/\/www.addtoany.com\/add_to\/twitter?linkurl=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2020%2F01%2F23%2Fusing-kubernetes-configmaps-to-define-your-quarkus-applications-properties%2F&amp;linkname=Using%20Kubernetes%20ConfigMaps%20to%20define%20your%20Quarkus%20application%E2%80%99s%20properties\" title=\"Twitter\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><\/a><a class=\"a2a_button_linkedin\" href=\"https:\/\/www.addtoany.com\/add_to\/linkedin?linkurl=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2020%2F01%2F23%2Fusing-kubernetes-configmaps-to-define-your-quarkus-applications-properties%2F&amp;linkname=Using%20Kubernetes%20ConfigMaps%20to%20define%20your%20Quarkus%20application%E2%80%99s%20properties\" title=\"LinkedIn\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><\/a><a class=\"a2a_button_email\" href=\"https:\/\/www.addtoany.com\/add_to\/email?linkurl=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2020%2F01%2F23%2Fusing-kubernetes-configmaps-to-define-your-quarkus-applications-properties%2F&amp;linkname=Using%20Kubernetes%20ConfigMaps%20to%20define%20your%20Quarkus%20application%E2%80%99s%20properties\" title=\"Email\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><\/a><a class=\"a2a_button_print\" href=\"https:\/\/www.addtoany.com\/add_to\/print?linkurl=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2020%2F01%2F23%2Fusing-kubernetes-configmaps-to-define-your-quarkus-applications-properties%2F&amp;linkname=Using%20Kubernetes%20ConfigMaps%20to%20define%20your%20Quarkus%20application%E2%80%99s%20properties\" title=\"Print\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><\/a><a class=\"a2a_button_reddit\" href=\"https:\/\/www.addtoany.com\/add_to\/reddit?linkurl=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2020%2F01%2F23%2Fusing-kubernetes-configmaps-to-define-your-quarkus-applications-properties%2F&amp;linkname=Using%20Kubernetes%20ConfigMaps%20to%20define%20your%20Quarkus%20application%E2%80%99s%20properties\" title=\"Reddit\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><\/a><a class=\"a2a_button_flipboard\" href=\"https:\/\/www.addtoany.com\/add_to\/flipboard?linkurl=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2020%2F01%2F23%2Fusing-kubernetes-configmaps-to-define-your-quarkus-applications-properties%2F&amp;linkname=Using%20Kubernetes%20ConfigMaps%20to%20define%20your%20Quarkus%20application%E2%80%99s%20properties\" title=\"Flipboard\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><\/a><a class=\"a2a_dd addtoany_share_save addtoany_share\" href=\"https:\/\/www.addtoany.com\/share#url=https%3A%2F%2Fdevelopers.redhat.com%2Fblog%2F2020%2F01%2F23%2Fusing-kubernetes-configmaps-to-define-your-quarkus-applications-properties%2F&#038;title=Using%20Kubernetes%20ConfigMaps%20to%20define%20your%20Quarkus%20application%E2%80%99s%20properties\" data-a2a-url=\"https:\/\/developers.redhat.com\/blog\/2020\/01\/23\/using-kubernetes-configmaps-to-define-your-quarkus-applications-properties\/\" data-a2a-title=\"Using Kubernetes ConfigMaps to define your Quarkus application\u2019s properties\"><img decoding=\"async\" src=\"https:\/\/static.addtoany.com\/buttons\/favicon.png\" alt=\"Share\"><\/a><\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/developers.redhat.com\/blog\/2020\/01\/23\/using-kubernetes-configmaps-to-define-your-quarkus-applications-properties\/\">Using Kubernetes ConfigMaps to define your Quarkus application&#8217;s properties<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/developers.redhat.com\/blog\">Red Hat Developer<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s properties. After all, your app probably has to connect with a database, call other [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":107905,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[858,3],"tags":[886,887,888,444,539,859,513,872],"class_list":["post-107904","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","category-tutorials","tag-cluster","tag-configmap","tag-deploy-app","tag-devops","tag-docker","tag-java","tag-kubernetes","tag-quarkus"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/107904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=107904"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/107904\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/107905"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=107904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=107904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=107904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}