WEBVTT

00:00.160 --> 00:01.810
Mumshad Mannambeth:
Hello, and welcome to this demo.

00:01.980 --> 00:04.130
My name is Mumshad Mannambeth.

00:04.620 --> 00:05.800
In this video,

00:05.960 --> 00:09.460
we will look at
how to use JSON Path in Kubernetes

00:09.680 --> 00:11.400
with a KubeCtl utility.

00:11.740 --> 00:13.100
We will start by discussing

00:13.270 --> 00:16.290
why you might want to use
JSON Path in the first place.

00:16.480 --> 00:20.090
We will then look at viewing
and interpreting KubeCtl output

00:20.260 --> 00:21.570
in JSON format

00:21.890 --> 00:24.360
post which, we look
at the different steps involved

00:24.530 --> 00:26.180
in using JSON Path

00:26.410 --> 00:28.390
with the KubeCtl utility.

00:28.570 --> 00:31.100
We then look at several
JSON Path examples

00:31.270 --> 00:34.770
and then go through loops
before finally looking at

00:34.940 --> 00:38.740
custom columns
and sort functionality of KubeCtl.

00:39.010 --> 00:41.780
Once done, you will go through
a set of practice tests

00:41.940 --> 00:44.460
where you get to practice
what you learn

00:44.680 --> 00:46.970
through some fun
and challenging exercises.

00:47.140 --> 00:48.240
Let's get started.

00:48.400 --> 00:49.780
Now, before you begin,

00:50.010 --> 00:53.190
you must know
how to work with JSON Path.

00:54.130 --> 00:55.490
That's a prerequisite.

00:56.000 --> 00:59.370
If you have never worked
with JSON Path queries before,

00:59.540 --> 01:02.360
check out the videos
and practice tests on that first.

01:02.620 --> 01:04.670
They're available
for free on my YouTube

01:04.840 --> 01:06.790
or on the KodeKloud website.

01:07.160 --> 01:08.930
If you have worked with it already,

01:09.100 --> 01:10.100
let's continue.

01:10.290 --> 01:15.480
Also, it's good to first practice
JSON Path on Kubernetes dataset

01:15.640 --> 01:18.300
so that you know
how to navigate through it

01:18.470 --> 01:22.640
before actually working
on JSON Path with KubeCtl utility.

01:23.430 --> 01:25.200
We also have a set of exercises

01:25.360 --> 01:28.030
on working with JSON Path
on Kubernetes dataset.

01:28.360 --> 01:30.110
Make sure you go
through that as well.

01:30.560 --> 01:32.830
In this video, our focus will be on

01:32.990 --> 01:37.570
how to use JSON Path queries
with the KubeCtl utility.

01:38.130 --> 01:40.680
Why JSON Path on the first place?

01:41.770 --> 01:45.870
When you're working with production
environments for Kubernetes,

01:46.140 --> 01:49.330
you will need to view information
about hundreds of nodes

01:49.500 --> 01:53.580
and thousands of objects like
deployments, pods, and replica sets,

01:53.750 --> 01:55.980
and services and secrets, et cetera.

01:56.660 --> 01:59.010
You will be using KubeCtl utility

01:59.180 --> 02:01.840
to view information
about these objects.

02:02.430 --> 02:04.320
You will often have requirements

02:04.520 --> 02:07.390
where you will need to print
summary of different states

02:07.560 --> 02:09.130
about different resources.

02:09.440 --> 02:13.140
You will want to view specific fields
of all resources,

02:13.450 --> 02:17.540
query data about the resources based
on different criteria, et cetera.

02:18.100 --> 02:20.680
Viewing such information
by going through

02:20.850 --> 02:24.410
thousands of these resources
is an overwhelming task,

02:24.580 --> 02:28.550
which is why KubeCtl
supports a JSON Path option

02:28.710 --> 02:32.390
that makes filtering data
across large datasets,

02:32.770 --> 02:35.730
using complex criteria, an easy task.

02:35.890 --> 02:37.230
Let's take a step back

02:37.400 --> 02:40.480
and understand
how the KubeCtl utility works.

02:41.030 --> 02:44.370
We know that the KubeCtl utility
is the Kubernetes CLI.

02:44.540 --> 02:47.400
We use it for reading
and writing Kubernetes objects.

02:47.660 --> 02:50.270
Every time you run a KubeCtl command,

02:50.630 --> 02:54.720
it interacts with the Kubernetes API
through the Kube API server.

02:55.180 --> 02:58.260
The Kube API server
speaks the JSON language

02:58.430 --> 03:00.840
so it returns
the request of information

03:01.010 --> 03:02.560
in a JSON format.

03:03.040 --> 03:04.550
The KubeCtl utility,

03:04.720 --> 03:07.740
on receiving the information
in a JSON format,

03:08.090 --> 03:10.940
converts it
into a human readable format

03:11.110 --> 03:13.090
and prints it out to our screen.

03:13.920 --> 03:15.280
During that process,

03:15.440 --> 03:19.780
a lot of information that came in
in the JSON format is hidden

03:20.120 --> 03:22.900
in an effort to make
the output readable

03:23.070 --> 03:25.900
by showing
only the necessary details.

03:26.440 --> 03:29.140
If you'd like to see
additional details,

03:29.310 --> 03:32.570
you could use the "-o wide" option

03:32.730 --> 03:34.710
with the KubeCtl get command.

03:35.230 --> 03:37.020
This prints additional details,

03:37.190 --> 03:39.240
but again, this is not complete.

03:39.570 --> 03:43.410
There are still a lot more details
that are not part of this output.

03:43.590 --> 03:44.590
For example,

03:44.720 --> 03:47.560
the resource capacity
available on these nodes

03:47.730 --> 03:50.880
and the taints set on the nodes,
the conditions of the nodes,

03:51.080 --> 03:52.550
the hardware architecture,

03:52.720 --> 03:55.110
the images available
on these nodes, et cetera.

03:55.780 --> 03:58.750
You can see them if you're
on the KubeCtl describe command,

03:59.180 --> 04:01.460
but what if you want
to see it like a report?

04:02.300 --> 04:04.670
For example,
say I'd like to see the nodes

04:04.830 --> 04:07.950
and their CPU counts
in a tabular format like this.

04:08.250 --> 04:11.250
Or the list of nodes
and the taints set on them,

04:11.420 --> 04:15.030
the architecture,
or say print the list of pods

04:15.200 --> 04:16.670
and the images they use.

04:17.320 --> 04:21.100
None of the built-in commands
can give me these in this format.

04:21.580 --> 04:24.520
That's where
JSON Path queries can help.

04:24.880 --> 04:27.440
With JSON Path queries,
you can filter

04:27.610 --> 04:30.710
and format the output
of a command as you like,

04:30.880 --> 04:33.110
and that's what
we will see in this lecture

04:33.280 --> 04:36.340
and in the practice exercises
that follow this video.

04:36.750 --> 04:39.870
In order to get started
with JSON Path in KubeCtl,

04:40.030 --> 04:41.810
you must follow these four steps.

04:42.340 --> 04:44.570
First, you need to know the command

04:44.730 --> 04:48.440
that will give you the required
information in the raw format.

04:48.620 --> 04:49.620
For example,

04:49.790 --> 04:51.950
if you need information
regarding nodes,

04:52.120 --> 04:55.050
then you must use
the "kubectl get nodes" command.

04:55.350 --> 04:57.570
If you need
information regarding pods,

04:57.730 --> 05:00.680
you must use
the "kubectl get pods" command.

05:01.150 --> 05:04.230
We have seen a lot of these commands
throughout this course.

05:04.410 --> 05:05.960
So that should be easy.

05:06.530 --> 05:08.240
Once you identify the command,

05:08.410 --> 05:11.130
inspect its output in JSON format.

05:11.300 --> 05:13.150
For these, add the option

05:13.310 --> 05:15.610
"-o json" to the command.

05:15.780 --> 05:18.460
It will print the output
in a JSON format.

05:18.770 --> 05:21.880
This is the same format
of documents you worked with

05:22.040 --> 05:24.950
during the JSON practice test
on Kubernetes objects

05:25.120 --> 05:27.490
that I mentioned
in the beginning of this video.

05:28.210 --> 05:31.630
The next step is to look through
the structure of the JSON document

05:31.800 --> 05:33.660
and form the JSON Path query

05:33.830 --> 05:36.390
that will retrieve
the required information for you.

05:36.560 --> 05:38.360
For example, to get the image,

05:38.530 --> 05:44.450
you would use the query
".items[0].spec.containers[0].image".

05:45.090 --> 05:49.940
Finally, use the query you developed
with the same KubeCtl command.

05:50.320 --> 05:53.780
To do that,
use the "-o jsonpath" option

05:53.940 --> 05:57.850
and pass in the same JSON Path query
that you just developed.

05:58.360 --> 06:02.260
Remember, you must encapsulate
the JSON Path query

06:02.420 --> 06:06.110
within a pair of single quotes
and curly braces like this.

06:07.000 --> 06:11.170
We now have our KubeCtl command
with the JSON Path query.

06:11.880 --> 06:13.930
If you're a beginner to JSON Path

06:14.100 --> 06:15.570
and to this kind of logic,

06:15.860 --> 06:17.070
then I would recommend

06:17.240 --> 06:19.140
strictly following this approach

06:19.310 --> 06:22.170
where you first view
the JSON version of the output,

06:22.590 --> 06:25.870
copy the output
to a JSON Path query evaluator like

06:26.040 --> 06:27.400
jsonpath.com,

06:27.710 --> 06:28.710
then play around with it

06:28.880 --> 06:31.870
until you come up
with the right JSON Path query,

06:32.180 --> 06:35.180
and then move that query
to the KubeCtl command.

06:35.950 --> 06:38.990
With that, you should be able
to come up with JSON Path queries

06:39.160 --> 06:41.660
to pull useful information
like the below.

06:42.370 --> 06:47.620
A JSON Path query
of ".items[*].metadata.name"

06:47.790 --> 06:50.060
with a "kubectl get nodes" command

06:50.370 --> 06:52.670
gives us the names
of the nodes in the cluster.

06:53.400 --> 06:57.740
A query of
".items[*].status.nodeInfo.architecture"

06:58.090 --> 07:00.790
returns me the hardware architecture
of the nodes,

07:01.730 --> 07:04.350
and "status.capacity.cpu"

07:04.520 --> 07:07.950
returns me
the count of CPUs on these nodes.

07:08.620 --> 07:11.460
Finally,
I can merge these queries together

07:11.630 --> 07:13.430
into a single command.

07:13.610 --> 07:16.010
For example,
I could use the first query

07:16.170 --> 07:17.360
to get the node names

07:17.530 --> 07:19.360
and add the query I used

07:19.670 --> 07:21.690
to get the CPU count to it.

07:22.180 --> 07:25.220
That gets me both the results
in a single command.

07:26.250 --> 07:29.700
That's good. I can get
any information from the JSON output

07:29.870 --> 07:33.680
as long as I can figure out
the right JSON Path queries for it.

07:34.220 --> 07:36.340
That's still not pretty, is it?

07:36.920 --> 07:38.420
That's not how I want it to look.

07:38.910 --> 07:42.280
Let's look at some of the predefined
and formatting options.

07:42.790 --> 07:46.140
In this, I have two types
of information in the same line.

07:46.460 --> 07:47.770
I can insert a new line

07:47.930 --> 07:49.550
by adding the new line parameter

07:49.720 --> 07:52.190
in between the two queries like this.

07:52.550 --> 07:54.940
The "
" is for new line,

07:55.440 --> 07:58.170
and "\t" is for tab.

07:58.730 --> 08:01.600
We will now look
at loops using ranges.

08:02.180 --> 08:04.580
This might be a bit advanced
for some users,

08:04.750 --> 08:07.470
so don't worry
if you don't fully get it.

08:07.960 --> 08:10.380
From a certification
and exam standpoint,

08:10.550 --> 08:11.910
we've covered what we need.

08:12.690 --> 08:15.730
I'd like to cover this as well
before we wind up.

08:16.530 --> 08:18.880
With the queries
we have built so far,

08:19.040 --> 08:21.660
we were able to get
different sets of information like

08:21.830 --> 08:25.320
names of nodes, the CPU counts,
et cetera, in this format.

08:26.040 --> 08:27.350
This is not what we really want.

08:27.510 --> 08:29.620
We want it to look like
this one on the right.

08:30.270 --> 08:31.850
The node names in one column,

08:32.160 --> 08:33.890
and CPU counts in the other.

08:34.610 --> 08:38.390
This is where we use loops
to iterate through items in a list

08:38.560 --> 08:40.710
and print properties of each item.

08:42.240 --> 08:45.360
We will focus
on the JSON query part alone for now.

08:46.080 --> 08:48.710
When I run
the "kubectl get nodes" command,

08:49.060 --> 08:50.630
I get a list of items,

08:51.040 --> 08:53.260
each item representing a node.

08:53.660 --> 08:55.260
I want to be able to say

08:55.510 --> 08:57.590
for each item or a node,

08:57.860 --> 08:59.000
print the node name,

08:59.380 --> 09:01.790
then insert a tab as a separator,

09:02.080 --> 09:05.840
and then print the CPU count
followed by a new line character.

09:06.080 --> 09:08.920
This will get me the results
in the format I want.

09:09.800 --> 09:12.230
To specify the for-each statement,

09:12.390 --> 09:13.980
use the range keyword;

09:14.610 --> 09:16.670
"range.items[*]"

09:16.920 --> 09:18.870
means for each item.

09:19.750 --> 09:21.000
To print the node name,

09:21.170 --> 09:23.330
use the same query we built earlier;

09:23.700 --> 09:25.440
".metadata.name",

09:25.910 --> 09:27.090
then to add a tab,

09:27.260 --> 09:29.270
add the "\t" keyword.

09:29.440 --> 09:31.050
Then to print CPU count,

09:31.220 --> 09:34.950
use the query ".status.capacity.cpu",

09:35.130 --> 09:36.520
and then to add a new line,

09:36.690 --> 09:38.400
add the "
" keyword.

09:39.080 --> 09:42.450
Finally, end the loop
using the "end" keyword.

09:43.220 --> 09:45.720
Merge it all into a single line

09:45.850 --> 09:47.510
and pass it as a parameter

09:47.690 --> 09:50.830
to the JSON Path option
of the KubeCtl command.

09:53.150 --> 09:54.890
You can also use JSON Path

09:55.060 --> 09:57.730
for printing custom columns
with KubeCtl.

09:58.370 --> 10:00.480
At times, this is an easier approach

10:00.650 --> 10:02.720
when compared
to using the loop method.

10:03.330 --> 10:04.620
Let's take the previous example

10:04.790 --> 10:06.540
where we use the JSON Path option

10:06.710 --> 10:09.080
to print node names
and CPU capacities.

10:09.850 --> 10:13.360
What we really want is to print them
as separate columns,

10:13.650 --> 10:15.010
one column with the node name,

10:15.170 --> 10:16.880
and another with the CPU count.

10:17.740 --> 10:21.570
Instead of using the JSON Path option
directly with the command,

10:21.990 --> 10:24.350
you could use
the custom columns option

10:24.510 --> 10:25.930
of the KubeCtl command.

10:26.250 --> 10:27.980
The custom columns option

10:28.260 --> 10:31.380
takes a set of column name
and JSON Path options.

10:31.800 --> 10:35.300
For example, for the first column,
the column name is node.

10:35.700 --> 10:38.440
The JSON Path corresponding
to the node name

10:38.680 --> 10:41.020
is .metadata.name.

10:41.800 --> 10:45.150
Note that you must exclude
the items section of the query

10:45.320 --> 10:47.310
as the custom columns assumes

10:47.610 --> 10:50.120
the query is for each item
in the list.

10:50.290 --> 10:52.020
This prints the first column

10:52.250 --> 10:54.360
with the given column name
and its data.

10:54.920 --> 10:57.420
Similarly,
you can add additional columns

10:57.700 --> 11:01.250
by adding additional column
and JSON Path pairs

11:01.440 --> 11:02.810
separated by a comma.

11:03.440 --> 11:05.890
We add the next column named CPU

11:06.140 --> 11:10.300
with this data
at .status.capacity.cpu,

11:11.150 --> 11:12.760
much easier and cleaner.

11:12.990 --> 11:15.010
Again, for this too,
I would recommend

11:15.180 --> 11:17.310
first coming up
with the JSON Path queries

11:17.480 --> 11:18.480
for each column,

11:18.650 --> 11:20.870
and then putting them together
in the command.

11:21.790 --> 11:24.400
Finally, JSON Path can also be used

11:24.540 --> 11:28.330
while sorting objects
by specifying the sort by option.

11:29.170 --> 11:32.420
The KubeCtl command comes
with a sort by option

11:32.590 --> 11:35.620
where you can sort the output
based on the value of a property

11:35.790 --> 11:38.730
from the JSON formatted properties
of each item.

11:39.910 --> 11:42.350
When you run
the "kubectl get nodes" command,

11:42.520 --> 11:45.800
specify the JSON Path query
we developed earlier,

11:46.350 --> 11:48.460
as is, in the sort by option

11:48.630 --> 11:51.540
to sort based on name or CPU count.

11:55.860 --> 11:57.290
That's it for this lecture.

11:57.500 --> 11:58.810
Head over to the practice tests

11:58.970 --> 12:00.960
and practice working with JSON Path

12:01.130 --> 12:03.280
and some advanced KubeCtl commands.

