当它们的configmap被更改/更新时,我如何自动重新启动Kubernetes pod和与部署相关的pod ?


我知道有关于在配置映射发生变化时自动重启pod的讨论,但据我所知,这在Kubernetes 1.2中还没有。

因此(我认为)我想做的是“滚动重新启动”与使用配置映射的pod相关联的部署资源。在Kubernetes中,在不改变实际模板的情况下强制滚动重启部署是否可能?如果可能的话,如何实现?这是目前最好的方式还是有更好的选择?


当前回答

另一种方法是将其插入部署的命令部分:

...
command: [ "echo", "
  option = value\n
  other_option = value\n
" ]
...

或者,为了使它更像configmap,使用一个额外的部署,它只会在命令节中托管该配置,并在其上执行kubectl create,同时在其名称中添加一个唯一的“版本”(比如计算内容的哈希值),并修改所有使用该配置的部署:

...
command: [ "/usr/sbin/kubectl-apply-config.sh", "
  option = value\n
  other_option = value\n
" ]
...

我可能会发布kubectl-apply-config.sh,如果它最终能够工作的话。

(不要那样做;看起来太糟糕了)

其他回答

Helm 3文档页

通常情况下,配置映射或秘密作为配置文件注入到容器中。根据应用程序的不同,如果后续的头盔升级更新了这些配置,则可能需要重新启动,但如果部署规范本身没有更改,则应用程序将继续使用旧配置运行,从而导致部署不一致。

sha256sum函数可以与include函数一起使用,以确保在其他规范更改时更新部署模板部分:

kind: Deployment
spec:
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
[...]

在我的例子中,由于某些原因,$. template。BasePath没有工作,但$. chart。名称:

spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: admin-app
      annotations:
        checksum/config: {{ include (print $.Chart.Name "/templates/" $.Chart.Name "-configmap.yaml") . | sha256sum }}

向配置映射添加不可变属性完全避免了这个问题。使用配置散列有助于无缝滚动更新,但对回滚没有帮助。你可以看看这个开源项目- 'Configurator' - https://github.com/gopaddle-io/configurator.git .'Configurator'通过以下使用自定义资源工作:

配置器将部署生命周期与configMap绑定在一起。当 配置映射被更新,并为此创建一个新版本 configMap。所有附加到configMap的部署 获得与最新configMap版本绑定的滚动更新。 当您将部署回滚到旧版本时,它会反弹到 configMap在滚动更新之前的版本。

通过这种方式,您可以维护配置映射的版本,并方便与配置映射一起滚转和回滚到部署。

当部署在子图表中,而控制它的值在父图表的值文件中时,出现了这个问题。这是我们用来触发重启的:

spec:
  template:
    metadata:
      annotations:
        checksum/config: {{ tpl (toYaml .Values) . | sha256sum }}

显然,这将在任何值更改时触发重新启动,但它适用于我们的情况。最初在子图中的内容只有在配置。Yaml在子图中本身发生了变化:

    checksum/config: {{ include (print $.Template.BasePath "/config.yaml") . | sha256sum }}

如何自动重启Kubernetes pod和相关的pod 当他们的configmap改变/更新部署?

如果您使用configmap作为环境,则必须使用外部选项。

复载机 Kube观察家 配置器

Kubernetes自动重新加载配置映射,如果它被挂载为卷(如果子路径在那里,它不会工作)。

When a ConfigMap currently consumed in a volume is updated, projected keys are eventually updated as well. The kubelet checks whether the mounted ConfigMap is fresh on every periodic sync. However, the kubelet uses its local cache for getting the current value of the ConfigMap. The type of the cache is configurable using the ConfigMapAndSecretChangeDetectionStrategy field in the KubeletConfiguration struct. A ConfigMap can be either propagated by watch (default), ttl-based, or by redirecting all requests directly to the API server. As a result, the total delay from the moment when the ConfigMap is updated to the moment when new keys are projected to the Pod can be as long as the kubelet sync period + cache propagation delay, where the cache propagation delay depends on the chosen cache type (it equals to watch propagation delay, ttl of cache, or zero correspondingly).

正式文档:https://kubernetes.io/docs/concepts/configuration/configmap/#mounted-configmaps-are-updated-automatically

作为环境变量使用的configmap不会自动更新,并且需要pod重新启动。

简单示例

apiVersion: v1
kind: ConfigMap
metadata:
  name: config
  namespace: default
data:
  foo: bar

舱配置

spec:
  containers:
  - name: configmaptestapp
    image: <Image>
    volumeMounts:
    - mountPath: /config
      name: configmap-data-volume
    ports:
    - containerPort: 8080
  volumes:
    - name: configmap-data-volume
      configMap:
        name: config

例如:https://medium.com/@harsh.manvar111/update-configmap- with-restart -pod-56801dce3388

如果k8 > 1.15;然后做一个rollout重启对我来说是最好的,作为CI/CD的一部分,应用配置路径与卷挂载相连。重新加载插件或设置restartPolicy: Always in deployment manifest YML对我不起作用。不需要更改应用程序代码,既适用于静态资产,也适用于微服务。

kubectl rollout restart deployment/<deploymentName> -n <namespace>