Kubernetes

如何在 Kustomize YAML 文件中載入 ConfigMap?

  • October 7, 2021

我原來的 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

從 base 的父文件夾:

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 正在被丟棄,請建議如何解決該問題。

在 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 baseConfigMap 將在輸出的頂部生成,即使它是在資源文件的底部定義的。檢查下面。

命令的輸出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

引用自:https://serverfault.com/questions/1079854