DEV Community

Cover image for kubectl output like a pro
dejanualex
dejanualex

Posted on

kubectl output like a pro

If your work involves kubectl, even more wrapping kubectl commands in bash scripts, then this might be for you.

Image description

So let's say you want to take only the pods names for all namespaces. You can use things la AWK, grep but also you can customize your output:

kubectl get po -o name -A
Enter fullscreen mode Exit fullscreen mode

Pretty simple, and it looks like this:

pod/harbor-jobservice-5f56f77c85-75fzj
pod/harbor-nginx-858bb8887f-8grwb
pod/harbor-portal-59ff88c985-2k878
pod/harbor-redis-0

If you don't like that fact that pod's names are prefixed with pod/...say no more:

kubectl get po -o=custom-columns=NAME:.metadata.name -A
Enter fullscreen mode Exit fullscreen mode

harbor-jobservice-5f56f77c85-75fzj
harbor-nginx-858bb8887f-8grwb
harbor-portal-59ff88c985-2k878
harbor-redis-0

Would be nice though to know the namespace in which each pod is running:

kubectl get po -A -o go-template='{{range .items}} --> {{.metadata.name}} in namespace: {{.metadata.namespace}}{{"\n"}}{{end}}'
Enter fullscreen mode Exit fullscreen mode

--> harbor-jobservice-5f56f77c85-75fzj in namespace: harbor
--> harbor-nginx-858bb8887f-8grwb in namespace: harbor
--> harbor-portal-59ff88c985-2k878 in namespace: harbor
--> harbor-redis-0 in namespace: harbor

That's about it, also this applies for Docker check this article.

Top comments (0)