Log
Log

Reputation: 483

How to get full name of pod by name in kubernetes?

I have backend service description in skaffold.yaml similiar this:

...
deploy:
  helm:
    releases:
    - name: 'myapp-backend'
      chartPath: myapp-chart-backend
      values:
        APP_IMAGE: ...
      namespace: myapp-ns
      recreatePods: true
...

After cluster is up I have list pods and kubectl get pods return

...
myapp-backend-7dbf4b6fb8-kw7zv
myapp-backend-redis-646f454bcb-7thrc
...

I need full name of pod (myapp-backend-7dbf4b6fb8-kw7zv) to use it in kubectl cp command, which requires full name. But this command I run in my bash script, so I need to get full name myapp-backend-7dbf4b6fb8-kw7zv by name myapp-backend.

Upvotes: 0

Views: 3147

Answers (1)

Mark Bramnik
Mark Bramnik

Reputation: 42541

Assuming you know the name in the deployment ('myapp-backend' in this case), you can:

kubectl get pods --selector=app=myapp-backend -o jsonpath='{.items[*].metadata.name}'

Update

Since I obviously don't have an access to your environment, I've rather provided a a general path for solution, you can fiddle with this command but the idea probably will remain the same:

  1. call kubectl get pods --selector=... (Its possible that you should add more selectors in __your__environment)
  2. Assume that the output is json. Here one nifty trick is to examine json by using: kubectl get pods --selector=app=<myapp-backend> -o json. You'll get a properly formatted json that you can inspect and see what part of it you actually want to get.
  3. query by jsonpath only the part of json that you want by providing a jsonpath expression, for example {.items[0].metadata.name} will also work

Upvotes: 2

Related Questions