Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kubernetes 使用Helm访问k8s configmap中的配置文件时,数据为空_Kubernetes_Kubernetes Helm_Configmap - Fatal编程技术网

Kubernetes 使用Helm访问k8s configmap中的配置文件时,数据为空

Kubernetes 使用Helm访问k8s configmap中的配置文件时,数据为空,kubernetes,kubernetes-helm,configmap,Kubernetes,Kubernetes Helm,Configmap,我正在尝试在我的部署中使用配置图和头盔图表。现在似乎可以使用Helm根据以下文档访问文件: 这是我的部署: kind: Deployment apiVersion: extensions/v1beta1 metadata: name: "{{ template "service.fullname" . }}" labels: chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" spec: replicas: {{ .

我正在尝试在我的部署中使用配置图和头盔图表。现在似乎可以使用Helm根据以下文档访问文件:

这是我的部署:

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: "{{ template "service.fullname" . }}"
  labels:        
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: "{{ template "service.fullname" . }}"
    spec:
      containers:
      - name: "{{ .Chart.Name }}"
        image: "{{ .Values.registryHost }}/{{ .Values.userNamespace }}/{{ .Values.projectName }}/{{ .Values.serviceName }}:{{.Chart.Version}}"
        volumeMounts:
        - name: {{ .Values.configmapName}}configmap-volume
          mountPath: /app/config
        ports:
        - containerPort: 80
          name: http            
        livenessProbe:
          httpGet:
            path: /health
            port: http
          initialDelaySeconds: 10
          timeoutSeconds: 5
        readinessProbe:
          httpGet:
            path: /health
            port: http
          initialDelaySeconds: 10
          timeoutSeconds: 5            
      volumes:
        - name: {{ .Values.configmapName}}configmap-volume
          configMap:            
            name: "{{ .Values.configmapName}}-configmap"
我的configmap正在访问配置文件。以下是配置图:

apiVersion: v1
kind: ConfigMap
metadata:
  name: "{{ .Values.configmapName}}-configmap"
  labels:
    app: "{{ .Values.configmapName}}"
data:
  {{ .Files.Get "files/{{ .Values.configmapName}}-config.json" | indent 2}}
图表目录如下所示:

files/
--runtime-config.json
templates/
--configmap.yaml
--deployment.yaml
--ingress.yaml
--service.yaml
chart.value
vaues.yaml
这就是我的runtime-confi.json文件的样子:

{
    "GameModeConfiguration": {
        "command": "xx",
        "modeId": 10,
        "sessionId": 11            
    }
}
问题是,当我安装图表时(即使在干运行模式下),configmap的数据是空的。它不会将配置文件中的数据添加到我的configmap声明中。这是我进行试运行时的样子:

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: "runtime-configmap"
  labels:
    app: "runtime"
data:
---

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: "whimsical-otter-runtime-service"
  labels:        
    chart: "runtime-service-unknown/version"
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: "whimsical-otter-runtime-service"
    spec:
      containers:
      - name: "runtime-service"
        image: "gcr.io/xxx-dev/xxx/runtime_service:unknown/version"
        volumeMounts:
        - name: runtimeconfigmap-volume
          mountPath: /app/config
        ports:
        - containerPort: 80
          name: http
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 100m
            memory: 100Mi

        livenessProbe:
          httpGet:
            path: /health
            port: http
          initialDelaySeconds: 10
          timeoutSeconds: 5
        readinessProbe:
          httpGet:
            path: /health
            port: http
          initialDelaySeconds: 10
          timeoutSeconds: 5

      volumes:
        - name: runtimeconfigmap-volume
          configMap:            
            name: "runtime-configmap"
---

我没有获取数据,这有什么错?

替换字符串中的变量不起作用:

{{ .Files.Get "files/{{ .Values.configmapName}}-config.json" | indent 2}}
但是您可以使用
printf
函数生成字符串,如下所示:

{{ .Files.Get (printf "files/%s-config.json" .Values.configmapName) | indent 2 }}

替换字符串中的变量无效:

{{ .Files.Get "files/{{ .Values.configmapName}}-config.json" | indent 2}}
但是您可以使用
printf
函数生成字符串,如下所示:

{{ .Files.Get (printf "files/%s-config.json" .Values.configmapName) | indent 2 }}

除了@adebasi指出的语法问题外,您还需要在密钥中设置此代码以获得有效的configmap yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: "{{ .Values.configmapName}}-configmap"
  labels:
    app: "{{ .Values.configmapName}}"
data:
  my-file: |
    {{ .Files.Get (printf "files/%s-config.json" .Values.configmapName) | indent 4}}
或者您可以使用方便的configmap帮助程序:

apiVersion: v1
kind: ConfigMap
metadata:
  name: "{{ .Values.configmapName}}-configmap"
  labels:
    app: "{{ .Values.configmapName}}"
data:
{{ (.Files.Glob "files/*").AsConfig | indent 2 }}

除了@adebasi指出的语法问题外,您还需要在密钥中设置此代码以获得有效的configmap yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: "{{ .Values.configmapName}}-configmap"
  labels:
    app: "{{ .Values.configmapName}}"
data:
  my-file: |
    {{ .Files.Get (printf "files/%s-config.json" .Values.configmapName) | indent 4}}
或者,您可以使用方便的configmap帮助程序:

apiVersion: v1
kind: ConfigMap
metadata:
  name: "{{ .Values.configmapName}}-configmap"
  labels:
    app: "{{ .Values.configmapName}}"
data:
{{ (.Files.Glob "files/*").AsConfig | indent 2 }}

是的,这就是问题所在。我不知道字符串替换如何在Go模板中正常工作。是的,这就是问题所在。我不知道字符串替换如何在Go模板中正常工作。是的,你是对的。字符串替换问题。第二个解决方案很简单。谢谢是的,你是对的。字符串替换问题。第二个解决方案很简单。谢谢