问题描述
我正在尝试通过头盔中的yaml循环创建许多吊舱.如果我使用--debug --dry-run
运行,则输出符合我的期望,但是当我实际部署到集群时,仅显示循环的最后一次迭代.
I'm trying to create a number of pods from a yaml loop in helm. if I run with --debug --dry-run
the output matches my expectations, but when I actually deploy to to a cluster, only the last iteration of the loop is present.
为您准备的一些Yaml:
some yaml for you:
{{ if .Values.componentTests }}
{{- range .Values.componentTests }}
apiVersion: v1
kind: Pod
metadata:
name: {{ . }}
labels:
app: {{ . }}
chart: {{ $.Chart.Name }}-{{ $.Chart.Version | replace "+" "_" }}
release: {{ $.Release.Name }}
heritage: {{ $.Release.Service }}
spec:
{{ toYaml $.Values.global.podSpec | indent 2 }}
restartPolicy: Never
containers:
- name: {{ . }}
ports:
- containerPort: 3000
image: 123456789012.dkr.ecr.eu-west-1.amazonaws.com/{{ . }}:latest
imagePullPolicy: Always
command: ["sleep"]
args: ["100d"]
resources:
requests:
memory: 2000Mi
cpu: 500m
{{- end }}
{{ end }}
当我运行helm upgrade --install --set componentTests="{a,b,c}" --debug --dry-run
我得到以下输出:
# Source: <path-to-file>.yaml
apiVersion: v1
kind: Pod
metadata:
name: a
labels:
app: a
chart: integrationtests-0.0.1
release: funny-ferret
heritage: Tiller
spec:
restartPolicy: Never
containers:
- name: content-tests
ports:
- containerPort: 3000
image: 123456789012.dkr.ecr.eu-west-1.amazonaws.com/a:latest
imagePullPolicy: Always
command: ["sleep"]
args: ["100d"]
resources:
requests:
memory: 2000Mi
cpu: 500m
apiVersion: v1
kind: Pod
metadata:
name: b
labels:
app: b
chart: integrationtests-0.0.1
release: funny-ferret
heritage: Tiller
spec:
restartPolicy: Never
containers:
- name: b
ports:
- containerPort: 3000
image: 123456789012.dkr.ecr.eu-west-1.amazonaws.com/b:latest
imagePullPolicy: Always
command: ["sleep"]
args: ["100d"]
resources:
requests:
memory: 2000Mi
cpu: 500m
apiVersion: v1
kind: Pod
metadata:
name: c
labels:
app: users-tests
chart: integrationtests-0.0.1
release: funny-ferret
heritage: Tiller
spec:
restartPolicy: Never
containers:
- name: c
ports:
- containerPort: 3000
image: 123456789012.dkr.ecr.eu-west-1.amazonaws.com/c:latest
imagePullPolicy: Always
command: ["sleep"]
args: ["100d"]
resources:
requests:
memory: 2000Mi
cpu: 500m
---
(由于敏感度/不相关性,某些部分已被编辑/删除)
(some parts have been edited/removed due to sensitivity/irrelevance)
就像我做的一样,即为a
创建一个pod,为b
创建另一个pod,为c
创建第三个pod.
which looks to me like I it does what I want it to, namely create a pod for a
another for b
and a third for c
.
但是,当实际将其安装到集群中时,我总是只得到与列表中最后一个元素相对应的pod. (在这种情况下为c
)几乎就像它们相互覆盖一样,但是鉴于它们具有不同的名称,我不认为它们应该吗?即使使用--debug
但不使用--dry-run
运行,输出也会告诉我我应该有3个吊舱,但是使用kubectl get pods
我只能看到一个吊舱.
however, when actually installing this into a cluster, I always only end up with the pod corresponding to the last element in the list. (in this case, c
) it's almost as if they overwrite each other, but given that they have different names I don't think they should? even running with --debug
but not --dry-run
the output tells me I should have 3 pods, but using kubectl get pods
I can see only one.
如何使用Helm从列表中迭代创建吊舱?
How can I iteratively create pods from a list using Helm?
推荐答案
找到了!
因此,很明显,头盔使用---
作为pods/services/whatHaveYou规范之间的分隔符.
so apparently, helm uses ---
as a separator between specifications of pods/services/whatHaveYou.
在单个图表中多次指定相同字段是有效的,它将为任何给定字段使用最后指定的值.为了避免覆盖值,而是创建了多个Pod,只需在循环末尾添加分隔符即可:
specifying the same fields multiple times in a single chart is valid, it will use the last specified value for for any given field. To avoid overwriting values and instead have multiple pods created, simply add the separator at the end of the loop:
{{ if .Values.componentTests }}
{{- range .Values.componentTests }}
apiVersion: v1
kind: Pod
metadata:
name: {{ . }}
labels:
app: {{ . }}
chart: {{ $.Chart.Name }}-{{ $.Chart.Version | replace "+" "_" }}
release: {{ $.Release.Name }}
heritage: {{ $.Release.Service }}
spec:
{{ toYaml $.Values.global.podSpec | indent 2 }}
restartPolicy: Never
containers:
- name: {{ . }}
ports:
- containerPort: 3000
image: 123456789012.dkr.ecr.eu-west-1.amazonaws.com/{{ . }}:latest
imagePullPolicy: Always
command: ["sleep"]
args: ["100d"]
resources:
requests:
memory: 2000Mi
cpu: 500m
---
{{- end }}
{{ end }}
这篇关于如何使用Helm从列表中迭代创建Pod?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!