We have integrated auto-scaling to our kubernetes setup with KEDA. Currently we are using KEDA custom auto scalers as triggers for auto-scaling with our custom logic. We have a component which have specific requirement where it should know the count of its replicas available when component is starting. I need a way to implement this. This should work for both statefulsets and deployments. Please add a comment if you need more information
Replicas are the number of pods created by the StatefulSet controller. To see the ReplicaSet (rs) created by the Deployment, run the command kubectl get rs.
StatefulSets manages the deployment and scaling of a set of Pods, and provides guarantees about the ordering and uniqueness of these Pods. you can simply find the StatefulSet you want to scale,run kubectl get statefulsets <stateful-set-name>.
As per the kubernetes document, you can try to use init containers to get the replica count.
spec:
initContainers:
- name: get-replica-count
image: busybox
command: ["sh", "-c"]
args:
- |
REPLICAS=$(kubectl get statefulset example-statefulset -o=jsonpath='{.spec.replicas}')
echo "REPLICAS=$REPLICAS" > /replica-count.txt
volumeMounts:
- name: config-volume
mountPath: /config
containers:
- name: main-container
image: nginx:1.14.2
volumeMounts:
- name: config-volume
mountPath: /config
readOnly: true
volumes:
- name: config-volume
emptyDir: {}
By the above yaml manifest, kubectl gets the number of replicas and stores it in an empty directory file and the main container can then read this file to get the replica count.
2