20

The command kubectl get pods <POD NAME> will return the specific pod with that name. I wonder if there is a way that I can use part of the name, for instance, a command that returns all pods that start with j.

Kiarash Alinasab
  • 794
  • 1
  • 4
  • 16

4 Answers4

24

In Linux Bash:

kubectl get pods | grep ^j

In Windows PowerShell:

kubectl get pods | Select-String '^j'
mirekphd
  • 4,799
  • 3
  • 38
  • 59
Kiarash Alinasab
  • 794
  • 1
  • 4
  • 16
  • 1
    Filter with grep or equivalent is the only way. Regex/glob filtering in `kubectl` was proposed and rejected several times: https://github.com/kubernetes/kubernetes/issues/109400 https://github.com/kubernetes/kubernetes/issues/107053 https://github.com/kubernetes/kubernetes/issues/107285 – Geoff Williams Feb 16 '23 at 23:23
3

I am using the service name as a filter. it is kind of easier.

kubectl get pods -l service=usercontent

or something like this

kubectl get pods -l app=rabbit

You will get all pods that related this service. If you have multiple databases pods or apps it is quite useful.

nzrytmn
  • 6,193
  • 1
  • 41
  • 38
  • That's exactly what I needed. Not a 'sql-like' but 'all the pods related to a deployment. Your command didn't work on my k8s-minikube local installation, I did a little modification: after a full list of details, got by 'kubectl describe pods', I did 'kubectl get pods -l app=xxx' – Diego Pascotto Jun 14 '23 at 08:22
1

If you have crictl installed in your node:

 crictl pods --name '^j'
P....
  • 17,421
  • 2
  • 32
  • 52
1

Filter without grep (unfortunately requires full name):

kubectl get pods --field-selector metadata.name=<your_pod_name>

this works for me. Of course you have to precise the namespace if not set in the current-context

Dumitru
  • 27
  • 2