Kubernetes使用CI/CD的单一模板进行多个部署

yuvru6vn  于 2022-10-06  发布在  Kubernetes
关注(0)|答案(1)|浏览(187)

我对Kubernetes还是个新手,我知道我们可以使用相同的模板创建多个部署。我已经经历了this。但我的要求略有不同。我有30个部署文件,其中只有两个参数-部署namepython script1.py-针对所有部署不断更新。以下是示例部署文件

deployment1.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app: deploy1 <-- Will be updated every time for all deployments
  6. name: deploy1 <-- Will be updated every time for all deployments
  7. spec:
  8. replicas: 3
  9. selector:
  10. matchLabels:
  11. app: deploy1
  12. strategy:
  13. rollingUpdate:
  14. maxSurge: 2
  15. maxUnavailable: 0
  16. type: RollingUpdate
  17. template:
  18. metadata:
  19. labels:
  20. app: deploy1
  21. spec:
  22. containers:
  23. - name: web
  24. image: nginx
  25. command: ["/bin/sh"]
  26. args:
  27. - -c
  28. - >-
  29. python script1.py <-- Will be updated every time for all deployments

deployment2.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app: deploy2 <-- Will be updated every time for all deployments
  6. name: deploy2 <-- Will be updated every time for all deployments
  7. spec:
  8. replicas: 3
  9. selector:
  10. matchLabels:
  11. app: deploy2
  12. strategy:
  13. rollingUpdate:
  14. maxSurge: 2
  15. maxUnavailable: 0
  16. type: RollingUpdate
  17. template:
  18. metadata:
  19. labels:
  20. app: deploy2
  21. spec:
  22. containers:
  23. - name: web
  24. image: nginx
  25. command: ["/bin/sh"]
  26. args:
  27. - -c
  28. - >-
  29. python script2.py <-- Will be updated every time for all deployments

我想知道如何将其转换为单模板,以便可以将多个部署部署到集群中。最终,我想将其集成到云构建中,作为我的CI/CD的一部分。

在这里,任何帮助都将不胜感激。

更新1:

@Moritz Schmitz v.Hülst我已经更新了我的代码,将以下文件包括在我的舵表中。

这是我的values.yaml

  1. deployments:
  2. - image: nginx
  3. - name: deploy1
  4. script: script1.py
  5. - name: deploy2
  6. script: script2.py

模板/部署.yaml

{{-Range.Values.Deployments}}

  1. ---
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. labels:
  6. app: {{ .name }}
  7. name: {{ .name }}
  8. spec:
  9. replicas: 3
  10. selector:
  11. matchLabels:
  12. app: {{ .name }}
  13. strategy:
  14. rollingUpdate:
  15. maxSurge: 2
  16. maxUnavailable: 0
  17. type: RollingUpdate
  18. template:
  19. metadata:
  20. labels:
  21. app: {{ .name }}
  22. spec:
  23. containers:
  24. - name: web
  25. image: {{ .image }}
  26. ports:
  27. - containerPort: 80
  28. {{- end }}

模板/服务.yaml

  1. {{- range .Values.deployments }}
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: {{ .name }}
  6. spec:
  7. type: ClusterIP
  8. ports:
  9. - port: 80
  10. targetPort: 80
  11. selector:
  12. app: {{ .name }}
  13. {{- end }}

运行helm install demo-nginx demo-hello/时出现以下错误,

错误:安装失败:无法从版本清单构建Kubernetes对象:验证“”时出错:验证数据时出错:[Deployment.metadata.labels.app中的未知对象类型“nil”,Deployment.spec.seltor.matchLabels.app中的未知对象类型“nil”,Deployment.spec.template.metadata.Labels.app中的未知对象类型“nil”]

oalqel3c

oalqel3c1#

部署.yaml:

  1. {{- range .Values.deployments }}
  2. ---
  3. apiVersion: apps/v1
  4. kind: Deployment
  5. metadata:
  6. labels:
  7. app: {{ .name }}
  8. name: {{ .name }}
  9. spec:
  10. replicas: 3
  11. selector:
  12. matchLabels:
  13. app: {{ .name }}
  14. strategy:
  15. rollingUpdate:
  16. maxSurge: 2
  17. maxUnavailable: 0
  18. type: RollingUpdate
  19. template:
  20. metadata:
  21. labels:
  22. app: {{ .name }}
  23. spec:
  24. containers:
  25. - name: web
  26. image: nginx
  27. command: ["/bin/sh"]
  28. args:
  29. - -c
  30. - >-
  31. python {{ .script }}
  32. {{- end }}

Values.yaml:

  1. deployments:
  2. - name: deploy1
  3. script: script1.py
  4. - name: deploy2
  5. script: script2.py
展开查看全部

相关问题