I'm using StatefulSet in kubernetes, and need to get ordinal index of pod.
I tried to export in postStart.exec.command
, but it doesn't work for me.
Here is my code I tried.
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "export INDEX=${POD_NAME##*-}"]
How can I export ordinal index?
CodePudding user response:
As per this GitLink and SO Link use postStart.exec.command as below and give a try.
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "export INDEX=${HOSTNAME##*-}"]
Refer to this Gitlink and Doc for more information.
CodePudding user response:
You can create a variable based on the POD name, for example:
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
And then, in the post start lifecycle, use TR command to create your ordinary number, example:
lifecycle:
postStart:
exec:
command:
- /bin/bash
- -ec
- export ORDINARY_NUMBER=$(echo $POD_NAME|tr -dc '0-9')