In this article we will learn how to use JSONPath in Kubernetes to easily fetch specific fields from any of your existing resources in your Kubernetes cluster.
Let’s start!
Introduction
First of all, to be able to follow this article, you will need kubectl installed on your machine.
With kubectl we can perform multiple actions on our Kubernetes cluster in an easy manner. For example, if we wanted to get a list of running pods in JSON format we’d have to run something like this:
kubectl --context my-context -n my-namespace get pods -o json
This will print a big chunk of JSON, which will be fine if you just want to take a look at the whole output but, this won’t be very readable or useful in many cases.
That’s where JSONPath comes to the rescue, we can filter individual fields of our JSON output to build a more comprehensible output. If you need to understand how JSONPath works first, you could read our article “How to Use JSONPath Expressions”.
We’re going to look into a few example to be able to understand how can we use JSONPath in kubectl to gather information easily.
Get Nth Element in Array
This first example is quite straightforward if you have some basics in JSONPath syntax. We are going to get only the data for an element in the “n” position of an array. For instance, let’s get the first element (0-indexed):
$ kubectl --context my-context -n my-namespace get pods -o jsonpath='{$.items[0]}' | jq
You can see how we get the first element from the items
object contained in the Kubernetes JSON response by using []
notation. You will also notice that we’re passing the output to the jq
command, this is because every time you get a JSON object in your terminal, it won’t be “pretty-formatted”. You can achieve that easily by installing jq using your package manager. For instance, brew install jq
.
Let’s now look at more specific examples.
Get Single Field from Nth Element in Array
This example is based on the previous example, so we’ll get the first element and fetch a single field from the JSON response. This would look like the following:
kubectl --context my-context -n my-namespace get pods -o jsonpath='{$.items[0].status.hostIP}'
This should return a single IP address, for example:
10.154.198.114
Get Single Field From Each Element in Array
Our next example will be to obtain the IP address for each running pod in our deployment. We’d like to get a plain list of IP addresses that, potentially, we could use to perform a further action.
How would this look like then?
$ kubectl --context my-context -n my-namespace get pods -o jsonpath={$.items[*].status.hostIP}
10.154.196.228 10.154.202.136 10.154.201.54
This will return a list of host IP addresses in one line.
What if we want to display a list of IPs in a different format? For example, a vertical list with a break line after each IP? Let’s see how!
$ kubectl --context my-context -n my-namespace get pods -o jsonpath='{range .items[*]}{.status.hostIP}{"\n"}{end}'
10.154.196.228
10.154.202.136
10.154.201.54
In the example above we make use of the “range/end” notation to iterate a list and do something for each item, in this case just add a break line.
What if we wanted to include multiple fields in the output? Knowing “range/end” notation, this is quite easy!
Get Multiple Fields From Each Element in Array
The following example will also make use of range and end keywords and it’ll be simpler than what it looks like.
We are going to include an additional phase
field indicating if the pod is running. It looks like this:
$ kubectl --context my-context -n my-namespace get pods -o jsonpath={range .items[*]}{.status.hostIP}{"\t"}{.status.phase}{"\n"}{end}
10.154.196.228 Running
10.154.202.136 Running
10.154.201.54 Running
As you can see, we now have a list of IP addresses together with the phase
field, indicating if the pod is running or not.
Using Regular expressions
Something worth mentioning before you waste time trying it, is that kubectl does not allow regular expressions using JSONPath. The simplest solution is to use jq instead to be able to parse the JSON output using regular expressions.
You can check how to use test()
, match()
or other functions with regular expressions support in jq in the following link.
If you are interested in learning more about Kubernetes, we highly recommend the following books:
Conclusion
In this article we’ve learned some basic on how to use JSONPath in Kubernetes to be able to filter data from Kubernetes responses.
That’s all from us today! We hope you’ve found this article useful and hopefully learned something new!
Thanks for reading us and we hope to see you soon with us again!
You must log in to post a comment.