Passing Information about Statefulset to Container

3,589 views
Skip to first unread message

scott....@kaseware.com

unread,
May 8, 2017, 6:35:03 PM5/8/17
to Kubernetes user discussion and Q&A
I need to pass the following variables into each container for a StatefulSet:

1. The DNS name of the current pod.
2. The ordinal value of the current pod.
3. The total number of replicas defined (not necessarily running).

It would appear that the downward API would be a good fit for this, but I'm not sure how to set those values as environment variables using that functionality. It appears that I'd use fieldPath, but I can't find of list of viable fieldPath variables anywhere.

Any help, or pointers in the right direction, are very much appreciated.

owe...@google.com

unread,
May 9, 2017, 1:30:40 PM5/9/17
to Kubernetes user discussion and Q&A, scott....@kaseware.com

1. You can consume the HOSTNAME environment variable, or you can follow these instructions https://kubernetes.io/docs/tasks/configure-pod-container/environment-variable-expose-pod-information/#using-pod-fields-as-values-for-environment-variable to consume values from the Pod or Container as an environment variable.

2. You can use a regular expression to extract the ordinal from the HOSTNAME environment variable ${HOSTNAME##*-}.

3. This is not currently passed to the Pod or the Container. The only way to do this would be to get and watch the parent StatefulSet, and to extract the value from there. I'd caution against this if you can avoid it.

scott....@kaseware.com

unread,
May 10, 2017, 11:00:57 AM5/10/17
to Kubernetes user discussion and Q&A, scott....@kaseware.com, owe...@google.com
Thank you, that is helpful. I may be able to work around the lack of support for #3.

One additional question comes to mind, is there a way to pass information about one stateful set to another?

I.E. let's say that I have one stateful set that has five replicas. I want to be able to store the hostnames of each of the replicas, and provide them to another stateful set that needs to communicate with them, preferably as environment variables. Can this be done?

owe...@google.com

unread,
May 11, 2017, 5:06:02 PM5/11/17
to Kubernetes user discussion and Q&A, scott....@kaseware.com, owe...@google.com

The hostnames of the replicas are deterministic based on the name of the StatefulSet. You should be able to resolve the membership of the first StatefulSet from the second by looking up the SRV records associated with the CNAME provided that you use "service.alpha.kubernetes.io/tolerate-unready-endpoints" on the Headless Service of the first StatefulSet.

mkru...@moreover.com

unread,
Feb 6, 2018, 8:02:18 AM2/6/18
to Kubernetes user discussion and Q&A

Did you come up with a workaround for the lack of support for obtaining the number of replicas? This is the exact scenario I have.

I am curious why the downwardAPI doesn't just provide support for accessing every field in whatever k8s object is configured.

Reply all
Reply to author
Forward
0 new messages