Using Kubernetes ConfigMaps to define your Quarkus application’s properties - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Java Language, JVM, and the JRE (https://www.sickgaming.net/forum-78.html) +--- Thread: Using Kubernetes ConfigMaps to define your Quarkus application’s properties (/thread-99078.html) |
Using Kubernetes ConfigMaps to define your Quarkus application’s properties - xSicKxBot - 03-27-2022 Using Kubernetes ConfigMaps to define your Quarkus application’s properties <div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/01/using-kubernetes-configmaps-to-define-your-quarkus-applications-properties.png" width="16" height="16" title="" alt="" /></div><div><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’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’t work once deployed onto your cluster.</p> <p>So, how do you easily solve this problem? Let’s walk through an example.</p> <p><span id="more-675247"></span></p> <h2>Create the example Quarkus application</h2> <p>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:</p> <pre>$ mvn io.quarkus:quarkus-maven-plugin:1.1.1.Final:create</pre> <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> <pre>@Path("/hello") public class HelloResource { @ConfigProperty(name = "greeting.message") String message; @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return "hello " + message; } } </pre> <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 annotation.</p> <p>Now, let’s start our application to see if it works as expected: <code></code></p> <pre>$ mvn compile quarkus:dev</pre> <p>Browse to <code>http://localhost:8080/hello</code>, which should output <code>hello localhost</code>. That’s it for the Quarkus app. It’s ready to go.</p> <h2>Deploy the application to the Kubernetes cluster</h2> <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> <p>At this point, we are almost ready to deploy our app to Kubernetes, but we need to do three more things:</p> <ol> <li>Create a Docker image of your application and push it to a repository that your cluster can access.</li> <li>Define a ConfgMap resource.</li> <li>Generate the Kubernetes resources for our application.</li> </ol> <p>To create the Docker image, simply execute this command:</p> <pre>$ docker build -f src/main/docker/Dockerfile.jvm -t quarkus/hello-app .</pre> <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> <p>Next, create the file <code>config-hello.yml</code>:</p> <pre>apiVersion: v1 data: greeting: "Kubernetes" kind: ConfigMap metadata: name: hello-config </pre> <p>Make sure that you are connected to a cluster and apply this file:</p> <pre>$ kubectl apply -f config-hello.yml</pre> <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—for more details, check out this <a href="https://quarkus.io/guides/kubernetes" target="_blank" rel="noopener noreferrer">guide</a>.</p> <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> <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> <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> <pre>- name: "GREETING_MESSAGE" valueFrom: configMapKeyRef: key: "greeting" name: "hello-config"</pre> <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> <pre>$ kubectl apply -f target/kubernetes/kubernetes.yml</pre> <p>Expose your service:</p> <pre class="highlightjs highlight"><code class="language-shell hljs" data-lang="shell">kubectl expose deployment hello --type=NodePort</code></pre> <p>Then, browse to the public URL or do a curl. For instance, with Minikube:</p> <pre class="highlightjs highlight"><code class="language-shell hljs" data-lang="shell">$ curl $(minikube service hello-app --url)/hello</code></pre> <p>This command should output: <code>hello Kubernetes</code>.</p> <h2>Conclusion</h2> <p>Now you know how to use a ConfigMap in combination with environment variables and your Quarkus’s <code>application.properties</code>. As we said in the introduction, this technique is particularly useful when defining a DB connection’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> <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&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&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&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&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&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&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&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&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’s properties"><img src="https://static.addtoany.com/buttons/favicon.png" alt="Share"></a></p> <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’s properties</a> appeared first on <a rel="nofollow" href="https://developers.redhat.com/blog">Red Hat Developer</a>.</p> </div> https://www.sickgaming.net/blog/2020/01/23/using-kubernetes-configmaps-to-define-your-quarkus-applications-properties/ |