如何使用Helm从列表中迭代创建吊舱?

时间:2018-06-25 12:58:50

标签: kubernetes yaml kubernetes-helm

我正在尝试通过头盔中的yaml循环创建许多吊舱。如果我使用--debug --dry-run运行,则输出符合我的期望,但是当我实际部署到集群时,只会显示循环的最后一次迭代。

一些Yaml:

{{ 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
---

(由于敏感度/不相关性,某些部分已被编辑/删除)

在我看来,它确实满足了我的要求,即为a创建一个广告连播,为b创建一个广告连播,为c创建一个广告连播。

但是,当实际将其安装到集群中时,我总是只得到与列表中最后一个元素相对应的pod。 (在这种情况下,c)几乎就像它们相互覆盖一样,但是鉴于它们具有不同的名称,我认为它们不应该吗?即使运行--debug而不运行--dry-run,输出也告诉我应该有3个吊舱,但是使用kubectl get pods只能看到一个吊舱。

如何使用Helm从列表中迭代创建吊舱?

1 个答案:

答案 0 :(得分:3)

找到了!

显然,头盔使用---作为pod / services / whatHaveYou规范之间的分隔符。

在单个图表中多次指定相同字段是有效的,它将对任何给定字段使用最后指定的值。为了避免覆盖值,而是创建多个Pod,只需在循环末尾添加分隔符即可:

{{ 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 }}