我一直在尝试弄清楚如何通过Kustomize使用使用ConfigMap生成器创建的ConfigMap。
当使用Kustomize生成器创建时,configMaps会带有特殊后缀。请参见此处: 问题是如何引用这个?
当使用Kustomize生成器创建时,configMaps会带有特殊后缀。请参见此处: 问题是如何引用这个?
您无需自己引用它。Kustomize会识别其他资源(比如Deployment)中使用的configMap,并更改这些引用以使用name+hash。
这样做的原因是,如果更改了configMap,Kustomize会生成一个新的哈希并更新Deployment,导致Pods进行滚动重启。
如果您不想要此行为,可以将以下内容添加到kustomization.yaml
文件中:
generatorOptions:
disableNameSuffixHash: true
kubectl apply -k .
时,会创建一个名为game-config-4-m9dm2f92bt
的配置映射。
您可以通过以下方式检查已创建的ConfigMap:kubectl get configmap
。此ConfigMap将包含一个数据字段,其中包含您提供的数据。apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
env:
# Define the environment variable
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
# The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
name: special-config
# Specify the key associated with the value
key: special.how
restartPolicy: Never
apiVersion: v1
kind: Pod
metadata:
name: configmap-demo-pod
spec:
containers:
- name: demo
image: alpine
command: ["sleep", "3600"]
env:
# Define the environment variable
- name: PLAYER_INITIAL_LIVES # Notice that the case is different here
# from the key name in the ConfigMap.
valueFrom:
configMapKeyRef:
name: game-demo # The ConfigMap this value comes from.
key: player_initial_lives # The key to fetch.
- name: UI_PROPERTIES_FILE_NAME
valueFrom:
configMapKeyRef:
name: game-demo
key: ui_properties_file_name
volumeMounts:
- name: config
mountPath: "/config"
readOnly: true
volumes:
# You set volumes at the Pod level, then mount them into containers inside that Pod
- name: config
configMap:
# Provide the name of the ConfigMap you want to mount.
name: game-demo
# An array of keys from the ConfigMap to create as files
items:
- key: "game.properties"
path: "game.properties"
- key: "user-interface.properties"
path: "user-interface.properties
我也曾经为此苦恼。我无法弄清楚为什么kustomize没有更新部署中卷的configmap名称,以包括哈希值。对我有用的解决方法是在kustomization.yaml中为基础和叠加层都添加namespace: <namespace>
。
kustomization.file
之外都删除了namespace
,这解决了问题。在基本的kustomization.yaml
文件中,将configMapGenerator[0].name
设置为my-configmap
,最终在我的部署中,configMapKeyRef.name
正确设置为dev-my-configmap-g75875hhh9
,而不是错误地设置为my-configmap
。 - undefined