Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format the output of kubectl describe to JSON

kubectl get command has this flag -o to format the output.

Is there a similar way to format the output of the kubectl describe command?

For example:

kubectl describe -o="jsonpath={...}" pods my-rc 

would print a JSON format for the list of pods in my-rc replication controller. But -o is not accepted for the describe command.

like image 971
4 revs, 2 users 76% Avatar asked May 26 '16 14:05

4 revs, 2 users 76%


People also ask

What is the format of commands used with kubectl?

To do this, JSON or YAML formats are accepted. In the same way, we can create multiple things as listed using the create command along with kubectl. kubectl delete − Deletes resources by file name, stdin, resource and names.

How would you describe a kubectl pod?

The kubectl describe pods command provides detailed information about each of the pods that provide Kubernetes infrastructure. If the output from a specific pod is desired, run the command kubectl describe pod pod_name --namespace kube-system .

Does Kubernetes use JSON?

Kubectl uses JSONPath expressions to filter on specific fields in the JSON object and format the output.


2 Answers

kubectl describe doesn't support -o or equivalent. It's meant to be human-readable rather than script-friendly. You can achieve what you described with kubectl get pods -l <selector_of_your_rc> -o <output_format>, for example:

$ kubectl get pods -l app=guestbook,tier=frontend -o name pod/frontend-a4kjz pod/frontend-am1ua pod/frontend-yz2dq 
like image 63
janetkuo Avatar answered Sep 28 '22 03:09

janetkuo


In my case, I needed to get the load balancer address from the service. I did it using kubectl get service:

$ kubectl -n <namespace> -ojson get service <service>  {     "apiVersion": "v1",     "kind": "Service",     [...]     "status": {         "loadBalancer": {             "ingress": [                 {                     "hostname": "internal-xxxxxxxxxxxxxxxxxxxxxxxxxxx-yyyyyyyyyy.us-east-1.elb.amazonaws.com"                 }      [...] } 
like image 32
JonDoe297 Avatar answered Sep 28 '22 05:09

JonDoe297