如何在 Kustomize YAML 文件中加载 ConfigMap?

如何在 Kustomize YAML 文件中加载 ConfigMap?

我原来的 YAML

基础/部署.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - image: nginx
          name: nginx
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: database-configmap
data:
  config: |
    dbport=1234
    dcname=sfsdf
    dbssl=false
    locktime=300
    domainuser=

基础/Kustomization.yaml

resources:
  - deployment.yaml

commonLabels:
  owner: sara

从基础文件夹的父文件夹中:

kustomize build base

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
    owner: sara
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
      owner: sara
  template:
    metadata:
      labels:
        app: nginx
        owner: sara
    spec:
      containers:
      - image: nginx
        name: nginx

如果您观察到上述情况,ConfigMap 被丢弃,请建议如何解决这个问题。

答案1

在 Kustomize 的两个版本中(当前最新版本(v.4.4.0)和v4.1.3问题中使用的版本)它都可以正常工作。作者注意到重新启动可能会有所帮助:

重启机器后,它就可以正常工作了。

请记住两件事:

  • base/Kustomization.yaml名称无法使用;您将收到错误Error: unable to find one of 'kustomization.yaml', 'kustomization.yml' or 'Kustomization' in directory。需要使用正确的名称。
  • 运行命令后:kustomize build base即使 ConfigMap 是在资源文件底部定义的,它也会在输出的顶部生成。请检查以下内容。

命令输出kustomize build base

apiVersion: v1
data:
  config: |
    dbport=1234
    dcname=sfsdf
    dbssl=false
    locktime=300
    domainuser=
kind: ConfigMap
metadata:
  labels:
    owner: sara
  name: database-configmap
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
    owner: sara
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
      owner: sara
  template:
    metadata:
      labels:
        app: nginx
        owner: sara
    spec:
      containers:
      - image: nginx
        name: nginx

相关内容