get k8s pods from a node with regex pattern match in namespace name

Viewed 5992

Team,

I am able to fetch all pods running on a node with its namespace but my namespaces are generated dynamically and they change with characters in end. is there a way i can include a regex/pattern that I can use in kubectl command to pull all pods from all matching namespace?

kubectl get pods -n team-1-user1 --field-selector=spec.nodeName=node1,status.phase=Running

actual output1: works

NAMESPACE                                     NAME                                                                   READY   STATUS    RESTARTS   AGE
team-1-user1                                   calico-node-9j5k2                                                      1/1     Running   2          104d
team-1-user1                                   kube-proxy-ht7ch                                                       1/1     Running   2          130d

I want below pulling pods for all namespaces starting with "team-".

kubectl get pods -n team-* --field-selector=spec.nodeName=node1,status.phase=Running

actual output2: fails

No resources found in team-workflow-2134-asf-324-d.yaml namespace.

expected outout: want this..

NAMESPACE                                     NAME                                                                   READY   STATUS    RESTARTS   AGE
team-1-user1                                   calico-node-9j5k2                                                      1/1     Running   2          104d
team-1-user1                                   kube-proxy-ht7ch                                                       1/1     Running   2          130d

team-2-user1                                   calico-node-9j5k2                                                      1/1     Running   2          1d
team-2-user1                                   kube-proxy-ht7ch                                                       1/1     Running   2          10d

1 Answers

You can pipe the output of kubectl get pods into awk and match a regex for the same:

kubectl get pods --all-namespaces --no-headers |  awk '{if ($1 ~ "team-") print $0}'

Here's a sample output for the same, searching for pods in kube- namespace:

❯❯❯ kubectl get pods --all-namespaces --no-headers |  awk '{if ($1 ~ "kube-") print $0}'
kube-system            coredns-6955765f44-27wxs                     1/1   Running             0     107s
kube-system            coredns-6955765f44-ztgq8                     1/1   Running             0     106s
kube-system            etcd-minikube                                1/1   Running             0     109s
kube-system            kube-addon-manager-minikube                  1/1   Running             0     108s
Related