7

I am configuring a StatefulSet where I want the number of replicas (spec.replicas as shown below) available to somehow pass as a parameter into the application instance. My application needs spec.replicas to determine the numer of replicas so it knows what rows to load from a MySQL table. I don't want to hard-code the number of replicas in both spec.replicas and the application parameter as that will not work when scaling the number of replicas up or down, since the application parameter needs to adjust when scaling.

Here is my StatefulSet config:

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  labels:
    run: my-app
  name: my-app
  namespace: my-ns
spec:
  replicas: 3
  selector:
    matchLabels:
      run: my-app
  serviceName: my-app
  podManagementPolicy: Parallel
  template:
    metadata:
      labels:
        run: my-app
    spec:
      containers:
      - name: my-app
        image: my-app:latest
        command:
          - /bin/sh
          - /bin/start.sh
          - dev
          - 2000m
          - "0"
          - "3" **Needs to be replaced with # replicas**
          - 127.0.0.1
          - "32990"
        imagePullPolicy: Always
        livenessProbe:
          httpGet:
            path: /health
            port: 8081
          initialDelaySeconds: 180
          periodSeconds: 10
          timeoutSeconds: 3
        readinessProbe:
          failureThreshold: 10
          httpGet:
            path: /ready
            port: 8081
            scheme: HTTP
          initialDelaySeconds: 30
          periodSeconds: 15
          successThreshold: 1
          timeoutSeconds: 3
        ports:
        - containerPort: 8080
          protocol: TCP
        resources:
          limits:
            memory: 2500Mi
      imagePullSecrets:
      - name: snapshot-pull
      restartPolicy: Always

I have read the Kubernetes docs and the spec.replicas field is scoped at the pod or container level, never the StatefulSet, at least as far as I have seen.

Thanks in advance.

Shahriar
  • 13,460
  • 8
  • 78
  • 95
mjkrumlauf
  • 89
  • 1
  • 4
  • Have you considered using something helm? It will allow you to keep your spec templated and use a single source of truth outside of the spec to fill it in. – ffledgling Apr 25 '18 at 06:03

2 Answers2

3

You could use a yaml anchor to do this:

Check out: https://helm.sh/docs/chart_template_guide/yaml_techniques/#yaml-anchors

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  labels:
    run: my-app
  name: my-app
  namespace: my-ns
spec:
  replicas: &numReplicas 3
  selector:
    matchLabels:
      run: my-app
  serviceName: my-app
  podManagementPolicy: Parallel
  template:
    metadata:
      labels:
        run: my-app
    spec:
      containers:
      - name: my-app
        image: my-app:latest
        command:
          - /bin/sh
          - /bin/start.sh
          - dev
          - 2000m
          - "0"
          - *numReplicas
          - 127.0.0.1
          - "32990"
        imagePullPolicy: Always
        livenessProbe:
          httpGet:
            path: /health
            port: 8081
          initialDelaySeconds: 180
          periodSeconds: 10
          timeoutSeconds: 3
        readinessProbe:
          failureThreshold: 10
          httpGet:
            path: /ready
            port: 8081
            scheme: HTTP
          initialDelaySeconds: 30
          periodSeconds: 15
          successThreshold: 1
          timeoutSeconds: 3
        ports:
        - containerPort: 8080
          protocol: TCP
        resources:
          limits:
            memory: 2500Mi
      imagePullSecrets:
      - name: snapshot-pull
      restartPolicy: Always
dnault
  • 8,340
  • 1
  • 34
  • 53
MichaelGreen
  • 53
  • 1
  • 9
2

Normally you would use the downward api for this kind of thing. https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/

However it is currently not possible for kubernetes to propagate deployment/statefulset spec data into the pod spec with the downward api, nor should it be. If you are responsible for this software I'd set up some internal functionality so that it can find it's peers and determine their count periodically.

Brett Wagner
  • 1,134
  • 2
  • 10
  • 18