sebastiandaschner blog

Inject Kubernetes ConfigMap values with Java EE & WildFly

wednesday, april 26, 2017

The Kubernetes ConfigMap concept is used to configure applications from the orchestration environment. The configured values can be made accessible within the Java EE container. This blog posts shows the integration of Kubernetes ConfigMaps, WildFly and Java EE vendor-agnostic configuration.

Kubernetes can inject configured values to the running POD, e.g. as properties files. By defining custom modules in WildFly, these files — that are not shipped with the deployment artifact — can be made accessible from the classpath.

First define a Kubernetes ConfigMap via YAML file:

kind: ConfigMap
apiVersion: v1
  name: hello-config
data: |

Alternatively, you can also define ConfigMaps directly from properties files: kubectl create configmap hello-config

The properties entries of this ConfigMap are injected into the running POD in different ways, for instance via mounted volume, which results in a properties file being created, containing all the configured values.

Consider following container definition in a deployment resource using WildFly as application server and referring to the ConfigMap.

kind: Deployment
apiVersion: apps/v1beta1
  name: cloud-native-jee
  replicas: 1
        app: cloud-native-jee
      - name: cloud-native-jee
        image: cloud-native-jee:123
        - name: config-volume
          mountPath: /opt/wildfly/modules/com/sebastian-daschner/configuration/main/properties
      - name: config-volume
          name: hello-config
      restartPolicy: Always

The base image containing the WildFly application server has to configure an additional module. This is done by placing a module.xml file into a module directory <wildfly_home>/modules/com/sebastian-daschner/configuration/main:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.3" name="com.sebastian-daschner.configuration">
        <resource-root path="properties"/>

Also declare the module in the standalone.xml configuration:

<subsystem xmlns="urn:jboss:domain:ee:4.0">
        <module name="com.sebastian-daschner.configuration" slot="main" />

This will access the new module and make all files residing under …​/configuration/main/properties accessible from the classpath.

Now the running container can access the file. To make the development process more convenient, we define a custom CDI producer for the properties:

public class Configurator {

    private final Properties properties = new Properties();

    private void initProperties() {
        try (final InputStream inputStream = Configurator.class.getResourceAsStream("/")) {
        } catch (IOException e) {
            throw new RuntimeException("Could not init configuration", e);

    public String exposeConfig(InjectionPoint injectionPoint) {
        final Config config = injectionPoint.getAnnotated().getAnnotation(Config.class);
        if (config != null)
            return properties.getProperty(config.value());
        return null;


The @Config annotation is defined in our application to select the specific keys:

public @interface Config {

    String value();


Now we can @Inject values from anywhere in our application:

public class HelloResource {

    String greeting;

    String name;

    public String hello() {
        return greeting + ", " + name + "!";


Happy Kubernetes configuring!


Found the post useful? Subscribe to my newsletter for more free content, tips and tricks on IT & Java: