在Kustomize/Kubernetes中使用生成器创建ConfigMap

3
3个回答

3

您无需自己引用它。Kustomize会识别其他资源(比如Deployment)中使用的configMap,并更改这些引用以使用name+hash。

这样做的原因是,如果更改了configMap,Kustomize会生成一个新的哈希并更新Deployment,导致Pods进行滚动重启。

如果您不想要此行为,可以将以下内容添加到kustomization.yaml文件中:

generatorOptions:
  disableNameSuffixHash: true

1
在文档中有说明。当您执行kubectl apply -k .时,会创建一个名为game-config-4-m9dm2f92bt的配置映射。 您可以通过以下方式检查已创建的ConfigMap:kubectl get configmap。此ConfigMap将包含一个数据字段,其中包含您提供的数据。
现在,像往常一样,您可以在Pod中使用此configmap。如下所示:
来自k8s的示例:
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

您可以将ConfigMap作为卷使用,就像k8s文档中的这个示例一样:
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

你可以查看Kubernetes官方文档

1
这似乎没有起作用。ConfigMap的名称是'game-config-4-m9dm2f92bt'。但你引用的是'game-config'。你是在告诉我,当搜索'game-config'时,你的安装找到了'game-config-4-m9dm2f92bt'吗? - undefined
问题是如何以编程方式完成这个任务。创建代码使用了 'game-config-4',但实际上会创建 'game-config-4-m9dm2f92bt'。而消费代码也应该引用 'game-config-4',而不事先知道可能添加的标签。是否有类似于 'game-config-4:latest' 或带有某个时间段的限定条件? - undefined

1

我也曾经为此苦恼。我无法弄清楚为什么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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接