使用 yaml 文件在 kubernetes 上创建多个配置映射
Create multiple configmaps on kubernetes with using yaml files
我有几个包含一些值的 yaml 文件。我想在 helm 部署时读取这些文件并为每个文件创建配置映射。
我在 helm 图表下添加了配置文件。 (与模板文件夹同级)
chart structure
然后我尝试创建位于 'templates' 文件夹下的 'configmap-creator.yaml'。
我只是运行'helm upgrade --install ealpkar --namespace ealpkar --create-namespace .'
它已成功完成,但只有一个名为 'config2-configmap' 的配置映射。我错过了第一个 ( config1-configmap )
这是'configmap-creator.yaml'
{{- $files := .Files }}
{{- range $key, $value := .Files }}
{{- if hasPrefix "config/" $key }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $key | trimPrefix "config/" | replace ".yaml" "" | replace "_" "-" }}-configmap
data:
{{ $key | trimPrefix "config/" }}: {{ $files.Get $key | quote }}
{{- end }}
{{- end }}
'config' 文件夹下的 yaml 文件示例;
config1.yaml
dummy_product:
ip: 10.10.10.10
port: 22
config2.yaml
dummy_product_2:
ip: 10.10.10.20
port: 22
修复您的模板,在对象之间添加分隔符。
{{- $files := .Files }}
{{- range $key, $value := .Files }}
{{- if hasPrefix "config/" $key }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $key | trimPrefix "config/" | replace ".yaml" "" | replace "_" "-" }}-configmap
data:
{{ $key | trimPrefix "config/" }}: {{ $files.Get $key | quote }}
{{- end }}
{{- end }}
我有几个包含一些值的 yaml 文件。我想在 helm 部署时读取这些文件并为每个文件创建配置映射。
我在 helm 图表下添加了配置文件。 (与模板文件夹同级)
chart structure
然后我尝试创建位于 'templates' 文件夹下的 'configmap-creator.yaml'。
我只是运行'helm upgrade --install ealpkar --namespace ealpkar --create-namespace .' 它已成功完成,但只有一个名为 'config2-configmap' 的配置映射。我错过了第一个 ( config1-configmap )
这是'configmap-creator.yaml'
{{- $files := .Files }}
{{- range $key, $value := .Files }}
{{- if hasPrefix "config/" $key }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $key | trimPrefix "config/" | replace ".yaml" "" | replace "_" "-" }}-configmap
data:
{{ $key | trimPrefix "config/" }}: {{ $files.Get $key | quote }}
{{- end }}
{{- end }}
'config' 文件夹下的 yaml 文件示例;
config1.yaml
dummy_product: ip: 10.10.10.10 port: 22
config2.yaml
dummy_product_2: ip: 10.10.10.20 port: 22
修复您的模板,在对象之间添加分隔符。
{{- $files := .Files }}
{{- range $key, $value := .Files }}
{{- if hasPrefix "config/" $key }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $key | trimPrefix "config/" | replace ".yaml" "" | replace "_" "-" }}-configmap
data:
{{ $key | trimPrefix "config/" }}: {{ $files.Get $key | quote }}
{{- end }}
{{- end }}