Finding the name of a new pod with kubectl
Complementing what @nickgryg said, if kubernetes is in the cloud you can add the namespace and kubeconfig to get the pod name:
kubectl get pods -l app=my-app -o jsonpath="{.items[0].metadata.name}" -n my-namespace --kubeconfig=/home/$USER/.kube/kubeconfig
And to delete it with a single command, you can use the following:
NAMESPACE=my-namespace APP_NAME=my-app && kubectl delete -n $NAMESPACE pod $(kubectl get pods -l app=$APP_NAME -o jsonpath="{.items[0].metadata.name}" -n $NAMESPACE --kubeconfig=/home/$USER/.kube/kubeconfig) --kubeconfig=/home/$USER/.kube/kubeconfig
You need to label your deployment
somehow, for example we set label app: myapp
below:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx
spec:
template:
metadata:
labels:
app: my-app
spec:
containers:
- image: nginx
name: nginx
After that you can get deployment's pod name very easy:
POD=$(kubectl get pod -l app=my-app -o jsonpath="{.items[0].metadata.name}")
and execute some command there, for example:
kubectl exec -ti $POD -- uname -a
Like Nickolay wrote, use a label to help selecting. Then you can use
kubectl get pods -l app=my-app -o custom-columns=:metadata.name
This gets you the name of the pod that has the label "app=my-app"
All other answers doesnt work for me
Not working option
kubectl -n $NAMESPACE get pods -l app.kubernetes.io/name=nats -o name
working option
kubectl -n $NAMESPACE get pods --selector=app.kubernetes.io/name=nats -o name