Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit configmap configuration in spring boot kubernetes application during runtime

We have application with huge configuration (this is just a part):

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-app
data:
  application.yaml: |-
    config:
      app: MY-APP
      my-custom-map:
        KEY1: value1
        KEY2: value2
        KEY3: value3
        KEY4: value4
      something1: true
      something2: 123
      something3: string123
      something4: null
      subclass:
        anotherMap:
          "[AAA:0.0.1,BBB:CCC]": "DDD:EEEE"
      subclass2:
        something4: AAAA
        anotherMap2:
          0.0.3: 0.0.3

I follow this example to bind configmap with spring boot configuration but there is still some problem for example how to solve null in yaml which spring yaml postprocessor resolve as empty string: issue

second problem is how to handle this configmap. I know I can edit and then use apply but this can lead to some error. Is there some tool which I can use to edit this yaml and make some bash script for editing ? like: ./my-script.sh -function addMyCustomMapValue -args "KEY5:value5" . I tried to explore yq but I think there is some limitation and it is hard to use for some use-case and then kustomize which I think is good for creating configmap but not for editing existing one.

Is there already some good example for this use-case ?

like image 881
hudi Avatar asked Dec 07 '21 14:12

hudi


People also ask

How do I create a ConfigMap for Kubernetes?

You can use kubectl create configmap to create a ConfigMap from multiple files in the same directory. When you are creating a ConfigMap based on a directory, kubectl identifies files whose basename is a valid key in the directory and packages each of those files into the new ConfigMap.

How do I create a ConfigMap in Kubernetes from Yaml?

You can create a ConfigMap in a declarative way by first declaring the configuration data in a manifest YAML file and then use kubectl create command to create the ConfigMap.

How to configure Spring Boot application on Kubernetes?

One of the ways configuring the spring boot application on kubernetes is to use ConfigMaps. ConfigMaps is a way to decouple the application specific artifacts from the container image, thereby enabling better portability and externalization. The sources of this blog post are available in my github repo.

What is ConfigMaps in Spring Boot?

ConfigMaps is the Kubernetes counterpart of the Spring Boot externalized configuration. ConfigMaps is a simple key/value store, which can store simple values to files. In this post "Configuring Spring Boot on Kubernetes with ConfigMap", we will see how to use ConfigMaps to externalize the application configuration.

What is configmap in Kubernetes?

The ConfigMap residing at container and available for the application at runtime when deployed in pod. The ConfigMap API resource holds key-value pairs of configuration data that can be consumed in pods or used to store configuration data for system components such as controllers.

How do I create a spring app config map?

kubectl create configmap spring-app-config --from-file=src/main/resources/application.properties The command above will create a ConfigMap called spring-app-config with the application.properties file stored as one of the properties.


3 Answers

Option : 1

You can Use the Lens : https://k8slens.dev/kubernetes.html

It's UI for monitoring and Managing K8s clusters. Using this you can also edit the configmap.

Option : 2

You can manage all the Key value into single YAML file and create configmap from file :

kubectl create configmap some-config \
  --from-file=some-key=some-config.yaml \
  -n some-namespace \
  -o yaml \
  --dry-run | kubectl apply -f - 

Option : 3

Use helm and values.yaml template to create and your chart and apply it further.

Configmap using YAML helm

apiVersion: v1
kind: ConfigMap
metadata:
  name: jksconfig
data:
  config.json: |-
    {{ .Files.Get "config.json" | indent 4 }}

Option : 4

If you are using the configmap as Environment or injecting it to file path you can use the Hashi corp vault also : https://www.vaultproject.io/

Option : 5

As you suggested you can create one Bash script which will export the existing running Configmap to a new YAML file one you are done with editing YAML manually. You can apply the changes to K8s cluster.

#bin/bash
kubectl get configmap <configmap-name>  -o yaml > cofig.yaml

You can also check the : https://github.com/Gallore/yaml_cli might be helpful.

like image 131
Harsh Manvar Avatar answered Oct 20 '22 08:10

Harsh Manvar


apiVersion: v1
kind: ConfigMap
metadata:
  name: application-conf
data: 
  {{- if .Values.global.applicationConfiguration }}
  application.properties: | 
    {{- .Values.global.applicationConfiguration  | nindent 4 }}
   {{- end }}

This is how we can specify a config map. Add applicationConfiguration in your values.yaml if it specified then only it will write an application.properties externally. It need not have all the properties.

like image 3
Komal Kadam Avatar answered Oct 20 '22 07:10

Komal Kadam


This is best tool to edit and make changes in kubernetes env

k9s : https://github.com/derailed/k9s

You can explore https://kubeval.instrumenta.dev/ to catch any configmap error before deploying

like image 2
RITESH SANJAY MAHAJAN Avatar answered Oct 20 '22 08:10

RITESH SANJAY MAHAJAN