Get IP addressses for pods in Kubernetes statefulset

Viewed 77

I'm using the C# client API and I'm trying to:

  1. Get all pods associated with a given StatefulSet
  2. Get the cluster IP address of each pod

I am aware of Kubernetes.CoreV1.ListNamespacedPodAsync and Kubernetes.AppsV1.ListNamespacedStatefulSetAsync. The part that I'm missing is how to

  1. Know whether a given pod is associated with the StatefulSet
  2. Discover a pod's IP address
1 Answers

A feature of Kubernetes StatefulSet is Stable Network Identity. This requires that additional headless Services are created, in addition to the StatefulSet resource.

When this is done, you would typically access these instances by their hostname instead of using IP address directly. See Stable Network ID for documentation about this.

E.g. after headless Services are created you could access 3 instances on these hostnames

  • my-app-0
  • my-app-1
  • my-app-2

(when accessed from within the same namespace)

Related