Search code examples
kubernetescontainersreplicaset

how to get all replicaset names inside a container


Consider the following example provided in this doc.

What I'm trying to achieve is to see the 3 replicas names from inside the container. following this guide I was able to get the current pod name, but i need also the pod names from my replicas.

Ideally i would like to:

print(k8s.get_my_replicaset_names())

or

print(os.getenv("MY_REPLICASET"))

and have a result like:

[frontend-b2zdv,frontend-vcmts,frontend-wtsmm]

that is the pod names of all the container's replicas (also the current container of course) and eventually compare the current name in the name list to get my index in the list.

Is there any way to achieve this?


Solution

  • As you can read here, the Downward API is used to expose Pod and Container fields to a running Container:

    There are two ways to expose Pod and Container fields to a running Container:

    Together, these two ways of exposing Pod and Container fields are called the Downward API.

    It is not meant to expose any information about other objects/resources such as ReplicaSet or Deployment, that manage such a Pod.

    You can see exactly what fields contains the yaml manifest that describes a running Pod by executing:

    kubectl get pods <pod_name> -o yaml
    

    The example fragment of its output may look as follows:

    apiVersion: v1
    kind: Pod
    metadata:
      annotations:
        <some annotations here>
        ...
      creationTimestamp: "2020-10-08T22:18:03Z"
      generateName: nginx-deployment-7bffc778db-
      labels:
        app: nginx
        pod-template-hash: 7bffc778db
      name: nginx-deployment-7bffc778db-8fzrz
      namespace: default
      ownerReferences: 👈
      - apiVersion: apps/v1
        blockOwnerDeletion: true
        controller: true
        kind: ReplicaSet 👈
        name: nginx-deployment-7bffc778db 👈
     ...
    

    As you can see, in metadata section it contains ownerReferences which in the above example contains one reference to a ReplicaSet object by which this Pod is managed. So you can get this particular ReplicaSet name pretty easily as it is part of a Pod yaml manifest.

    However, you cannot get this way information about other Pods managed by this ReplicaSet .

    Such information only can be obtained from the api server e.g by using kubectl client or programmatically with direct calls to the API.