Answer a question

This is the output with custom-columns

$ kubectl -n web get pod -ocustom-columns="Name:.metadata.name,Image:.spec.containers[0].image"
Name      Image
mysql-0   myrepo.mydomain.com/mysql:5.7
mysql-1   myrepo.mydomain.com/mysql:5.7
mysql-2   myrepo.mydomain.com/mysql:5.7

This is the output with jsonpath for single pod

$ kubectl -n web get pod -o jsonpath="{..metadata.name}:{..spec.containers[0].image}" mysql-0
mysql-0:myrepo.mydomain.com/mysql:5.7

This is the output with jsonpath for multiple pods

$ kubectl -n web get pod -o jsonpath="{..metadata.name}:{..spec.containers[0].image}"
mysql-0 mysql-1 mysql-2:myrepo.mydomain.com/mysql:5.7 myrepo.mydomain.com/mysql:5.7 myrepo.mydomain.com/mysql:5.7

Now how to combine this into a single column or word, something like this, using -ocustom-columns or -ojsonpath

mysql-0=myrepo.mydomain.com/mysql:5.7
mysql-1=myrepo.mydomain.com/mysql:5.7
mysql-2=myrepo.mydomain.com/mysql:5.7

Answers

Using kubectl using plain jsonpath:

kubectl get pod -n <namespace> -o jsonpath='{range .items[*]}{.metadata.name}={.spec.containers[*].image}{"\n"}{end}'

Example:

kubectl get pod -n default -o jsonpath='{range .items[*]}{.metadata.name}={.spec.containers[*].image}{"\n"}{end}'
nginx-0=nginx
nginx-1=nginx
nginx-2=nginx

Here range feature is used to loop over all the pods:

{range items[*]} ...<LOGIC HERE>... {end}

Between the range block(As described above), use the jsonpath, notice the = sign is used as per our requirement.

{.metadata.name}={.spec.containers[*].image}{"\n"}
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