Search code examples
kubernetes-helm

Helm Charts - can you dynamically generate n StatefulSets?


I have an application that requires a configurable number of master nodes and replicas. Is there any way to dynamically generate a n stateful sets where n is the number of master nodes I have? The number of master nodes is currently set in values.yaml.


Solution

  • Yes, it is possible with until function.

    values.yaml:

    masterCount: 5
    

    templates/statefulset.yaml:

    {{ range $k, $v := until ( .Values.masterCount | int) }}
    ---
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: nginx-{{ $v }}
    spec:
      serviceName: "nginx-{{ $v }}"
      replicas: 3
      selector:
        matchLabels:
          app: nginx-{{ $v }}
      template:
        metadata:
          labels:
            app: nginx-{{ $v }}
        spec:
          containers:
          - name: nginx
            image: k8s.gcr.io/nginx-slim:0.8
            ports:
            - containerPort: 80
              name: web
            volumeMounts:
            - name: www
              mountPath: /usr/share/nginx/html
    {{ end }}