How to Format the Output of “kubectl” Command

By default, the output from the “kubectl” command will be easily readable by humans, But it can be further formatted based on our needs with the “-o” flag
kubectl [command] [TYPE] [NAME] -o
Below are the commonly used format
- -o json
- -o name
- -o wide
- -o yaml
- Using JSONPath expressions
Using -o json format
-o json will provide a JSON formatted API object
Example:
Output with JSON format:
kubectl create namespace test-123 --dry-run -o json
{
"kind": "Namespace",
"apiVersion": "v1",
"metadata": {
"name": "test-123",
"creationTimestamp": null
},
"spec": {},
"status": {}
}
Using -o yaml format
-o yaml will provide a YAML formatted API Object
Output with YAML format:
kubectl create namespace test-123 --dry-run -o yaml
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: null
name: test-123
spec: {}
status: {}
Using -o wide format
-o wide helps us to get the output with more additional information.
It’s the most commonly used output format to get additional information about the resource
Example:
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox 1/1 Running 0 3m39s 10.20.0.2 node01
ningx 1/1 Running 0 7m32s 10.21.0.1 node02
redis 1/1 Running 0 3m59s 10.22.0.1 node03
master $
Using -o name format
-o name help to print the resource name and no other extra information
Example:
kubectl get pod -o name
pod/frontend-deployment_1
pod/frontend-deployment_2
pod/frontend-deployment_3
pod/frontend-deployment_4
Using JSONPath expressions
Kubectl uses a JSON path to filter out a particular topic from the resource definition file and provide it as an output. This expression is enclosed in the curly bracket {}
For example to get all the names from the running pods using the JSON path expression
YAML FILE:
apiVersion: v1
kind: Pod
metadata:
labels:
name: busybox-pod
pod-template-hash: 6d8c45b946
name: frontend-deployment_1
namespace: default
ownerReferences:
- apiVersion: apps/v1
controller: true
kind: ReplicaSet
name: frontend-deployment_1
spec:
containers:
- command:
- sh
- -c
- echo Hello Kubernetes! && sleep 3600
image: busybox888
imagePullPolicy: Always
name: busybox-container
resources: {}
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: kube-api-access-qnq5f
readOnly: true
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: kube-api-access-qnq5f
projected:
defaultMode: 420
To get the pod name using JSON path expression
kubectl get pods -o=jsonpath='{.items[0].metadata.name}'
Output
frontend-deployment_1
To get the pod name from all running pods using JSON path expression
kubectl get pods -o=jsonpath='{.items[*].metadata.name}'
Output:
frontend-deployment_1 frontend-deployment_2 frontend-deployment_3 frontend-deployment_4
Good Luck with your Learning !!