kuberneteskubernetes-helmskaffold

Create an identical pod using different values


I currently have an app deployed on k8s using Skaffold. Using Helm I have defined the main deployment.yaml manifest as a template and used a values.yaml to insert other values into that deployment template.

What I would like to do is have 2 deployments of the same app using different values from the values.yaml file. So essentially 2 pods running the same application aside from the different values provided, for instance a my-app-blue pod and my-app-green pod. Is there anyway that I can do this without having to use a whole new deployment.yaml file? Sorry for any ambiguity I'm new to K8s, Helm, and Skaffold.

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
   namespace: {{ .Values.namespace }}
   name: {{ .Values.name }}
spec:
   replicas: {{ .Values.blue-deployment.replicas }}
   template:
      spec:
         containers:
            -  name: {{ .Values.blue-deployment.name }}
               image: {{ .Values.blue-deployment.image }}

values.yaml

namespace: my-app
name: app

blue-deployment:
   name: blue
   image: my-image
   replicas: 1
   env:
      - name: COLOR
        value: 'blue'

green-deployment:
   name: green
   image: my-image
   replicas: 1
   env:
      - name: COLOR
        value: 'green'

skaffold.yaml

apiVersion: skaffold/v2beta
kind: Config
build:
   artifacts:
      - image: blue-deployment
        context: './'
deploy:
   - name: my-application
     chartPath: kubernetes
     valuesFiles:
        - kubernetes/values.yaml
     namespace: my-app

directory structure:

myApp/
kubernetes/
   templates/
      myApp/
         deployment.yaml
   values.yaml
skaffold.yaml
Dockerfile   

Solution

  • Just use a loop. helm range

    values.yaml

    namespace: my-app
    name: app
    
    deps:
      - name: blue
        image: my-image
        replicas: 1
        env:
          - name: COLOR
            value: 'blue'
    
      - name: green
        image: my-image
        replicas: 1
        env:
          - name: COLOR
            value: 'green'
    

    deployment.yaml

    {{- $i, $v := range .Values.deps}}
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
       namespace: {{ .Values.namespace }}
       name: {{ $v.name }}
    spec:
       replicas: {{ $v.replicas }}
       template:
          spec:
             containers:
                -  name: {{ $v.name }}
                   image: {{ $v.image }}
    {{- end }}
    
    

    output

    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
       namespace: my-app
       name: blue
    spec:
       replicas: 1
       template:
          spec:
             containers:
                -  name: blue
                   image: my-image
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
       namespace: my-app
       name: green
    spec:
       replicas: 1
       template:
          spec:
             containers:
                -  name: green
                   image: my-image