Kubernetes

Kubernetes Jsonpath with Examples

When dealing with Kubernetes in a production setting, you will need to see information on hundreds of nodes and thousands of items like deployments, pods, replica sets, services, and secrets, among other things. To get this information, you will use the command kubectl CLI. However, in many cases, you will be required to filter information and get more facts beyond what the default output of kubectl provides.

Going through thousands of these resources to find such exact information would be a daunting task. As a result, kubectl includes the Jsonpath option, which makes filtering data across big data sets a breeze.

The kubectl command interacts with the Kubernetes API every time you run it. The Kube-apiserver then sends a JSON-formatted response. kubectl translates it to a human-readable format and displays it on the screen. To make an output understandable, a lot of information from the answer is buried during this procedure, leaving only the relevant fields visible. We can use the -o wide option to get more of it, but this isn’t all of it. There are many more details that aren’t presented.

You will need to install Ubuntu 20.04 on your Linux operating system to run the instructions in Kubernetes. Additionally, you must install the Minikube cluster on your machine in order to run Kubernetes on Linux. Minikube provides an extraordinarily seamless experience by allowing you to test commands and applications in a methodical manner. As a result, it provides the greatest learning environment for Kubernetes newbies.

The first step is to begin the Minikube cluster. Then, navigate to the command line terminal in Ubuntu 20.04, which you have installed. You can do so by pressing the Ctrl+Alt+T shortcut key or typing “Terminal” into the Ubuntu 20.04 system’s search box. Either of the aforementioned techniques will launch the entire terminal. The Minikube will thereafter be started. Enter the command “minikube start” in the terminal to start the Minikube. The Kubernetes cluster will be started, and a virtual machine capable of running a single node cluster will be created. It will also work on the kubectl installation. This too will interact with the cluster.

$ minikube start

https://lh4.googleusercontent.com/ne3X97_nxzz8eKkkcqMKkzOBKWpgjFqOlGz_WY2aSq4X1a4-j68yrnRulZNjj4Qr6WRyDcsWuxi-kNjT5MbJWG8rOlNSlz1zVsOEalXKppWU4VJ7XAuo-4rt2Vk5TqloU3Fbt0hd

How to Split the Output of Kubectl Jsonpath into Separate Lines?

All programmers must Google how to get kubectl to output JSON path results on distinct lines at all times. The following command, for example, retrieves the podIP for every running Pod in all namespaces. It gives you something like this as a result. That is unquestionably not the most user-friendly result.

https://lh6.googleusercontent.com/M8GKQdJ0Q0t5IjoL_wmKjza-Bu98uNA9-89EIfwS5s7WdOSeFQTWXgOIjRiVy7b5DwBSZMTsvBmyu85joN7ab29HUY47XIrGqAoEkrFJvoi0df5LvcMbIj2bgKu7ZOuArE_ghAgx

You can iterate over the list with the Jsonpath range function, adding a new line after each member with n. Awesome! We may now use all of the normal UNIX tools that act on new lines to work with the output (e.g. sort, xargs, uniq, etc.).

https://lh6.googleusercontent.com/OBflfioEGrFZ2mtHw6JZb-s8uAChmoGc1THkVIktAKRfxIFzGK1T7KyQ-2020arZBIRXJC95w66xkRGUh-TrCTXyhilg3rA5kl0VpV8jf3aS7FcvgDpxWhmSCGP5XkVmJY1AKFpi

Other whitespace characters can also be used. Assume we wanted to print the Pod namespaces/names, as well as their IP addresses, separated by a comma. It’s sometimes useful to output in Jsonpath.

https://lh3.googleusercontent.com/NCIFtfGO9vg7iM0Q0jEZ2VLWQUuIgQnMIoCpQ8XaPzJ2IK6VdCklHA2QmoiGEtaYRWwfhWW9LfbvXDuP8v1ODzM0LdESUHPsiNTHylsRBkjxI5B1CDIM1QaUPfDIsDYe1tJNR8EY

Example of Kubectl and Jsonpath Expressions

Jsonpath template is supported by Kubectl. Jsonpath expressions are encased in curly braces in the Jsonpath template. Kubectl employs Jsonpath expressions to successfully format the result. Also, filters the precise options in the JSON object. The syntax (mentioned below) is valid in addition to the inventive Jsonpath template syntax. You can also consider the below-mentioned example to understand all the elements.

  • To quote plain text inside Jsonpath expressions, insert double quotes.
  • Iterate lists utilizing the range and end operators.
  • Negative slice catalogues are used to go backward through a list.
  • @ is the current object
  • [ ] or . is the child operator
  • . . recursive descent
  • * is used to get all objects
  • [,]is the union operator
  • ” is used to quote interpreted string

Because the expression always begins from the root object by default, the $ operator is not required. The String() function is used to print the result object.

https://lh5.googleusercontent.com/mZq7yPWo8mrP_h-iAZf8DJJh7tSGbwJmqI_u4RuBhAufCzRNPldpOYdJkGLbZhS-593hd_rIuePw-GgZDWfIi0mGm1u6YiIdEv4UZtnsXorzI3sRRWu-DBkv_mG-bhccqiHuWrQu

The complete JSON input is shown in the following screenshots.

 

https://lh3.googleusercontent.com/Y1-HhAqIcMawwUhfF4hzzTh2PYAK5Lw-YDimT_PmmVIOdMgmARxN_gBNEe6zYxKoxJlFF1xGiNMFbIFcFwFk4EEXzOu3Q_XYhzy4rYsq0z104J15LX4Djuk8WeU43wTYLsiM_4lk

https://lh4.googleusercontent.com/ZB27uUPC92xhzjUcEYrHAj-YP5qSdPrDJoZPLi0u1gqGj5lm7IHSGZowrGS2lT1JKf2K7sOglFy2KX2FfcJmlM-MAym4iAQtg-JKLybYeLHwuStM2tN7eTmi_iRzMjQF-k9QL3px

In the following code, kubectl and Jsonpath expressions are used to display the output.

https://lh4.googleusercontent.com/KwPuvOzDgXdPFHxlpstHaDfdb89Bme1TLHXjr5bolyzY1cH-W3nUvERnFOl_UgTSrry7JMFAzVIIg7R2fYsHf1kAcnRuAb4_LikgCJ0rT2qysvOxhVOobcEKzvBH5So1DSHy5b1H

Any Jsonpath template that uses spaces must be enclosed in double-quoted on Windows. It should not be written in a single quote as shown above for bash. As a result, all literals in the template must be surrounded by a single quote or an escaped double quote. Consider the following piece of code.

https://lh3.googleusercontent.com/1nyAyar2_IaptcqvpI2PcbNpssl4hvxStOr-0FCoFy5kZXGTeVg5bx0JdAn0p-Tr8BfhmJWIU-aWZT9WEhJmt2M9kamA_FDp1uycOag3wVYWCvWpNWywFWu9_uiQ9cBvdyFuHi_u

Note: For Jsonpath output, kubectl does not accept regular expressions. You can use a program like jq if you wish to match using regular expressions.

The following code prints the name and start time.

https://lh3.googleusercontent.com/qsB5RdXzC5-Q06xvbYaZWi9hRp0ZNDaNnMn3sJWEhQyyzVXiq5F2Bn8MZ5-tW-pBI8yq1maofhZ5iUkfgwxR_tBPNPpuoUR0orQwH_IO39P-X1lrxYp_Vo4Da-Gt4s97DKD_BOWF

Conclusion

This guide was all about Kubernetes JSON and why are they used. We have also gone through detailed examples of how Kubectl and Jsonpath expressions work to display the specific output.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content