Helm pod 部署循环

问题描述 投票:0回答:1

我有 50 个城市的列表,尝试重用相同的部署模板来启动 5 个 Pod,每个 Pod 包含 10 个容器(代表 10 个城市)。

我成功地为每个城市创建了一个 Pod(50 个 Pod)部署。无法弄清楚如何为每个 Pod 划分 10 个城市。

示例代码

{{ if .Values.enabled }}
{{- range $i, $value := .Values.cities }}
kind: Pod
apiVersion: v1
metadata:
  name: "nginx-blob-{{ . }}"
spec:
  nodeSelector:
    "kubernetes.io/os": linux
  containers:
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-{{ $value }}"

---
{{- end }}
{{ end }}
kubernetes split kubernetes-helm nested-loops
1个回答
0
投票

对于初学者,您将需要一个嵌套循环——一个用于创建多个 Pod,然后在每个 Pod 中您需要一个循环来创建多个容器。

对于外层循环,我们可以使用

untilStep
以 10 为步长循环城市数量,如下所示:

{{- $cityCount := len .Values.cities }}
{{- range $beginningOfRange := untilStep 0 $cityCount 10 }}
beginningOfRange: {{ $beginningOfRange }}
{{end}}

假设我们的列表中有 50 个城市,这让我们:

---
# Source: example/templates/pod.yaml
beginningOfRange: 0

beginningOfRange: 10

beginningOfRange: 20

beginningOfRange: 30

beginningOfRange: 40

对于内循环,我们需要从

$beginningOfRange
$beginningOfRange + 10
,我们可以这样做:

{{- $cityCount := len .Values.cities }}
{{- range $beginningOfRange := untilStep 0 $cityCount 10 }}
beginningOfRange: {{ $beginningOfRange }}
cities:
{{- $endOfRange := add $beginningOfRange 10 | int}}
{{- range $cityNumber := untilStep $beginningOfRange $endOfRange 1 }}
- cityNumber: {{ $cityNumber }}
{{- end }}
{{end}}

这让我们:

---
# Source: example/templates/pod.yaml
beginningOfRange: 0
cities:
- cityNumber: 0
- cityNumber: 1
- cityNumber: 2
- cityNumber: 3
- cityNumber: 4
- cityNumber: 5
- cityNumber: 6
- cityNumber: 7
- cityNumber: 8
- cityNumber: 9

beginningOfRange: 10
cities:
- cityNumber: 10
- cityNumber: 11
- cityNumber: 12
- cityNumber: 13
- cityNumber: 14
- cityNumber: 15
- cityNumber: 16
- cityNumber: 17
- cityNumber: 18
- cityNumber: 19

beginningOfRange: 20
cities:
- cityNumber: 20
- cityNumber: 21
- cityNumber: 22
- cityNumber: 23
- cityNumber: 24
- cityNumber: 25
- cityNumber: 26
- cityNumber: 27
- cityNumber: 28
- cityNumber: 29

.
.
.

如您所见,我们现在有了模板的基本结构。如果我们修改您的原始模板,我们会得到如下内容:

{{- $cityCount := len .Values.cities }}
{{- range $beginningOfRange := untilStep 0 $cityCount 10 }}
kind: Pod
apiVersion: v1
metadata:
  name: "nginx-blob-{{ $beginningOfRange }}"
spec:
  containers:
{{- $endOfRange := add $beginningOfRange 10 | int}}
{{- range $cityNumber := untilStep $beginningOfRange $endOfRange 1 }}
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-{{ $cityNumber }}"
      env:
        - name: CITY_NAME
          value: "{{ index $.Values.cities $cityNumber }}"
{{- end }}
---
{{end}}

假设

.Values.cities
有美国州首府列表,这将生成如下输出:

---
# Source: example/templates/pod.yaml
kind: Pod
apiVersion: v1
metadata:
  name: "nginx-blob-0"
spec:
  containers:
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-0"
      env:
        - name: CITY_NAME
          value: "Montgomery, Alabama"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-1"
      env:
        - name: CITY_NAME
          value: "Juneau, Alaska"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-2"
      env:
        - name: CITY_NAME
          value: "Phoenix, Arizona"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-3"
      env:
        - name: CITY_NAME
          value: "Little-Rock, Arkansas"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-4"
      env:
        - name: CITY_NAME
          value: "Sacramento, California"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-5"
      env:
        - name: CITY_NAME
          value: "Denver, Colorado"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-6"
      env:
        - name: CITY_NAME
          value: "Hartford, Connecticut"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-7"
      env:
        - name: CITY_NAME
          value: "Dover, Delaware"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-8"
      env:
        - name: CITY_NAME
          value: "Tallahassee, Florida"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-9"
      env:
        - name: CITY_NAME
          value: "Atlanta, Georgia"
