简体   繁体   English

如何在运行时编辑 spring 启动 kubernetes 应用程序中的 configmap 配置

[英]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 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.第二个问题是如何处理这个 configmap。 I know I can edit and then use apply but this can lead to some error.我知道我可以编辑然后使用 apply 但这可能会导致一些错误。 Is there some tool which I can use to edit this yaml and make some bash script for editing?是否有一些工具可以用来编辑这个 yaml 并制作一些 bash 脚本进行编辑? like: ./my-script.sh -function addMyCustomMapValue -args "KEY5:value5".比如:./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.我尝试探索yq但我认为存在一些限制,并且很难用于某些用例然后kustomize我认为这对于创建 configmap 但不适用于编辑现有的。

Is there already some good example for this use-case?这个用例是否已经有一些很好的例子?

Why don't you use the kubernetes client sdk for Java and a yaml library for editing (SnakeYAML)?为什么不使用 kubernetes 客户端 sdk 用于 Java 和 Z6EEDC03A68A699374C763E6 库用于编辑? Those libraries helped me with the same problem you are trying to solve.这些库帮助我解决了您要解决的相同问题。

Option: 1选项1

You can Use the Lens: https://k8slens.dev/kubernetes.html您可以使用镜头: https://k8slens.dev/kubernetes.html

It's UI for monitoring and Managing K8s clusters.它是用于监控和管理 K8s 集群的 UI。 Using this you can also edit the configmap.使用它,您还可以编辑 configmap。

Option: 2选项:2

You can manage all the Key value into single YAML file and create configmap from file:您可以将所有 Key 值管理到单个 YAML 文件中,并从文件中创建配置映射:

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

Option: 3选项:3

Use helm and values.yaml template to create and your chart and apply it further.使用helmvalues.yaml模板来创建您的图表并进一步应用它。

Option: 4选项: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/如果您使用 configmap 作为环境或将其注入文件路径,您也可以使用 Hashi corp vault: https://www.vaultproject.io/

Option: 5选项: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.正如您所建议的,您可以创建一个Bash脚本,它将现有的正在运行的Configmap导出到一个新的YAML文件,您可以手动编辑 YAML 文件。 You can apply the changes to K8s cluster.您可以将更改应用到K8s集群。

#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.您还可以查看: https://github.com/Gallore/yaml_cli可能会有所帮助。

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.这就是我们可以指定配置 map 的方式。 Add applicationConfiguration in your values.yaml if it specified then only it will write an application.properties externally.在您的 values.yaml 中添加applicationConfiguration如果它指定,那么只有它会在外部写入application.properties It need not have all the properties.它不需要具有所有属性。

This is best tool to edit and make changes in kubernetes env这是在 kubernetes 环境中编辑和更改的最佳工具

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

You can explore https://kubeval.instrumenta.dev/ to catch any configmap error before deploying您可以探索https://kubeval.instrumenta.dev/以在部署之前捕获任何 configmap 错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM