Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubectl command to list pods of a deployment in Kubernetes

Tags:

Is there a way to use kubectl to list only the pods belonging to a deployment? Currently, I do this to get pods:

kubectl get pods| grep hello

But it seems an overkill to get ALL the pods when I am interested to know only the pods for a given deployment. I use the output of this command to see the status of all pods, and then possibly exec into one of them.

I also tried kc get -o wide deployments hellodeployment, but it does not print the Pod names.

like image 351
Bajal Avatar asked Oct 23 '18 20:10

Bajal


People also ask

How do I get a list of deployments in Kubernetes?

Run kubectl get deployments to check if the Deployment was created. When you inspect the Deployments in your cluster, the following fields are displayed: NAME lists the names of the Deployments in the namespace. READY displays how many replicas of the application are available to your users.

Which command can be used to list pods in Kubernetes?

The most common operations can be done with the following kubectl commands: kubectl get - list resources. kubectl describe - show detailed information about a resource. kubectl logs - print the logs from a container in a pod.

Which command gets list of pods?

To list one or more pods, replication controllers, services, or daemon sets, use the kubectl get command.


2 Answers

There's a label in the pod for the selector in the deployment. That's how a deployment manages its pods. For example for the label or selector app=http-svc you can do something like that this and avoid using grep and listing all the pods (this becomes useful as your number of pods becomes very large):

$ kubectl get pods -l=app=http-svc 

or

$ kubectl get pods --selector=app=http-svc 
like image 190
Rico Avatar answered Oct 11 '22 18:10

Rico


K8s components are linked to each other by labels and selectors. There are just no built-in attributes of My-List-of-ReplicaSets or My-List-Of-Pods for a deployment. You can't get them from kubectl describe or kubectl get

As @Rico suggested above, you have to use label filters. But you can't simply use the labels that you specify in the deployment metafile because deployment will generate a random hash and use it as an additional label.

For example, I have a deployment and a standalone pod that share the same label app=http-svc. While the first two are managed by the deployment, the 3rd one is not and shouldn't be in the result.

 ma.chi@~/k8s/deployments % kubectl get pods --show-labels NAME                   READY   STATUS    RESTARTS   AGE   LABELS http-9c89b5578-6cqbp   1/1     Running   0          7s    app=http-svc,pod-template-hash=574561134 http-9c89b5578-vwqbx   1/1     Running   0          7s    app=http-svc,pod-template-hash=574561134 nginx-standalone       1/1     Running   0          7s    app=http-svc ma.chi@~/k8s/deployments % 

The source file is

apiVersion: apps/v1 kind: Deployment metadata:   labels:     app: http-svc   name: http spec:   replicas: 2   selector:     matchLabels:       app: http-svc   strategy: {}   template:     metadata:       labels:         app: http-svc     spec:       containers:       - image: nginx:1.9.1         name: nginx1  ---  apiVersion: v1 kind: Pod metadata:   labels:     app: http-svc   name: nginx-standalone spec:   containers:   - image: nginx:1.9.1     name: nginx1-standalone 

To exact spot the containers created and managed by your deployment, you can use the script below(which is ugly, but this is the best I can do)

DEPLOY_NAME=http RS_NAME=`kubectl describe deployment $DEPLOY_NAME|grep "^NewReplicaSet"|awk '{print $2}'`; echo $RS_NAME  POD_HASH_LABEL=`kubectl get rs $RS_NAME -o jsonpath="{.metadata.labels.pod-template-hash}"` ; echo $POD_HASH_LABEL  POD_NAMES=`kubectl get pods -l pod-template-hash=$POD_HASH_LABEL --show-labels | tail -n +2 | awk '{print $1}'`; echo $POD_NAMES 
like image 25
kekaoyunfuwu Avatar answered Oct 11 '22 17:10

kekaoyunfuwu