---
# Source: example/templates/pod.yaml
kind: Pod
apiVersion: v1
metadata:
  name: "nginx-blob-10"
spec:
  containers:
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-10"
      env:
        - name: CITY_NAME
          value: "Honolulu, Hawaii"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-11"
      env:
        - name: CITY_NAME
          value: "Boise, Idaho"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-12"
      env:
        - name: CITY_NAME
          value: "Springfield, Illinois"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-13"
      env:
        - name: CITY_NAME
          value: "Indianapolis, Indiana"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-14"
      env:
        - name: CITY_NAME
          value: "Des-Moines, Iowa"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-15"
      env:
        - name: CITY_NAME
          value: "Topeka, Kansas"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-16"
      env:
        - name: CITY_NAME
          value: "Frankfort, Kentucky"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-17"
      env:
        - name: CITY_NAME
          value: "Baton-Rouge, Louisiana"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-18"
      env:
        - name: CITY_NAME
          value: "Augusta, Maine"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-19"
      env:
        - name: CITY_NAME
          value: "Annapolis, Maryland"
---
# Source: example/templates/pod.yaml
kind: Pod
apiVersion: v1
metadata:
  name: "nginx-blob-20"
spec:
  containers:
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-20"
      env:
        - name: CITY_NAME
          value: "Boston, Massachusetts"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-21"
      env:
        - name: CITY_NAME
          value: "Lansing, Michigan"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-22"
      env:
        - name: CITY_NAME
          value: "St.-Paul, Minnesota"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-23"
      env:
        - name: CITY_NAME
          value: "Jackson, Mississippi"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-24"
      env:
        - name: CITY_NAME
          value: "Jefferson-City, Missouri"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-25"
      env:
        - name: CITY_NAME
          value: "Helena, Montana"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-26"
      env:
        - name: CITY_NAME
          value: "Lincoln, Nebraska"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-27"
      env:
        - name: CITY_NAME
          value: "Carson-City, Nevada"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-28"
      env:
        - name: CITY_NAME
          value: "Concord, New-Hampshire"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-29"
      env:
        - name: CITY_NAME
          value: "Trenton, New-Jersey"
---
# Source: example/templates/pod.yaml
kind: Pod
apiVersion: v1
metadata:
  name: "nginx-blob-30"
spec:
  containers:
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-30"
      env:
        - name: CITY_NAME
          value: "Santa-Fe, New-Mexico"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-31"
      env:
        - name: CITY_NAME
          value: "Albany, New-York"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-32"
      env:
        - name: CITY_NAME
          value: "Raleigh, North-Carolina"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-33"
      env:
        - name: CITY_NAME
          value: "Bismarck, North-Dakota"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-34"
      env:
        - name: CITY_NAME
          value: "Columbus, Ohio"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-35"
      env:
        - name: CITY_NAME
          value: "Oklahoma-City, Oklahoma"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-36"
      env:
        - name: CITY_NAME
          value: "Salem, Oregon"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-37"
      env:
        - name: CITY_NAME
          value: "Harrisburg, Pennsylvania"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-38"
      env:
        - name: CITY_NAME
          value: "Providence, Rhode-Island"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-39"
      env:
        - name: CITY_NAME
          value: "Columbia, South-Carolina"
---
# Source: example/templates/pod.yaml
kind: Pod
apiVersion: v1
metadata:
  name: "nginx-blob-40"
spec:
  containers:
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-40"
      env:
        - name: CITY_NAME
          value: "Pierre, South-Dakota"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-41"
      env:
        - name: CITY_NAME
          value: "Nashville, Tennessee"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-42"
      env:
        - name: CITY_NAME
          value: "Austin, Texas"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-43"
      env:
        - name: CITY_NAME
          value: "Salt-Lake-City, Utah"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-44"
      env:
        - name: CITY_NAME
          value: "Montpelier, Vermont"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-45"
      env:
        - name: CITY_NAME
          value: "Richmond, Virginia"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-46"
      env:
        - name: CITY_NAME
          value: "Olympia, Washington"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-47"
      env:
        - name: CITY_NAME
          value: "Charleston, West-Virginia"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-48"
      env:
        - name: CITY_NAME
          value: "Madison, Wisconsin"
    - image: mcr.microsoft.com/oss/nginx/nginx:1.17.3-alpine
      name: "nginx-blob-49"
      env:
        - name: CITY_NAME
          value: "Cheyenne, Wyoming"
© www.soinside.com 2019 - 2024. All rights reserved.