Openshift/kubernetes: Getting pod names
If you want to get the names of the pod in Openshift/Kubernetes, do
# Using custom-columns
$ oc get pods --no-headers -o custom-columns=":metadata.name"
admin-1-dbxxq
client-1-wpfh7
server-1-qhcwn
database-1-cw64z
# Using json
$ oc get pod -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'
admin-1-dbxxq
client-1-wpfh7
server-1-qhcwn
database-1-cw64z
# Using template
$ oc get pod --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'
admin-1-dbxxq
client-1-wpfh7
server-1-qhcwn
database-1-cw64z
You can also get the first pod by running:
oc get pod --no-headers -o jsonpath={.items[0].metadata.name}
If you would like a specific pod, you can use --show-labels
and -l or --selector
. To list the labels, do:
# Get the pods labels
$ oc get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
admin-1-dbxxq 1/1 Running 0 2h deployment=admin-1,deploymentconfig=admin
client-1-wpfh7 1/1 Running 0 2h deployment=client-1,deploymentconfig=client
server-1-qhcwn 1/1 Running 0 2h deployment=server-1,deploymentconfig=server
database-1-cw64z 1/1 Running 0 9h deployment=database-1,deploymentconfig=database
And now you can select a specific pod, like admin (deploymentconfig=admin)
$ oc get pods --no-headers -o custom-columns=":metadata.name" -l deploymentconfig=admin
admin-1-dbxxq
# OR
$ oc get pods --no-headers -o jsonpath={.items[0].metadata.name} -l deploymentconfig=admin
# OR
$ oc get pod -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' -l deploymentconfig=admin
admin-1-dbxxq
Or you can exec into the pod with:
$ oc exec -it $(oc get pod -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' -l deploymentconfig=admin) -- ls -lah
total 28K
drwxr-xr-x. 1 default root 4.0K Sep 9 19:19 .
drwxr-xr-x. 1 default root 16 Aug 29 10:31 ..
drwxr-xr-x. 1 default root 18 Aug 29 09:53 .pki
-rw-r--r--. 1 root root 1.8K Sep 9 19:19 asset-manifest.json
-rw-r--r--. 1 root root 3.8K Sep 9 19:18 favicon.ico
-rw-r--r--. 1 root root 548 Sep 9 19:19 index.html
-rw-r--r--. 1 root root 317 Sep 9 19:18 manifest.json
drwxr-xr-x. 2 default root 6 Aug 29 10:42 nginx-start
-rw-r--r--. 1 root root 4.7K Sep 9 19:19 service-worker.js
drwxr-xr-x. 5 root root 37 Sep 9 19:19 static
Comments:
For comments, please visit https://plus.google.com/u/0/+JefferyBagirimvano/posts/Brw773DGuTn