kubernates configmaps - Neethahiremath/Wiki GitHub Wiki

  1. we can store the application yml and related configuration files in kubernative config maps and make the application to read the properties from that maps.

  2. we should have a script that loads the files to config maps, when running the application ex: in spinnaker you can specify the path and make it read from config map.


#!/bin/sh

# below two variables are passed as an arguments while triggering the script.
configmapName="$1"
appName="$2"
# path to the application configuration file
pathToConfigFiles="/app/config/$appName"

#command to create configmap

kubectl create configmap $configmapName --from-file=$pathToConfigFiles --dry-run -o yaml -n namespace_name | kubectl apply -f - ;
fi

whenever we make a change to yml we should run the script and push the updated yml to config maps, and restart the services. say we have some specific attributes from yml which we want to change and thats too frequent. we would not want to restart the application every time we load the changes, instead want those changed fields to load dynamically.

for this, we can maintain separate yml file along with application yml in a project that only have a config maintained and shell script to load the files to cloud config.

in that yml, we can have changing attributes mentioned and wherever we use those values, we can provide the path to k8 config map to pick from that.

now to avoid the restart of the application, we should have code changes in application for spring to load the properties dynamically

we use ReloadablePropertySource for this along with PeriodicReloadingTrigger

refer this https://github.com/Neethahiremath/dynamicpropertyloader for loading the value dynamically and assign to the attributes.

alternative is using the k8 config in bootstrap.yml

k8 reload configMap:

bootstrap.yml


spring:
    application:
        name: service
    profiles:
        include: kubernetes
    cloud:
        kubernetes:
            config:
                enabled: true
                namespace: ns
                sources:
                    - name: svc-configmap
            reload:
                enabled: true
                strategy: refresh

management:
    endpoint:
        restart:
            enabled: true
        refresh:
            enabled: true

application.yml

  • field that u want to reload add in application.yml
management:
    endpoints:
        web:
            base-path: /actuator
            exposure:
                include: ["configprops", "env", "metrics", "prometheus", "health", "info", "threaddump"]

cluster:
    active: central

  • use @configurationProperties class and add this field
@Configuration
@ConfigurationProperties("cluster")
@Data
public class Config {

    private String active;

}

use end point to check the value of fields

/actuator/configprops