35

目前我正在通过执行以下命令从文件 config.json 创建一个配置映射:

kubectl create configmap jksconfig --from-file=config.json

我希望将 ConfigMap作为部署的一部分创建并尝试这样做:

apiVersion: v1
kind: ConfigMap
metadata:
  name: jksconfig
data:
  config.json: |-
    {{ .Files.Get "config.json" | indent 4 }}

但似乎不起作用。什么应该进入 configmap.yaml 以便创建相同的 configmap?

- -更新 - -

当我进行 helm install 试运行时:

# Source: mychartv2/templates/jks-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: jksconfig
data:
  config.json: |

注意:我使用 minikube 作为我的 kubernetes 集群

4

3 回答 3

51

您的config.json文件应该在mychart/目录中,而不是在 mychart/templates中

图表模板指南

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  config.json: |-
{{ .Files.Get "config.json" | indent 4}}

配置文件

{
    "val": "key"
}

helm install --dry-run --debug mychart

[debug] Created tunnel using local port: '52091'     
                                                     
[debug] SERVER: "127.0.0.1:52091"                    
                                                     
...           
                                                     
NAME:   dining-saola                                 
REVISION: 1                                          
RELEASED: Fri Nov 23 15:06:17 2018                   
CHART: mychart-0.1.0                                 
USER-SUPPLIED VALUES:                                
{}                                                   
                                                     
...
                                                     
---                                                  
# Source: mychart/templates/configmap.yaml           
apiVersion: v1                                       
kind: ConfigMap                                      
metadata:                                            
  name: dining-saola-configmap                       
data:                                                
  config.json: |-                                    
    {                                                
        "val": "key"                                 
    }     

编辑:

但我希望 config.json 文件中的值取自 values.yaml。那可能吗?

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  config.json: |-
    {
{{- range $key, $val := .Values.json }}
{{ $key | quote | indent 6}}: {{ $val | quote }}
{{- end}}
    }

值.yaml

json:
  key1: val1
  key2: val2
  key3: val3

helm install --dry-run --debug mychart

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mangy-hare-configmap
data:
  config.json: |-
    {
      "key1": "val1"
      "key2": "val2"
      "key3": "val3"
    }
于 2018-11-23T13:07:29.827 回答
8

ConfigMap这是附加到 a的示例Deployment

配置图:

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: jksconfig
data:
  config.json: |-
{{ .Files.Get "config.json" | indent 4 }}

部署:

---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: jksapp
  labels:
    app: jksapp
spec:
  selector:
    matchLabels:
      app: jksapp
  template:
    metadata:
      labels:
        app: jksapp
      containers:
        - name: jksapp
          image: jksapp:1.0.0
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: config #The name(key) value must match pod volumes name(key) value 
              mountPath: /path/to/config.json
      volumes:
        - name: config
          configMap:
            name: jksconfig
于 2018-11-22T11:56:34.207 回答
3

索尔 01:

  • config.json文件内容插入模板
  • 然后将此模板用于您的data反对config.json
  • 然后运行$ helm install命令

最后,

{{define "config"}}
{
    "a": "A",
    "b": {
        "b1": 1
    }
}
{{end}}

apiVersion: v1
kind: ConfigMap
metadata:
  name: jksconfig
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app: "my-app"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
data:
  config.json: {{ (include "config" .) | trim | quote }}
于 2018-11-22T18:00:52.867 回答