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

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

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

deployment1.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: deploy1    <-- Will be updated every time for all deployments
  name: deploy1     <-- Will be updated every time for all deployments
spec:
  replicas: 3
  selector:
    matchLabels:
      app: deploy1
  strategy:
    rollingUpdate:
      maxSurge: 2
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: deploy1
    spec:
      containers:
      - name: web
        image: nginx
        command: ["/bin/sh"]
        args:
          - -c
          - >-
              python script1.py     <-- Will be updated every time for all deployments

deployment2.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: deploy2    <-- Will be updated every time for all deployments
  name: deploy2     <-- Will be updated every time for all deployments
spec:
  replicas: 3
  selector:
    matchLabels:
      app: deploy2
  strategy:
    rollingUpdate:
      maxSurge: 2
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: deploy2
    spec:
      containers:
      - name: web
        image: nginx
        command: ["/bin/sh"]
        args:
          - -c
          - >-
              python script2.py     <-- Will be updated every time for all deployments

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

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

更新1:

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

这是我的values.yaml

deployments:
  - image: nginx
  - name: deploy1
    script: script1.py
  - name: deploy2
    script: script2.py

模板/部署.yaml

{{-Range.Values.Deployments}}

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: {{ .name }}
  name: {{ .name }}
spec:
  replicas: 3
  selector:
    matchLabels:
      app: {{ .name }}
  strategy:
    rollingUpdate:
      maxSurge: 2
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: {{ .name }}
    spec:
      containers:
      - name: web
        image: {{ .image }}
        ports:
        - containerPort: 80
{{- end }}

模板/服务.yaml

{{- range .Values.deployments }}
apiVersion: v1
kind: Service
metadata:
  name: {{ .name }}
spec:
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: {{ .name }}
{{- 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:

{{- range .Values.deployments }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: {{ .name }}
  name: {{ .name }}
spec:
  replicas: 3
  selector:
    matchLabels:
      app: {{ .name }}
  strategy:
    rollingUpdate:
      maxSurge: 2
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: {{ .name }}
    spec:
      containers:
      - name: web
        image: nginx
        command: ["/bin/sh"]
        args:
          - -c
          - >-
              python {{ .script }}
{{- end }}

Values.yaml:

deployments:
  - name: deploy1
    script: script1.py
  - name: deploy2
    script: script2.py

相关问题