Eli m
Eli m

Reputation: 83

How do I list all pods controlled by deployments only?

i would like to list all pods of a specific namespace but only those that are controlled by deployment i have tried this but didn't succeed: kubectl get pods --all-namespaces -o json | jq -r '.items | map(select(.metadata.ownerReferences == deployment ) | .metadata.name) | .[]'

I use this command: kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.spec.containers[*].name}{"\n"}{end}' --all-namespaces |sed 's/\t/\'$'\\/g' | sort | uniq to list all containers and their namespaces so i want the same but filtering out all non-deployments kind

Upvotes: 3

Views: 3922

Answers (2)

Dedeepya Desineni
Dedeepya Desineni

Reputation: 11

For some reason kubectl get pods --all-namespaces -o custom-columns=NAME:.metadata.name,CONTROLLER:.metadata.ownerReferences[].kind,NAMESPACE:.metadata.namespace didn't work for me giving below error

zsh: no matches found: custom-columns=NAME:.metadata.name,CONTROLLER:.metadata.ownerReferences[].kind,NAMESPACE:.metadata.namespace

Instead this worked fine - kubectl get pods -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,Component:.metadata.ownerReferences\[\].kind

Upvotes: 1

Eduardo Baitello
Eduardo Baitello

Reputation: 11346

You can use the --custom-columns to find out which pods are controlled, and which kind of controller is the owner. Example:

$ kubectl get pods --all-namespaces -o custom-columns=NAME:.metadata.name,CONTROLLER:.metadata.ownerReferences[].kind,NAMESPACE:.metadata.namespace
NAME                                        CONTROLLER   NAMESPACE
nginx-858dbf7665-8t9vv                      ReplicaSet   default
coredns-74ff55c5b-xpgnq                     ReplicaSet   kube-system
etcd-minikube                               Node         kube-system
ingress-nginx-admission-create-n6j7k        Job          kube-system
ingress-nginx-admission-patch-45xvw         Job          kube-system
ingress-nginx-controller-65cf89dc4f-g7lwm   ReplicaSet   kube-system
kindnet-44pq8                               DaemonSet    kube-system
kindnet-nqhg9                               DaemonSet    kube-system
kube-apiserver-minikube                     Node         kube-system
kube-controller-manager-minikube            Node         kube-system
kube-proxy-nmzbn                            DaemonSet    kube-system
kube-proxy-wlmdz                            DaemonSet    kube-system
kube-scheduler-minikube                     Node         kube-system
metrics-server-58966dd6b9-schjr             ReplicaSet   kube-system
storage-provisioner                         <none>       kube-system

Remember that Deployments automatically creates ReplicaSets and rely on them to manage a set of pods and its desired state/replicas. So you can just filter them:

$ kubectl get pods --all-namespaces -o json | jq -r '.items | map(select(.metadata.ownerReferences[]?.kind == "ReplicaSet" ) | .metadata.name) | .[]'
nginx-858dbf7665-8t9vv
coredns-74ff55c5b-xpgnq
ingress-nginx-controller-65cf89dc4f-g7lwm
metrics-server-58966dd6b9-schjr

Upvotes: 3

Related Questions