I’m running the following query:
$ kubectl get pods -o wide -n logging-app-global --field-selector=status.phase=Running --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
ax-fmrsp-rpt-6c476796-gzsvv 1/1 Running 1 (5h40m ago) 5h40m 10.127.3.187 ip-10-127-3-187.ec2.internal <none> <none> app=ax-fmrsp-rpt,pod-template-hash=6c476796
ax-manager-logging-sp-766574f4f9-6f8gb 1/1 Running 0 5h40m 10.127.2.87 ip-10-127-2-87.ec2.internal <none> <none> app=ax-manager-logging-sp,pod-template-hash=766574f4f9
I’m wondering if there is a programmatic way to exclude the RESTARTS
column from output (e.g. awk), instead of implementing -o custom-columns
and defining all above listed columns.
using awk and print
kubectl get pods | awk 'NR==1 {print $1, $2, $3, $5} NR>1 {print $1, $2, $3, $5}'
kubectl get pods:
Fetches the list of pods.
awk ‘NR==1 {print $1, $2, $3, $5} NR>1 {print $1, $2, $3, $5}’:
awk NR==1 {print $1, $2, $3, $5}: For the header row (NR==1), print the 1st, 2nd, 3rd, and 5th columns, excluding the 4th column ("RESTARTS").
awk NR>1 {print $1, $2, $3, $5}: For all other rows (NR>1), print the 1st, 2nd, 3rd, and 5th columns.
using Custom Columns
Use the -o custom-columns option to specify which columns you want to display. You can exclude the “RESTARTS” column by not including it in your custom columns definition.
kubectl get pods -o custom-columns=NAME:.metadata.name,READY:.status.containerStatuses[0].ready,STATUS:.status.phase,AGE:.metadata.creationTimestamp
NAME READY STATUS AGE
nginx-pod true Running 2023-10-01T12:34:56Z
another-pod true Running 2023-10-01T12:45:23Z
Try this
$kubectl get pods -n logging-app-global | awk '{print $1 $2 $3 $5}'
This command removes the RESTARTS column (which is in the 4th position).
You can add -o wide and rest of the columns in print
Another approach is:
$kubectl get pods -o wide -n logging-app-global | awk '{$4=""; print $0}'
This will print all columns except RESTARTS, basically it sets forth column to empty string.