Is there a way to get ordinal index of a pod with in kubernetes statefulset configuration file?
Asked Answered
P

4

23

We are on Kubernetes 1.9.0 and wonder if there is way to access an "ordinal index" of a pod with in its statefulset configuration file. We like to dynamically assign a value (that's derived from the ordinal index) to the pod's label and later use it for setting pod affinity (or antiaffinity) under spec.

Alternatively, is the pod's instance name available with in statefulset configfile? If so, we can hopefully extract ordinal index from it and dynamically assign to a label (for later use for affinity).

Popele answered 7/6, 2018 at 21:54 Comment(2)
There's a thread about this as a feature request here: github.com/kubernetes/kubernetes/issues/40651 And one that may apply to you: github.com/kubernetes/kubernetes/issues/… NOTE: I know nothing of kubernetesTorgerson
What do you mean by "... under spec."?Analogous
S
17

Right now the only option is to extract index from host name

lifecycle:
  postStart:
    exec:
      command: ["/bin/sh", "-c", "export INDEX=${HOSTNAME##*-}"]
Selfannihilation answered 7/6, 2018 at 23:26 Comment(2)
Not a correct workaround if the env variable must be set before the entry point is called: kubernetes.io/docs/concepts/containers/… There is no guarantee that the hook will execute before the container ENTRYPOINTAlidia
how would one use the exported INDEX then in the args?Heuser
R
14

You could essentially get the unique name of your pod in statefulset as an environment variable, you have to extract the ordinal index from it though

In container's spec:

env:
  - name: cluster.name
    value: k8s-logs
  - name: node.name
    valueFrom:
      fieldRef:
        fieldPath: metadata.name
Revamp answered 22/1, 2019 at 10:19 Comment(1)
Question asked for ordinal, not whole name.Domesticity
S
11

To get the exact ordinal number of a statefulset you can use the following code

env:
  - name: ORDINAL_NUMBER
    valueFrom:
      fieldRef:
        fieldPath: metadata.labels['apps.kubernetes.io/pod-index']
Superficies answered 8/10, 2023 at 13:46 Comment(1)
It gets the job done for version 1.28.0+. See kubernetes.io/docs/reference/labels-annotations-taints/…Marshmallow
C
0

The pod index is exposed as via pod label app.kubernetes.io/pod-index as explained in Ordinal Index and Pod index label

You have two options to read this pod label from within the container:

The Expose Pod Information to Containers Through Environment Variables has been explained in other answers but here it is again (look for MY_POD_INDEX and pod-index):

...
spec: 
  template:
  ...
  spec:
    containers:
      - name: test-container
        image: registry.k8s.io/busybox
        command: [ "sh", "-c"]
        args:
        - while true; do
            echo -en '\n';
            printenv MY_POD_INDEX;
            sleep 10;
          done;
        env:
          - name: MY_POD_INDEX
            valueFrom:
              fieldRef:
                fieldPath: metadata.labels['apps.kubernetes.io/pod-index']
        
    restartPolicy: Never

and via files it would be like this:

...
spec: 
  ...
  template:
  ...
  spec:
    containers:
      - name: client-container
        image: registry.k8s.io/busybox
        command: ["sh", "-c"]
        args:
        - while true; do
            if [[ -e /etc/podinfo/labels ]]; then
              echo -en '\n\n'; cat /etc/podinfo/labels; fi;
            sleep 5;
          done;
        volumeMounts:
          - name: podinfo
            mountPath: /etc/podinfo
    volumes:
      - name: podinfo
        downwardAPI:
          items:
            - path: "labels"
              fieldRef:
                fieldPath: metadata.labels

in this case the /etc/podinfo/labels from the downwardAPI volume will contain something like:

app.kubernetes.io/pod-index="1"
Chalybite answered 29/4 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.