Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 430
Raj N Avatar asked Jun 07 '18 21:06

Raj N


People also ask

How do you get a StatefulSet pod name?

Each Pod in a StatefulSet derives its hostname from the name of the StatefulSet and the ordinal of the Pod. The pattern for the constructed hostname is $(statefulset name)-$(ordinal). The example above will create three Pods named web-0,web-1,web-2.

How do I check my StatefulSet?

You will need to use two terminal windows. In the first terminal, use kubectl get to watch the creation of the StatefulSet's Pods. In the second terminal, use kubectl apply to create the headless Service and StatefulSet defined in web. yaml .

What is serviceName in StatefulSet?

serviceName is the name of the service that governs this StatefulSet. This service must exist before the StatefulSet, and is responsible for the network identity of the set. Pods get DNS/hostnames that follow the pattern: pod-specific-string.


1 Answers

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
like image 176
Abhishek Jaisingh Avatar answered Sep 28 '22 13:09

Abhishek Jaisingh