• Skip to main content
  • Skip to after header navigation
  • Skip to site footer

Free-Mail

  • Events
  • Images
    • Instagram Goodness
  • Music
  • News
  • Software

Build a Quarkus reactive application using Kubernetes Secrets

Build a Quarkus reactive application using Kubernetes Secrets: Build a Quarkus reactive application using Kubernetes Secrets Daniel Oh Tue, 05/24/2022 – 03:00

Register or Login to like
Register or Login to like

Many organizations have security policies in place that dictate how to store sensitive information. When you’re developing applications for the cloud, you’re probably expected to follow those policies, and to do that you often have to externalize your data storage. Kubernetes has a built-in system to access external secrets, and learning to use that is key to a safe cloud-native app.

In this article, I’m going to demonstrate how to build a Quarkus reactive application with externalized sensitive information—for instance, a password or token—using Kubernetes Secrets. A secret is a good example of how cloud platforms can secure applications by removing sensitive data from your static code. Note that you can find a solution to this tutorial in this GitHub repository.

[ Read next: How to explain Kubernetes Secrets ]

1. Scaffold a new reactive Quarkus project

Use the Quarkus command-line interface (CLI) to scaffold a new project. If you haven’t already installed the Quarkus CLI, follow these instructions according to your operating system.

Run the following Quarkus CLI in your project directory to add kubernetes-config, resteasy-reactive, and openshift extensions:

 
$ quarkus create app quarkus-secret-example \
-x resteasy-reactive,kubernetes-config,openshift

The output should look like this:

 
Looking for the newly published extensions in registry.quarkus.io
selected extensions:
- io.quarkus:quarkus-kubernetes-config
- io.quarkus:quarkus-resteasy-reactive
- io.quarkus:quarkus-openshift


applying codestarts...
?  java
?  maven
?  quarkus
?  config-properties
?  dockerfiles
?  maven-wrapper
?  resteasy-reactive-codestart

-----------

[SUCCESS] ?  quarkus project has been successfully generated in:
--> /tmp/quarkus-secret-example
-----------
Navigate into this directory and get started: quarkus dev

2. Create a Secret in Kubernetes

More on Kubernetes
What is Kubernetes?
Free online course: Containers, Kubernetes and Red Hat OpenShift technical over…
eBook: Storage Patterns for Kubernetes
Test drive OpenShift hands-on
An introduction to enterprise Kubernetes
How to explain Kubernetes in plain terms
eBook: Running Kubernetes on your Raspberry Pi homelab
Kubernetes cheat sheet
eBook: A guide to Kubernetes for SREs and sysadmins
Latest Kubernetes articles

To manage the Kubernetes Secrets, you have three options:

  • Using kubectl
  • Using Configuration File
  • Node Kustomize

Use the kubectl command to create a new database credential (a username and password). Run the following command:

 
$ kubectl create secret generic db-credentials \                                                                      
 --from-literal=username=admin \
 --from-literal=password=secret

If you haven’t already installed a Kubernetes cluster locally, or you have no remote cluster, you can sign in to the developer sandbox, a no-cost sandbox environment for Red Hat OpenShift and CodeReady Workspaces.

You can confirm that the Secret is created properly by using the following command:

 $   kubectl get secret/db-credentials -o yaml

The output should look like this:

 
apiVersion: v1
data:
  password: c2VjcmV0
  username: YWRtaW4=
kind: Secret
metadata:
  creationTimestamp: "2022-05-02T13:46:18Z"
  name: db-credentials
  namespace: doh-dev
  resourceVersion: "1190920736"
  uid: 936abd44-1097-4c1f-a9d8-8008a01c0add
type: Opaque

The username and password are encoded by default.

3. Create a new RESTful API to access the Secret

Now you can add a new RESTful (for Representational State Transfer) API to print out the username and password stored in the Kubernetes Secret. Quarkus enables developers to refer to the secret as a normal configuration using a @ConfigureProperty annotation.

Open a GreetingResource.java file in src/main/java/org/acme. Then, add the following method and configurations:

 
@ConfigProperty(name = "username")
    String username;

    @ConfigProperty(name = "password")
    String password;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/securty")
    public Map securty() {
        HashMap<String, String> map = new HashMap<>();
        map.put("db.username", username);
        map.put("db.password", password);
        return map;
    }

Save the file.

4. Set the configurations for Kubernetes deployment

Open the application.properties file in the src/main/resources directory. Add the following configuration for the Kubernetes deployment. In the tutorial, I’ll demonstrate using the developer sandbox, so the configurations tie to the OpenShift cluster.

