简体   繁体   English

应用动态配置的k8s configmap

[英]K8s configmap for application dynamic configuration

I have a microservice for handling retention policy.我有一个用于处理保留策略的微服务。 This application has default configuration for retention, eg: size for retention, files location etc. But we also want create an API for the user to change this configuration with customized values on runtime.此应用程序具有保留的默认配置,例如:保留大小、文件位置等。但我们还希望为用户创建一个 API 以在运行时使用自定义值更改此配置。

I created a configmap with the default values, and in the application I used k8s client library to get/update/watch the configmap.我使用默认值创建了一个 configmap,并在应用程序中使用了 k8s 客户端库来获取/更新/监视 configmap。

My question is, is it correct to use configmap for dynamic buisness configuration?我的问题是,使用 configmap 进行动态业务配置是否正确? or is it meant for static configuration that user is not supposed to touch during runtime?或者它是否意味着用户在运行时不应触摸的 static 配置?

Thanks in advance提前致谢

There are no rules against it.没有反对它的规则。 A lot of software leverages kube API to do some kind of logic / state, ie.许多软件利用 kube API 来执行某种逻辑 / state,即。 leader election.领导人选举。 All of those require the app to apply changes to a kube resource.所有这些都需要应用程序将更改应用于 kube 资源。 With that in mind do remember it always puts some additional load on your API and if you're unlucky that might become an issue.考虑到这一点,请记住它总是会给您的 API 带来一些额外的负担,如果您不走运,这可能会成为一个问题。 About two years ago we've been experiencing API limits exhaustion on one of the managed k8s services cause we were using a lot of deployments that had rather intensive leader election logic (2 requests per pod every 5 sec).大约两年前,我们在其中一项托管的 k8s 服务上遇到了 API 限制耗尽,因为我们使用了很多具有相当密集的领导者选举逻辑的部署(每 5 秒每个 pod 2 个请求)。 The issue is long gone since then, but it shows what you have to take into account when designing interactions like this (retries, backoffs etc.)从那时起这个问题早已不复存在,但它显示了在设计这样的交互时必须考虑的因素(重试、退避等)

Using configMaps is perfectly fine for such use cases.使用 configMaps 非常适合此类用例。 You can use a client library in order to watch for updates on the given configMap, however a cleaner solution would be to mount the configMap as a file into the pod and have your configuration set up from the given file.您可以使用客户端库来监视给定 configMap 的更新,但是更简洁的解决方案是将 configMap 作为文件安装到 pod 中,并从给定文件设置配置。 Since you're mounting the configMap as a Volume, changes won't need a pod restart for changes to be visible within the pod (unlike env variables that only "refresh" once the pod get's recreated).由于您将 configMap 作为卷安装,因此更改不需要重新启动 pod 即可使更改在 pod 中可见(不像 env 变量只在 pod get 重新创建后才“刷新”)。


Let's say you have this configMap :假设您有这个configMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm

And then you mount this configMap as a Volume into your Pod:然后你将这个configMap作为一个 Volume 挂载到你的 Pod 中:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config
  restartPolicy: Never

When the pod runs, the command ls /etc/config/ produces the output below:当 pod 运行时,命令ls /etc/config/会生成以下 output:

SPECIAL_LEVEL
SPECIAL_TYPE

This way you would also reduce "noise" to the API-Server as you can simply query the given files for updates to any configuration.通过这种方式,您还可以减少 API 服务器的“噪音”,因为您可以简单地查询给定文件以获取对任何配置的更新。

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

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