当它们的configmap被更改/更新时,我如何自动重新启动Kubernetes pod和与部署相关的pod ?
我知道有关于在配置映射发生变化时自动重启pod的讨论,但据我所知,这在Kubernetes 1.2中还没有。
因此(我认为)我想做的是“滚动重新启动”与使用配置映射的pod相关联的部署资源。在Kubernetes中,在不改变实际模板的情况下强制滚动重启部署是否可能?如果可能的话,如何实现?这是目前最好的方式还是有更好的选择?
当它们的configmap被更改/更新时,我如何自动重新启动Kubernetes pod和与部署相关的pod ?
我知道有关于在配置映射发生变化时自动重启pod的讨论,但据我所知,这在Kubernetes 1.2中还没有。
因此(我认为)我想做的是“滚动重新启动”与使用配置映射的pod相关联的部署资源。在Kubernetes中,在不改变实际模板的情况下强制滚动重启部署是否可能?如果可能的话,如何实现?这是目前最好的方式还是有更好的选择?
当前回答
考虑使用kustomize(或kubectl apply -k),然后利用它强大的configMapGenerator特性。例如,来自:https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/configmapgenerator/
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
# Just one example of many...
- name: my-app-config
literals:
- JAVA_HOME=/opt/java/jdk
- JAVA_TOOL_OPTIONS=-agentlib:hprof
# Explanation below...
- SECRETS_VERSION=1
然后在部署中引用my-app-config即可。当使用kustomize构建时,它会自动查找并更新my-app-config的引用,并使用更新的后缀,例如my-app-config-f7mm6mhf59。
额外的好处是,更新秘密:我还使用此技术强制重新加载秘密(因为它们以同样的方式受到影响)。虽然我个人完全单独管理我的秘密(使用Mozilla sops),但您可以将配置映射与您的秘密捆绑在一起,例如在您的部署中:
# ...
spec:
template:
spec:
containers:
- name: my-app
image: my-app:tag
envFrom:
# For any NON-secret environment variables. Name is automatically updated by Kustomize
- configMapRef:
name: my-app-config
# Defined separately OUTSIDE of Kustomize. Just modify SECRETS_VERSION=[number] in the my-app-config ConfigMap
# to trigger an update in both the config as well as the secrets (since the pod will get restarted).
- secretRef:
name: my-app-secrets
然后,像我上面所做的那样,在ConfigMap中添加一个像SECRETS_VERSION这样的变量。然后,每次更改my-app-secrets时,只需增加SECRETS_VERSION的值,这除了触发kustomize'd ConfigMap名称的更改外,没有其他用途,这也应该导致重新启动pod。然后就变成:
其他回答
当部署在子图表中,而控制它的值在父图表的值文件中时,出现了这个问题。这是我们用来触发重启的:
spec:
template:
metadata:
annotations:
checksum/config: {{ tpl (toYaml .Values) . | sha256sum }}
显然,这将在任何值更改时触发重新启动,但它适用于我们的情况。最初在子图中的内容只有在配置。Yaml在子图中本身发生了变化:
checksum/config: {{ include (print $.Template.BasePath "/config.yaml") . | sha256sum }}
您可以更新与您的部署无关的元数据注释。它将触发滚动更新
例如:
spec:
template:
metadata:
annotations:
configmap-version: 1
如何自动重启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
目前这个问题的最佳解决方案(在兄弟回答的https://github.com/kubernetes/kubernetes/issues/22368链接中有深入引用)是使用部署,并将configmap视为不可变的。
当您想要更改配置时,请创建一个带有您想要进行更改的新ConfigMap,并将部署指向新的ConfigMap。如果新的配置被破坏,部署将拒绝缩小您的工作ReplicaSet。如果新的配置有效,那么旧的ReplicaSet将被缩放为0副本并删除,新的pod将使用新的配置启动。
虽然没有编辑ConfigMap那么快,但是更安全。
考虑使用kustomize(或kubectl apply -k),然后利用它强大的configMapGenerator特性。例如,来自:https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/configmapgenerator/
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
# Just one example of many...
- name: my-app-config
literals:
- JAVA_HOME=/opt/java/jdk
- JAVA_TOOL_OPTIONS=-agentlib:hprof
# Explanation below...
- SECRETS_VERSION=1
然后在部署中引用my-app-config即可。当使用kustomize构建时,它会自动查找并更新my-app-config的引用,并使用更新的后缀,例如my-app-config-f7mm6mhf59。
额外的好处是,更新秘密:我还使用此技术强制重新加载秘密(因为它们以同样的方式受到影响)。虽然我个人完全单独管理我的秘密(使用Mozilla sops),但您可以将配置映射与您的秘密捆绑在一起,例如在您的部署中:
# ...
spec:
template:
spec:
containers:
- name: my-app
image: my-app:tag
envFrom:
# For any NON-secret environment variables. Name is automatically updated by Kustomize
- configMapRef:
name: my-app-config
# Defined separately OUTSIDE of Kustomize. Just modify SECRETS_VERSION=[number] in the my-app-config ConfigMap
# to trigger an update in both the config as well as the secrets (since the pod will get restarted).
- secretRef:
name: my-app-secrets
然后,像我上面所做的那样,在ConfigMap中添加一个像SECRETS_VERSION这样的变量。然后,每次更改my-app-secrets时,只需增加SECRETS_VERSION的值,这除了触发kustomize'd ConfigMap名称的更改外,没有其他用途,这也应该导致重新启动pod。然后就变成: