Mumshad Mannambeth: Hello, and welcome to this demo.
My name is Mumshad Mannambeth.
In this video,
we will look at how to use JSON Path in Kubernetes
with a KubeCtl utility.
We will start by discussing
why you might want to use JSON Path in the first place.
We will then look at viewing and interpreting KubeCtl output
in JSON format
post which, we look at the different steps involved
in using JSON Path
with the KubeCtl utility.
We then look at several JSON Path examples
and then go through loops before finally looking at
custom columns and sort functionality of KubeCtl.
Once done, you will go through a set of practice tests
where you get to practice what you learn
through some fun and challenging exercises.
Let's get started.
Now, before you begin,
you must know how to work with JSON Path.
That's a prerequisite.
If you have never worked with JSON Path queries before,
check out the videos and practice tests on that first.
They're available for free on my YouTube
or on the KodeKloud website.
If you have worked with it already,
let's continue.
Also, it's good to first practice JSON Path on Kubernetes dataset
so that you know how to navigate through it
before actually working on JSON Path with KubeCtl utility.
We also have a set of exercises
on working with JSON Path on Kubernetes dataset.
Make sure you go through that as well.
In this video, our focus will be on
how to use JSON Path queries with the KubeCtl utility.
Why JSON Path on the first place?
When you're working with production environments for Kubernetes,
you will need to view information about hundreds of nodes
and thousands of objects like deployments, pods, and replica sets,
and services and secrets, et cetera.
You will be using KubeCtl utility
to view information about these objects.
You will often have requirements
where you will need to print summary of different states
about different resources.
You will want to view specific fields of all resources,
query data about the resources based on different criteria, et cetera.
Viewing such information by going through
thousands of these resources is an overwhelming task,
which is why KubeCtl supports a JSON Path option
that makes filtering data across large datasets,
using complex criteria, an easy task.
Let's take a step back
and understand how the KubeCtl utility works.
We know that the KubeCtl utility is the Kubernetes CLI.
We use it for reading and writing Kubernetes objects.
Every time you run a KubeCtl command,
it interacts with the Kubernetes API through the Kube API server.
The Kube API server speaks the JSON language
so it returns the request of information
in a JSON format.
The KubeCtl utility,
on receiving the information in a JSON format,
converts it into a human readable format
and prints it out to our screen.
During that process,
a lot of information that came in in the JSON format is hidden
in an effort to make the output readable
by showing only the necessary details.
If you'd like to see additional details,
you could use the "-o wide" option
with the KubeCtl get command.
This prints additional details,
but again, this is not complete.
There are still a lot more details that are not part of this output.
For example,
the resource capacity available on these nodes
and the taints set on the nodes, the conditions of the nodes,
the hardware architecture,
the images available on these nodes, et cetera.
You can see them if you're on the KubeCtl describe command,
but what if you want to see it like a report?
For example, say I'd like to see the nodes
and their CPU counts in a tabular format like this.
Or the list of nodes and the taints set on them,
the architecture, or say print the list of pods
and the images they use.
None of the built-in commands can give me these in this format.
That's where JSON Path queries can help.
With JSON Path queries, you can filter
and format the output of a command as you like,
and that's what we will see in this lecture
and in the practice exercises that follow this video.
In order to get started with JSON Path in KubeCtl,
you must follow these four steps.
First, you need to know the command
that will give you the required information in the raw format.
For example,
if you need information regarding nodes,
then you must use the "kubectl get nodes" command.
If you need information regarding pods,
you must use the "kubectl get pods" command.
We have seen a lot of these commands throughout this course.
So that should be easy.
Once you identify the command,
inspect its output in JSON format.
For these, add the option
"-o json" to the command.
It will print the output in a JSON format.
This is the same format of documents you worked with
during the JSON practice test on Kubernetes objects
that I mentioned in the beginning of this video.
The next step is to look through the structure of the JSON document
and form the JSON Path query
that will retrieve the required information for you.
For example, to get the image,
you would use the query ".items[0].spec.containers[0].image".
Finally, use the query you developed with the same KubeCtl command.
To do that, use the "-o jsonpath" option
and pass in the same JSON Path query that you just developed.
Remember, you must encapsulate the JSON Path query
within a pair of single quotes and curly braces like this.
We now have our KubeCtl command with the JSON Path query.
If you're a beginner to JSON Path
and to this kind of logic,
then I would recommend
strictly following this approach
where you first view the JSON version of the output,
copy the output to a JSON Path query evaluator like
jsonpath.com,
then play around with it
until you come up with the right JSON Path query,
and then move that query to the KubeCtl command.
With that, you should be able to come up with JSON Path queries
to pull useful information like the below.
A JSON Path query of ".items[*].metadata.name"
with a "kubectl get nodes" command
gives us the names of the nodes in the cluster.
A query of ".items[*].status.nodeInfo.architecture"
returns me the hardware architecture of the nodes,
and "status.capacity.cpu"
returns me the count of CPUs on these nodes.
Finally, I can merge these queries together
into a single command.
For example, I could use the first query
to get the node names
and add the query I used
to get the CPU count to it.
That gets me both the results in a single command.
That's good. I can get any information from the JSON output
as long as I can figure out the right JSON Path queries for it.
That's still not pretty, is it?
That's not how I want it to look.
Let's look at some of the predefined and formatting options.
In this, I have two types of information in the same line.
I can insert a new line
by adding the new line parameter
in between the two queries like this.
The " " is for new line,
and "\t" is for tab.
We will now look at loops using ranges.
This might be a bit advanced for some users,
so don't worry if you don't fully get it.
From a certification and exam standpoint,
we've covered what we need.
I'd like to cover this as well before we wind up.
With the queries we have built so far,
we were able to get different sets of information like
names of nodes, the CPU counts, et cetera, in this format.
This is not what we really want.
We want it to look like this one on the right.
The node names in one column,
and CPU counts in the other.
This is where we use loops to iterate through items in a list
and print properties of each item.
We will focus on the JSON query part alone for now.
When I run the "kubectl get nodes" command,
I get a list of items,
each item representing a node.
I want to be able to say
for each item or a node,
print the node name,
then insert a tab as a separator,
and then print the CPU count followed by a new line character.
This will get me the results in the format I want.
To specify the for-each statement,
use the range keyword;
"range.items[*]"
means for each item.
To print the node name,
use the same query we built earlier;
".metadata.name",
then to add a tab,
add the "\t" keyword.
Then to print CPU count,
use the query ".status.capacity.cpu",
and then to add a new line,
add the " " keyword.
Finally, end the loop using the "end" keyword.
Merge it all into a single line
and pass it as a parameter
to the JSON Path option of the KubeCtl command.
You can also use JSON Path
for printing custom columns with KubeCtl.
At times, this is an easier approach
when compared to using the loop method.
Let's take the previous example
where we use the JSON Path option
to print node names and CPU capacities.
What we really want is to print them as separate columns,
one column with the node name,
and another with the CPU count.
Instead of using the JSON Path option directly with the command,
you could use the custom columns option
of the KubeCtl command.
The custom columns option
takes a set of column name and JSON Path options.
For example, for the first column, the column name is node.
The JSON Path corresponding to the node name
is .metadata.name.
Note that you must exclude the items section of the query
as the custom columns assumes
the query is for each item in the list.
This prints the first column
with the given column name and its data.
Similarly, you can add additional columns
by adding additional column and JSON Path pairs
separated by a comma.
We add the next column named CPU
with this data at .status.capacity.cpu,
much easier and cleaner.
Again, for this too, I would recommend
first coming up with the JSON Path queries
for each column,
and then putting them together in the command.
Finally, JSON Path can also be used
while sorting objects by specifying the sort by option.
The KubeCtl command comes with a sort by option
where you can sort the output based on the value of a property
from the JSON formatted properties of each item.
When you run the "kubectl get nodes" command,
specify the JSON Path query we developed earlier,
as is, in the sort by option
to sort based on name or CPU count.
That's it for this lecture.
Head over to the practice tests
and practice working with JSON Path
and some advanced KubeCtl commands.