Kubernetes StatefulSet - obtain spec.replicas metadata and reference elsewhere in configuration Kubernetes StatefulSet - obtain spec.replicas metadata and reference elsewhere in configuration kubernetes kubernetes

Kubernetes StatefulSet - obtain spec.replicas metadata and reference elsewhere in configuration


You could use a yaml anchor to do this:

Check out:https://github.com/kubernetes/helm/blob/master/docs/chart_template_guide/yaml_techniques.md

apiVersion: apps/v1beta1kind: StatefulSetmetadata: labels: run: my-app name: my-app namespace: my-nsspec: 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


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.