Certified Kubernetes Administrator Exam Series (Part-10): Practice Topics

JSON PATH  

JavaScript Object Notation (JSON) is a lightweight data storage and sharing format that stores data objects in human-readable key-value pairs. When writing JSON data, it is written as a field (name) followed by a colon and its value. Kubernetes configuration files can be written in JSON. 

JSONPath is a query language used in the expressions that allow for the interactive discovery and extraction of JSON structures. With JSONPath, user requests can be reduced to the relevant services being requested, effectively minimizing the bandwidth and latency of response from the server. 

The Kubernetes control tool (kubectl)supports the JSONPath language format. The tool can read configuration files written in the JSON format, and facilitates JSONPath expressions in the command line for discovery. 

Working with JSON PATH

There are 4 main syntax rules that govern the creation and discovery of JSON data structures:

  • Data is written in key-value pairs
  • Individual data entries are separated by commas
  • JSON objects are held in curly braces
  • Object arrays are held within square brackets

A key-value pair is denoted by the field name (key) followed by a colon, then its respective value. If we want to input the name John in the field firstName for a particular data set, its JSON notation will be similar to: 

"firstName": "John"

An object includes a collection of data sets, so a JSON object will have several key-value pairs. If we wanted to add the last name Doe to the above data set, the object will look similar to:

{"firstName":"John", "lastName":"Doe"} 

An array contains several objects. If the above data set was part of an employee database, the JSON notation for the employee database will be similar to: 

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Georgia", "lastName":"Banks"},
    {"firstName":"Marshall", "lastName":"Jones"}
]
}

The JSON configuration file for a simple Kubernetes POD will be similar to:

{
  "kind": "Pod",
  "apiVersion": "v1",
  "metadata": {
    "name": "hello-openshift",
    "creationTimestamp": null,
    "labels": {
      "name": "hello-openshift"
    }
  },
  "spec": {
    "containers": [
      {
        "name": "hello-openshift",
        "image": "openshift/hello-openshift",
        "ports": [
          {
            "containerPort": 8080,
            "protocol": "TCP"
          }
        ],
        "resources": {},
        "volumeMounts": [
          {
            "name":"tmp",
            "mountPath":"/tmp"
          }
        ],
        "terminationMessagePath": "/dev/termination-log",
        "imagePullPolicy": "IfNotPresent",
        "securityContext": {
          "capabilities": {},
          "privileged": false
        }
      }
    ],
    "volumes": [
      {
        "name":"tmp",
        "emptyDir": {}
      }
    ],
    "restartPolicy": "Always",
    "dnsPolicy": "ClusterFirst",
    "serviceAccount": ""
  },
  "status": {}
}

Advanced Kubectl Commands

kubectl is the Kubernetes command-line interface and it helps to manage & deploy Kubernetes applications and for building application frameworks. When querying the Kubernetes API, the tool typically returns minimal text in a human-readable format. The Kubernetes ecosystem is large, consisting of many services, machines and large data sets. JSON makes the complex data structures in Kubernetes easy to explore. JSONPath can help cluster administrators and application users to extract formatted and filtered information from kubectl command outputs. 

Practising with Kubectl  

This section takes the candidate through the process of creating JSONPath expressions to format and filter the outputs of kubectl commands. 

The output of the  command can be viewed in JSON format by appending the command with -o json, i.e, for the kubectl get nodes operation, the command used to obtain node information in JSON format will be: 

$ kubectl get nodes -o json

By viewing the output of the above command, it is possible to create an expression that boxes the result into an array that combines different objects and their attributes in tabular format. 

Kubernetes also allows for the creation of custom columns, which means administrators can selectively view the object details as needed. This is done by appending the  command with -o custom-columns=<spec> for instance: 

$ kubectl get nodes <node-name> -o custom-columns=NAME:.metadata.name,RSRC:.metadata.resourceVersion

Cluster objects can also be output to the terminal window in a sorted list. To achieve this, the kubectl command is appended with a --sort-by flag that points to a field of objects referred to in a JSONPath expression. For instance, to print a list of nodes sorted by name, the following command is used:

$ kubectl get nodes --sort-by=.metadata.name

The complete list of advanced kubectl commands and operations can be seen here.

Tips for Exam Success

This course extensively covers the knowledge areas required to create and run working Kubernetes clusters. The course is, therefore, wide and requires focus and discipline for complete understanding. Here are a few tips to make the most of the CKA practice:

  1. Always refer to the Official Kubernetes Documentation page for useful information and commands
  2. Practice time management as the exam only allows about 6 minutes per question
  3. Learn to use the vi text editor and commands
  4. Learn some basic Linux commands
  5. Set study goals and deadlines 
  6. Bookmark key resources on the documentation page that you could refer to during the exam
  7. Follow the official Kubernetes Slack channel for conversations and contributions with fellow CKA candidates and established practitioners.

1. Get the details of the node node01 in JSON format and store it in the file /opt/outputs/node01.json.

Use the command

$ kubectl get node node01 -o json > /opt/outputs/node01.json

2. Use JSON PATH query to fetch node names and store them in /opt/outputs/node_names.txt.

Use the command

$ kubectl get nodes -o=jsonpath='{.items[*].metadata.name}' > /opt/outputs/node_names.txt

3. Use a JSON PATH query to identify the context configured for the aws-user in the my-kube-config context file and store the result in /opt/outputs/aws-context-name.

Use the command

$ kubectl config view --kubeconfig=my-kube-config -o jsonpath="{.contexts[?(@.context.user=='aws-user')].name}" > /opt/outputs/aws-context-name

Summary

As the final blog of the Certified Kubernetes Administrator (CKA) exam, this course covers JSON, and how JSONPath expressions can be used to customize kubectl outputs. With the knowledge gained from this class, a candidate can create commands that allow for easy access to information about cluster objects. JSONPath is used to format outputs into custom columns or sort them using specifications. Combining commands with JSONPath expressions, therefore, helps simplify cluster monitoring and management tasks. 

This brings us to the end of the CKA series. We hope the course in its entirety has been helpful and it gives you enough confidence to take the test. For comments, suggestions or questions, please do not hesitate to contact us back. 

Best wishes from KodeKloud! And do not forget to share the news once you have successfully cleared the certification. 

More details about KodeKloud’s CKA course with access to the lessons, labs, mock exams and demo can be found here – https://kodekloud.com/courses/certified-kubernetes-administrator-cka/.

Related Articles

Responses

Your email address will not be published. Required fields are marked *