How to access the name of a pod?

Sometimes you need the name of a pod within a script (e.g. with kubectl exec). But since you normally want to use a deployment or replica set to manage your pod, the name of the pod might change and cannot be predicted.

There is an easy way to access the name of the pod via ‘kubectl’ with the help of labels. In this example we access the name of the pod described by the label label=value.

$kubectl get pod -l label=value -o jsonpath='{.items[0].metadata.name}'

For details on the jsonpath parameter can be found at https://kubernetes.io/docs/reference/kubectl/jsonpath/.

Leave a comment

Your email address will not be published. Required fields are marked *