nik
nik

Reputation: 2294

Find sibling-pods (pods deployed by the same deployment)

I run an application which needs a list of all client ips for synchronization. I run this application in Kubernetes and make requests against https://kubernetes/api/v1/pods from these client-pods. So I am searching for a reliable way to identify all pods, which are created by the same deployment (replication set) this way.

Thereby it is important, that the pods are also identified correctly during a rolling-upgrade.

I do have a couple of ideas I want to share, even that none really convinces me:

1) Using labels (?labelSelector=label=value):

1.a) Define a custom label only these pods have: Valid solution, but since the solution should be really general, I don't want to define a special label, the pods need to have. Also this way I can not be sure to add other pods which got the label.

1.b) Use the auto-generated pod-template-hash label: Sadly this one gets changed during a rolling-update, so which the update takes place, the selector does not work correctly.

2) Using custom fields (?fieldSelector)

I thought of using either a subpart of the name or ownerReference.name attributes. Sadly the fieldSelector is badly documented or does not work at all. I also thought of parsing the full list of pods via jq, but don't really like the dependency.

3) Identify all pods by referencing a common service

I see now way to recognize which pod belongs to which service (beside using labels again)


Are there any other ideas to identify sibling-pods? I am kind of surprised that I did not find a simular question.

Upvotes: 3

Views: 1683

Answers (2)

daplho
daplho

Reputation: 1135

I wanted to follow up on this by sharing a solution that I've devised that depends on labels and utilizes jq (I realize the OP was specifically not asking for jq solutions but I think this is neat).

// extracts all pod labels and organizes them in a selector
sel=${$(kubectl get deployment --output=json my-deployment | jq -j '.spec.selector.matchLabels | to_entries | .[] | "\(.key)=\(.value),"')%?}

// list pods using the selector above
kubectl get pods --selector=$sel

I borrowed ideas from the kubectl cheat sheet - https://kubernetes.io/docs/reference/kubectl/cheatsheet/

Upvotes: 1

Oswin Noetzelmann
Oswin Noetzelmann

Reputation: 9555

That is what the app selector is typically used for.

You can use kubectl to list pods for a deployment.

The following is from the official docs:

List the pods created by the deployment:

kubectl get pods -l app=nginx

  NAME                                     READY     STATUS    RESTARTS  
  AGE  nginx-deployment-1771418926-7o5ns   1/1       Running   0        
  16h  nginx-deployment-1771418926-r18az   1/1       Running   0

You can also list Pods by yaml file:

$ kubectl get pod -f ./pod.yaml

Upvotes: 3

Related Questions