helm-chart-读取配置文件到ConfigMap并映射到容器中

5 篇文章 0 订阅

我们以一个项目例子来说明,项目文件结构如下:

$ tree
.
├── Chart.yaml
├── etc
│ └── data.txt
├── etc-values.yaml
└── templates
├── configmap.yaml
├── deployment.yaml
├── _helpers.tpl
├── NOTES.txt
└── service.yaml
准备测试用的配置文件:

$ ./etc/data.txt
username: a1
age: 30
在configMap中配置读取etc/目录下所有文件:

$ cat templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-template
data:
{{ (.Files.Glob “etc/*”).AsConfig | indent 2 }}
把configMap映射到容器中的/data/etc目录:

$ cat ./templates/deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: {{ template “fullname” . }}
labels:
app: {{ template “name” . }}
chart: {{ .Chart.Name }}-{{ .Chart.Version | replace “+” “_” }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
spec:
replicas: {{ .Values.replicaCount }}
template:
metadata:
labels:
app: {{ template “name” . }}
release: {{ .Release.Name }}
spec:
imagePullSecrets:
- name: {{ .Values.image.secret }}
containers:
- name: {{ .Chart.Name }}
image: “{{ .Values.image.repository }}:{{ .Values.image.tag }}”
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: [“sleep”]
args: [“5000”]
env:
{{- range .Values.environment }}
{{- range k e y , key, key,val := . }}
- name : {{ $key }}
value : {{ $val | quote }}
{{- end}}
{{- end}}
volumeMounts:
- mountPath: /data/etc
name: data-etc
ports:
- containerPort: {{ .Values.service.internalPort }}

  volumes:
  - name: data-etc
    configMap:
      name: {{ .Release.Name }}-template

发布项目:

$ helm upgrade --install --wait -f ./etc-values.yaml etc1 .
Release “etc1” does not exist. Installing it now.
NAME: etc1
LAST DEPLOYED: Thu Aug 16 07:45:50 2018
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/ConfigMap
NAME DATA AGE
etc1-template 1 3s

==> v1/Service
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
etc1-etc 10.254.237.234 33333/TCP 3s

==> v1beta1/Deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
etc1-etc 1 1 1 0 3s

NOTES:
Application will be accessible at: when you deploy stable track.
查看pod的运行状态:

$ kubectl get svc,po |grep etc
service/etc1-etc ClusterIP 10.254.237.234 33333/TCP 1m

pod/etc1-etc-5df796cf8f-4zfm9 2/2 Running 0 1m
进入容器检查文件是否映射成功:

$ kubectl exec -it etc1-etc-5df796cf8f-4zfm9 -c etc sh

cat /data/etc/data.txt

username: a1
age: 30

完整例子:https://github.com/4220182/k8s/tree/master/example/helm/external-etc

参考:https://docs.helm.sh/chart_template_guide/#glob-patterns

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值