I use a tmuxinator to generate a tmux window that watchs the output of some kubectl commands like:
watch -n 5 kubectl get pods/rc/svc/pv/pvc
But sometimes the output of Kubectl get too wide, i.e the selector column after rolling updates, and I would like not to show it on my setup. How do I do this with kubectl alone?
awk or cut can do the job too, but I could not figure out a way of doing this without loosing the table formating.
Rather than using a second tool/binary like awk and column. you can use the flag -o=custom-columns
in this way:
kubectl get pods --all-namespaces -o=custom-columns=NAME:.metadata.name,Namespace:.metadata.namespace
This is also an alternative and easy way to output custom columns than go-templates or jsonpath!
There is no explicit support for selecting a subset of columns in kubectl, but there are a few ways to accomplish this. You already mentioned awk
which can be pared with column -t
to get a nice table format:
$ kubectl get pods --all-namespaces | awk {'print $1" " $2'} | column -t
NAMESPACE NAME
kube-system fluentd-cloud-logging-k8s-stclair-minion-wnzd
kube-system kube-dns-v10-fo6gl
kube-system kube-proxy-k8s-stclair-minion-wnzd
...
Alternatively, you could use the go-template output of kubectl to create a custom output (which you could also pair with column), e.g. to print pod names and the first 8 characters of the UID:
$ kubectl get pods --all-namespaces -o=go-template='{{println "NAME UID"}}{{range .items}}{{.metadata.name}} {{printf "%.8s\n" .metadata.uid}}{{end}}' | column -t
NAME UID
fluentd-cloud-logging-k8s-stclair-minion-wnzd 8bcb7129
kube-dns-v10-fo6gl 90bce35e
kube-proxy-k8s-stclair-minion-wnzd 8bc752c8
kubernetes-dashboard-v0.1.0-cptxn 90d18852
l7-lb-controller-v0.5.2-n6i23 90daf833
More on go-templates here. A variant of JSONpath is also supported.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With