If you want to deploy it to the Kubernetes cluster, you can package the application via Docker container directly. Then, you need to push the container image to an external container registry (for example, Docker Hub, quay.io, or Google container registry).

 
# Kubernetes Deployment
quarkus.kubernetes.deploy=true
quarkus.kubernetes.deployment-target=openshift
openshift.expose=true
quarkus.openshift.build-strategy=docker
quarkus.kubernetes-client.trust-certs=true

# Kubernetes Secret
quarkus.kubernetes-config.secrets.enabled=true
quarkus.kubernetes-config.secrets=db-credentials

Save the file.

5. Build and deploy the application to Kubernetes

To build and deploy the reactive application, you can also use the following Quarkus CLI:

 $   quarkus build

This command triggers the application build to generate a fast-jar file. Then the application Jar file is containerized using a Dockerfile, which was already generated in the src/main/docker directory when you created the project. Finally, the application image is pushed into the integrated container registry inside the OpenShift cluster.

The output should end with a BUILD SUCCESS message.

When you deploy the application to the developer sandbox or normal OpenShift cluster, you can find the application in the Topology view in the Developer perspective, as shown in the figure below.

A screenshot of Red Hat OpenShift Dedicated. In the left sidebar menu Topology is highlighted, and in the main screen there is a Quarkus icon
Image by:

(Daniel Oh, CC BY-SA 4.0)

6. Verify the sensitive information

To verify that your Quarkus application can refer to the sensitive information from the Kubernetes Secret, get the route URL using the following kubectl command:

 $   kubectl get route

The output is similar to this:

 
NAME                     HOST/PORT                                                               PATH   SERVICES                 PORT   TERMINATION   WILDCARD
quarkus-secret-example   quarkus-secret-example-doh-dev.apps.sandbox.x8i5.p1.openshiftapps.com          quarkus-secret-example   8080                 None

Use the curl command to access the RESTful API:

 $   curl http://YOUR_ROUTE_URL/hello/security

The output:

 {db.password=secret, db.username=admin}

Awesome! The above username and password are the same as those you stored in the db-credentials secret.

Where to learn more

This guide has shown how Quarkus enables developers to externalize sensitive information using Kubernetes Secrets. Find additional resources to develop cloud-native microservices using Quarkus on Kubernetes here:

  • 7 guides for developing applications on the cloud with Quarkus
  • Extend Kubernetes service discovery with Stork and Quarkus
  • Deploy Quarkus applications to Kubernetes using a Helm chart

You can also watch this step-by-step tutorial video on how to manage Kubernetes Secrets with Quarkus.

Follow security policies while developing applications for the cloud by using Kubernetes Secrets.

Improve your DevOps security game with Ansible Vault
Image by:

Opensource.com

Kubernetes
Security and privacy

What to read next
Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.
Register or Login to post a comment.

: Opensource.com Daniel Oh

Supporting Open Source.

Have you tried: Sailing in Africa ?

Share this:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Pinterest (Opens in new window) Pinterest
  • Email a link to a friend (Opens in new window) Email
  • More
  • Share on Tumblr (Opens in new window) Tumblr
  • Print (Opens in new window) Print
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit
Published on 24 May 2022 by Alan Category: SoftwareTag: Application, Build, Kubernetes, Quarkus, reactive, Secrets, Using

5 ways to teach kids to program with Raspberry Pi

5 ways to teach kids to program with Raspberry Pi: As countless schools, libraries, and families …

Read more5 ways to teach kids to program with Raspberry Pi

Nice Image from page 7 of “The Chinese empire: a general & missionary survey ..” (1907) photo

A cool Paternoster image: Image from page 7 of “The Chinese empire: a general & …

Read moreNice Image from page 7 of “The Chinese empire: a general & missionary survey ..” (1907) photo

BRIEF-South Africa’s NUM union asks AngloGold to rethink potential job cuts

South Africa in the news: BRIEF-South Africa’s NUM union asks AngloGold to rethink potential …

Read moreBRIEF-South Africa’s NUM union asks AngloGold to rethink potential job cuts

About Alan

Previous Post:British Library digitised image from page 9 of “Memorials of South Africa”
Next Post:Nice DSC07016_ep photo

Reach Trust

Reach Trust is a holding trust for a number of companies within the Stratlec Group.

Copyright © 2026 · Free-Mail · All Rights Reserved · Powered by Straton Electrical

Back to top