Is it possible to pass the actual number of replicas to a pod in a StatefulSet?

I created a StatefulSet and I want to pass to each of its pods the number of pods in this StatefulSet through an environment variable named REPLICASIZE. Is this possible? My yaml looks as:

---
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: php-app-4-rcweb
      namespace: kube-system
      labels:
        app: mypod
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: mypod
      template:
        metadata:
          labels:
            app: mypod
        spec:
          containers:
          - name: php-apache
            image: my-registry.local:5000/my-image:0.0.1  # Or your custom image

            env:
             - name: TZ
               ...
             - name: REPLICASIZE
               value: ................


Asking for help? Comment out what you need so we can get more information to help you!

Cluster information:

Kubernetes version:

$ k3s -version
k3s version v1.33.3+k3s1 (236cbf25)
go version go1.24.4

Cloud being used: bare metal

Installation method:

curl -sfL https://get.k3s.io | sh -s -- --cluster-cidr=142.200.0.0/16 --service-cidr=142.201.0.0/16

Host OS: Ubuntu Linux 24
CNI and version: default
CRI and version: default

Natively, it’s not supported.

But you can create a headless svc that will return ips of all replicas, which you can use to get the count of replicas

something like this

amazinrahul@archlinux /tmp> k get svc,ep -n kube-system 
Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice
NAME               TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)                  AGE
service/antrea     ClusterIP   10.104.9.31   <none>        443/TCP                  3h33m
service/kube-dns   ClusterIP   10.96.0.10    <none>        53/UDP,53/TCP,9153/TCP   3h33m
service/php-app-4-rcweb-headless      ClusterIP   None          <none>        80/TCP                   8s

NAME                 ENDPOINTS                                               AGE
endpoints/antrea     192.168.1.169:10349                                     3h33m
endpoints/kube-dns   10.244.0.2:53,10.244.0.3:53,10.244.0.2:53 + 3 more...   3h33m
endpoints/php-app-4-rcweb-headless      10.244.0.21:80,10.244.0.22:80,10.244.0.23:80            8s
amazinrahul@archlinux /tmp> k exec -it -n kube-system php-app-4-rcweb-0 -- bash
root@php-app-4-rcweb-0:/# export REPLICASIZE=$(nslookup php-app-4-rcweb-headless.kube-system.svc.cluster.local | grep -c 'Address:' | awk '{print $1-1}')
root@php-app-4-rcweb-0:/# 
root@php-app-4-rcweb-0:/# echo $REPLICASIZE
3
root@php-app-4-rcweb-0:/# nslookup php-app-4-rcweb-headless.kube-system.svc.cluster.local
;; Got recursion not available from 10.96.0.10
;; Got recursion not available from 10.96.0.10
;; Got recursion not available from 10.96.0.10
;; Got recursion not available from 10.96.0.10
Server:		10.96.0.10
Address:	10.96.0.10#53

Name:	php-app-4-rcweb-headless.kube-system.svc.cluster.local
Address: 10.244.0.21
Name:	php-app-4-rcweb-headless.kube-system.svc.cluster.local
Address: 10.244.0.22
Name:	php-app-4-rcweb-headless.kube-system.svc.cluster.local
Address: 10.244.0.23

Hi @joseccz ,

What happens if you scale number of pods in statefulset? What are your expectations in this case?

Statefulset pods are indexed as with arrays and this could be great to create code that could use those indexes.