使用在Kustomize/Kubernetes中使用发生器创建的配置图

9rnv2umw  于 2022-12-17  发布在  Kubernetes
关注(0)|答案(3)|浏览(130)

我一直在试图弄清楚如何通过Kustomize使用ConfigMap生成器创建ConfigMap。
当使用自定义生成器创建时,configMap将使用特殊后缀命名。
https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#create-a-configmap-from-generator
问题是这怎么能被引用呢?

omhiaaxx

omhiaaxx1#

您自己不引用它,Kustomize识别configMap在其他资源(如部署)中的使用位置,并将这些引用更改为使用name+hash。
这样做的原因是,如果您更改configMap,Kustomize将生成新的哈希并更新部署,从而导致Pod滚动重启。
如果不希望出现这种情况,可以将以下代码添加到kustomization.yaml文件中:

generatorOptions:
  disableNameSuffixHash: true
wsewodh2

wsewodh22#

它在文档中指定。当您执行kubectl apply -k .时,将创建名为game-config-4-m9dm2f92bt的配置Map。您可以检查是否按如下方式创建了配置Map:kubectl get configmap。此ConfigMap将包含给定数据所属的字段数据。
现在,像往常一样,您可以在pod中使用此配置Map,如下所示:
来自k8s的Ex:

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

你可以看到k8s官方的doc

l7wslrjt

l7wslrjt3#

我也很纠结这个问题,我不明白为什么kustomize没有更新部署中卷的configmap名称来包含哈希,我解决这个问题的方法是在kustomization.yaml中为基础和覆盖添加namespace: <namespace>

相关问题