Using Kubectl with JSON PATH

Using Kubectl with JSON PATH

kubectl allows you to run commands against Kubernetes clusters. You can use kubectl to complete various actions, such as deploy applications, inspect and manage cluster resources, and view logs.

When working with large datasets JSON Path can be an invaluable tool on the command line. It allows us to view data in a human readable format which is normally out of sight via standard kubectl commands. The -o wide flag does give us some extra info but it still hides a lot. We could use the -o json flag to view the whole json object but instead we’ll look at how we can use the json output with JSON Path to view only the data we want.

JSON Path Examples

Below are some examples along with their output, note that the $. which identifies the root object in JSON Path is not mandatory as kubectl adds this automatically.

> kubectl get nodes -o jsonpath='{.items[*]}'
kubemaster kubenode01 kubenode02
> kubectl get nodes -o jsonpath='{.items[*].status.nodeInfo.architecture}'
amd64 amd64 amd64
> kubectl get pods --all-namespaces -o jsonpath='{.items[*].spec.containers[*].image}' grafana/grafana:6.1.6 prom/prometheus:v2.3.2 kubernetesui/metrics-scraper:v1.0.4 kubernetesui/dashboard:v2.0.0

We can use multiple queries in a single kubectl command.

> kubectl get nodes -o jsonpath='{.items[*]}{"\n"}{.items[*].status.nodeInfo.osImage}{"\n"}{.items[*].status.nodeInfo.architecture}'
kubemaster       kubenode01             kubenode02
Ubuntu 18.04.5 LTS Ubuntu 18.04.5 LTS Ubuntu 18.04.5 LTS
amd64              amd64                     amd64

We can also leverage range loops to iterate through items, the below loop extracts the same data as the above command but by using a range loop to iterate through all items.

> kubectl get nodes -o jsonpath='{range .items[*]} {}{"\t"}{.status.nodeInfo.osImage}{"\t"}{.status.nodeInfo.architecture}{"\n"}{end}' 
kubemaster    Ubuntu 18.04.5 LTS	  amd64
kubenode01   Ubuntu 18.04.5 LTS	  amd64
kubenode02   Ubuntu 18.04.5 LTS    amd64

Custom columns are the preferred approach when trying to achieve a similar query as above. Instead of adding the tabs and new lines ourselves the custom column option takes care of this for us, it also gives us fancy column headers! Depending on what you are trying to achieve it may be the easier route.

Custom columns are used via the -o custom-columns flag, columns are separated via commas.

>kubectl get nodes -o custom-columns=NODE:{},CPU:{.status.capacity.cpu},MEMORY:{.status.capacity.memory}
NODE             CPU   MEMORY
kubemaster    2       2040792Ki
kubenode01   2       2040792Ki
kubenode02   2       2040792Ki
> kubectl get pods --all-namespaces -o custom-columns=POD:{},IMAGE:{'.spec.containers[*].image'}
POD                                                                      IMAGE
grafana-788768d748-dzz4m                                grafana/grafana:6.1.6
prometheus-server-657c9f8644-dqz8l                  prom/prometheus:v2.3.2
weave-net-28wf7                                       ,
weave-net-d8vbs                                       ,
weave-net-xp6v7                                       ,
dashboard-metrics-scraper-7b59f7d4df-6blnh     kubernetesui/metrics-scraper:v1.0.4
kubernetes-dashboard-74d688b6bc-9pkbw         kubernetesui/dashboard:v2.0.0

Note that the .items section is omitted, this is because custom columns will iterate through all items in the list as we did with our range loop.

Using kubectl with json path does have a few restrictions, On Windows, you must double quote any JSON Path query that contains spaces. Regex is not supported, if regex is needed you can pipe the json output to jq to achieve the desired results. Also, kubectl’s JSONPath parser can hit issues with dots in elements, when using custom columns escaping these should solve the issue.

Depending on your workflow and how you’re managing & monitoring your Kubernetes cluster, leveraging kubectl with json path may not grant much additional insight into your clusters. However knowing how to leverage these two tools together can be a tremendous help when you need to extract very specific information.


No Comments

Add your comment