Certified Kubernetes Administrator Exam Series (Part-10): Practice Topics
In the previous blog of this 10-part series, we discussed Troubleshooting. This blog introduces you to JSON data format and advanced Kubectl Commands.
Let's get started!
What is JSON?
JavaScript Object Notation (JSON) is a lightweight data storage and sharing format that stores data objects in human-readable key-value pairs. JSON data 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.
kubectl supports the JSONPath language format. It can read configuration files written in JSON format and facilitates JSONPath expressions in the command line for discovery.
Here is a Pod manifests in JSON:
{
"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": {}
}
Working with JSON PATH
Four main syntax rules 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 the following:
"firstName": "John"
An object includes a collection of data sets. 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 would look like the following:
{"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 would be similar to the following:
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Georgia", "lastName":"Banks"},
{"firstName":"Marshall", "lastName":"Jones"}
]
}
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 extract formatted and filtered information from kubectl
command outputs.
Practicing 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=
, for instance:
kubectl get nodes -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.
Exam Preparation Course
Our CKA Exam Preparation course explains all the Kubernetes concepts included in the certification’s curriculum. After each topic, you get interactive quizzes to help you internalize the concepts learned. At the end of the course, we have mock exams that will help familiarize you with the exam format, time management, and question types.
Explore our CKA exam preparation course curriculum.
Tips for Exam Success
This course extensively covers the knowledge areas required to create and run working Kubernetes clusters. The course is wide and requires focus and discipline for complete understanding. Here are a few tips to make the most of the CKA practice:
- Always refer to the Official Kubernetes Documentation page for useful information and commands
- Practice time management as the exam only allows about 6 minutes per question
- Learn to use the vi text editor and commands
- Learn some basic Linux commands
- Set study goals and deadlines
- Bookmark key resources on the documentation page that you could refer to during the exam
- Follow the official Kubernetes Slack channel for conversations and contributions with fellow CKA candidates and established practitioners.
Practice Questions
Below are some practice questions that test your understanding of JSON in Kubernetes.
- Get the details of the node node01 in JSON format and store it in the file
/opt/outputs/node01.json
.
Solution: 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
.
Solution: 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
.
Solution: 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.
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:
This concludes the CKA certification exam series.
Here are the nine other blogs in the series:
- Certified Kubernetes Administrator Exam Series (Part-1): Core Concepts
- Certified Kubernetes Administrator Exam Series (Part-2): Scheduling
- Certified Kubernetes Administrator Exam Series (Part-3): Logging & Monitoring
- Certified Kubernetes Administrator Exam Series (Part-4): Application Lifecycle Management
- Certified Kubernetes Administrator Exam Series (Part-5): Cluster Maintenance
- Certified Kubernetes Administrator Exam Series (Part-6): Security
- Certified Kubernetes Administrator Exam Series (Part-7): Storage
- Certified Kubernetes Administrator Exam Series (Part-8): Networking
- Certified Kubernetes Administrator Exam Series (Part-9): Troubleshooting