0

I'm bit new to kubernetes, I know we can create multiple deployments using same template. I have already gone through this. But my requirement is slight different. I have 30 deployment files wherein only two parameters that's deployment name and python script1.py keeps on updating for all deployments. Below are sample deployment files

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

I want to know how can I convert this into single template so that multiple deployments can be deployed into the cluster. Eventually I want to integrate this into cloud build as a part of my CI/CD.

Any help would be appreciated here.

Update 1 :

@Moritz Schmitz v. Hülst I have updated my code to include below files in my helm chart.

here is my values.yaml

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

template/deployment.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 }}

template/service.yaml

{{- range .Values.deployments }}
apiVersion: v1
kind: Service
metadata:
  name: {{ .name }}
spec:
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: {{ .name }}
{{- end }}

I get below error while running helm install demo-nginx demo-hello/,

Error: INSTALLATION FAILED: unable to build kubernetes objects from release manifest: error validating "": error validating data: [unknown object type "nil" in Deployment.metadata.labels.app, unknown object type "nil" in Deployment.spec.selector.matchLabels.app, unknown object type "nil" in Deployment.spec.template.metadata.labels.app]

Rahul
  • 402
  • 2
  • 9
  • 23
  • Does it help? https://serverfault.com/questions/993167/in-a-helm-template-how-can-i-use-items-in-1-list-to-iterate-over-another-list – Sanjay Choudhary Sep 25 '22 at 07:50
  • @SanjayChoudhary I gone through this question too, I'm struggling to understand how can I loop over the deployment names and argument. – Rahul Sep 25 '22 at 08:16
  • In the question you link to, there's a `range` loop that outputs a list of Deployment objects. Can you adapt that approach? – David Maze Sep 25 '22 at 11:51
  • @DavidMaze Approach mentioned in question I linked.. they are suggesting to create multiple `values.yaml` for each microservices which I want to avoid. Is there any better approach to tackle this situation ? – Rahul Sep 25 '22 at 12:34
  • You could move the `range` loop into its own template, and `include` it multiple times; or you could create a `list` of the per-deployment values and then execute a `range` loop over that. – David Maze Sep 25 '22 at 12:52
  • @DavidMaze would really appreciate if you could share few link I can refer. – Rahul Sep 25 '22 at 12:59
  • Only three values change and the rest is all the same, do I get that right? – Moritz Schmitz v. Hülst Sep 25 '22 at 16:55
  • @MoritzSchmitzv.Hülst yes correct only only 3 values are getting updated for every deployments. – Rahul Sep 28 '22 at 13:23

1 Answers1

1

deployment.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
Moritz Schmitz v. Hülst
  • 3,229
  • 4
  • 36
  • 63
  • @Rahul, may I ask what you are trying to do with the script? – Moritz Schmitz v. Hülst Sep 30 '22 at 07:46
  • I will use this script to use it in cloudbuild for Continuous Integration and Continuous Deployment to the Kubernetes Cluster. – Rahul Sep 30 '22 at 14:06
  • I am asking about the function. Since you are using nginx and some python script. – Moritz Schmitz v. Hülst Oct 02 '22 at 07:39
  • I get this error while installing helm chart `Error: INSTALLATION FAILED: unable to build kubernetes objects from release manifest: error validating "": error validating data: [unknown object type "nil" in Deployment.metadata.labels.app, unknown object type "nil" in Deployment.spec.selector.matchLabels.app, unknown object type "nil" in Deployment.spec.template.metadata.labels.app] ` – Rahul Oct 02 '22 at 11:27
  • Does your deployment.yaml look exactly like mine? Just tested it again and it should work. Do you use other variables? – Moritz Schmitz v. Hülst Oct 04 '22 at 07:48
  • I have updated my question to include files I have used as per your suggestion. Let me know if anything is missing or anything is incorrect. – Rahul Oct 04 '22 at 09:54
  • 1
    After removing the image variable from `values.yaml` it's working as expected. – Rahul Oct 04 '22 at 09:58