我们以一个项目例子来说明,项目文件结构如下:
$ 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
username: a1
age: 30
完整例子:https://github.com/4220182/k8s/tree/master/example/helm/external-etc
参考:https://docs.helm.sh/chart_template_guide/#glob-patterns