Skip to content

fabric8-quickstarts/spring-boot-camel-config

Repository files navigation

Spring-Boot Camel QuickStart using ConfigMaps and Secrets

This quickstart demonstrates how to configure a Spring-Boot application using Kubernetes ConfigMaps and Secrets.

A route generates sample messages that are delivered to a list of recipient endpoints configured through a property named quickstart.recipients in the src/main/resources/application.properties file. The property can be overridden using a Kubernetes ConfigMap object. As soon as a ConfigMap named camel-config (containing a property named application.properties) is created or changed in the namespace, an application-context refresh event will be triggered and the logs will reflect the new configuration. A sample ConfigMap (sample-configmap.yml) is contained in this repository (it changes the configuration to use all available endpoints in the recipientList).

The quickstart will run on Openshift using a ServiceAccount named qs-camel-config, with the view role granted. This way, the application is allowed to read the ConfigMap and to listen for changes in the current Openshift project.

Secrets can also be used to configure the application (a sample username/password combination is configured using secrets in this quickstart). Unlike the ConfigMap objects, secrets require higher permissions in order to be read using the Openshift APIs. To overcome this security limitation, the approach used in this quickstart is to mount the secret as a volume in the Pod and configure its location in the spring-cloud config file (src/main/resources/bootstrap.yml).

A sample secret (sample-secret.yml) is contained in this repository (it just replaces the username wrong-username with myuser).

Note: a secret named camel-config must be present in the namespace before the application is deployed (otherwise the container remains in a pending status, waiting for it).

The application utilizes the Spring @ImportResource annotation to load a Camel Context definition via a src/main/resources/spring/camel-context.xml file on the classpath.

Important
This quickstart can run in 2 modes: standalone on your machine and on Kubernetes / OpenShift Cluster. Quickstart requires Java 8 or Java 11 (fuse-java-openshift-jdk11-rhel8 image is used to build in Java 11).

Deployment options

You can run this quickstart in the following modes:

  • Kubernetese / Single-node OpenShift cluster

  • Standalone on your machine

The most effective way to run this quickstart is to deploy and run the project on OpenShift.

For more details about running this quickstart on a single-node OpenShift cluster, CI/CD deployments, as well as the rest of the runtime, see the Spring Boot Runtime Guide.

Running the Quickstart on a single-node Kubernetes/OpenShift cluster

Important
You need to run this example on Container Development Kit 3.3 or OpenShift 3.7. Both of these products have suitable Fuse images pre-installed. If you run it in an environment where those images are not preinstalled follow the steps described in Running the Quickstart on a single-node Kubernetes/OpenShift cluster without preinstalled images.

A single-node Kubernetes/OpenShift cluster provides you with access to a cloud environment that is similar to a production environment.

If you have a single-node Kubernetes/OpenShift cluster, such as Minishift or the Red Hat Container Development Kit, installed and running, you can deploy your quickstart there.

  1. Log in to your OpenShift cluster:

    $ oc login -u developer -p developer
  2. Create a new OpenShift project for the quickstart:

    $ oc new-project MY_PROJECT_NAME
  3. Change the directory to the folder that contains the extracted quickstart application (for example, my_openshift/spring-boot-camel-config) :

    $ cd my_openshift/spring-boot-camel-config
  4. Create the (required) secret:

    $ oc create -f sample-secret.yml
  5. Create the ConfigMap (the ConfigMap can be also created after the application has been deployed, to see the live-reload feature in action):

    $ oc create -f sample-configmap.yml
  6. Build and deploy the project to the OpenShift cluster:

    $ mvn clean -DskipTests oc:deploy -Popenshift
  7. In your browser, navigate to the MY_PROJECT_NAME project in the OpenShift console. Wait until you can see that the pod for the spring-boot-camel-config has started up.

  8. On the project’s Overview page, navigate to the details page deployment of the spring-boot-camel-config application: https://OPENSHIFT_IP_ADDR:8443/console/project/MY_PROJECT_NAME/browse/rc/spring-boot-camel-config-NUMBER_OF_DEPLOYMENT?tab=details.

  9. Switch to tab Logs and then see the messages sent by Camel.

Running the Quickstart on a single-node Kubernetes/OpenShift cluster without preinstalled images

A single-node Kubernetes/OpenShift cluster provides you with access to a cloud environment that is similar to a production environment.

If you have a single-node Kubernetes/OpenShift cluster, such as Minishift or the Red Hat Container Development Kit, installed and running, you can deploy your quickstart there.

  1. Log in to your OpenShift cluster:

    $ oc login -u developer -p developer
  2. Create a new OpenShift project for the quickstart:

    $ oc new-project MY_PROJECT_NAME
  3. Configure Red Hat Container Registry authentication (if it is not configured). Follow documentation.

  4. Import base images in your newly created project (MY_PROJECT_NAME):

    $ oc import-image fuse-java-openshift:1.13 --from=registry.redhat.io/fuse7/fuse-java-openshift:1.13 --confirm
  5. Change the directory to the folder that contains the extracted quickstart application (for example, my_openshift/spring-boot-camel-config) :

    $ cd my_openshift/spring-boot-camel-config
  6. Create the (required) secret:

    $ oc create -f sample-secret.yml
  7. Create the ConfigMap (the ConfigMap can be also created after the application has been deployed, to see the live-reload feature in action):

    $ oc create -f sample-configmap.yml
  8. Build and deploy the project to the OpenShift cluster:

    $ mvn clean -DskipTests oc:deploy -Popenshift -Djkube.generator.fromMode=istag -Djkube.generator.from=MY_PROJECT_NAME/fuse-java-openshift:1.13
  9. In your browser, navigate to the MY_PROJECT_NAME project in the OpenShift console. Wait until you can see that the pod for the spring-boot-camel-config has started up.

  10. On the project’s Overview page, navigate to the details page deployment of the spring-boot-camel-config application: https://OPENSHIFT_IP_ADDR:8443/console/project/MY_PROJECT_NAME/browse/rc/spring-boot-camel-config-xml-NUMBER_OF_DEPLOYMENT?tab=details.

  11. Switch to tab Logs and then see the messages sent by Camel.

Integration Testing

The example includes a Kubernetes Integration Test. Once the container image has been built and deployed in Kubernetes, the integration test can be run with:

mvn test -Dtest=*KT

The test is disabled by default and has to be enabled using -Dtest.

Running the quickstart standalone on your machine

To run this quickstart as a standalone project on your local machine:

  1. Download the project and extract the archive on your local filesystem.

  2. Build the project:

    $ cd PROJECT_DIR
    $ mvn clean package
  3. Run the service:

    $ mvn spring-boot:run
  4. See the messages sent by Camel.