Can I get the number of current replicas from within statefulset pod with code?

Can I get the number of current replicas from within statefulset pod with code?

I know I can create environment variable during deployment but what if it gets re-scaled? Is there a way to know?

Use jsonPath. For example, the below line allows you to get the name of statefulset, the namespace and the current replicas for each one:

kubectl get statefulset -A -o=custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,CurrentReplicas:.status.currentReplicas