How to Build Custom Views for Kubernetes Cluster Objects
When managing a Kubernetes cluster, you may often need to retrieve specific data about objects such as nodes, pods, deployments, and builds. However, the default output from commands like kubectl get might not always provide the exact data you need. To solve this, you can create custom views for Kubernetes cluster objects, allowing you to retrieve only the information you want. In this guide, we’ll explore how to build custom queries using kubectl and jq to streamline your Kubernetes management tasks.

Why Custom Views Matter in Kubernetes
Custom views allow you to extract only the relevant information from your Kubernetes cluster, without the noise of unnecessary data. This is particularly useful when working with large clusters, where you need to focus on specific parameters like memory usage, CPU load, or specific environment variables within deployments. By defining custom queries, you can make your Kubernetes management more efficient and tailored to your needs.
Using kubectl Custom Columns for Kubernetes Queries
The kubectl command offers a powerful feature known as the custom-columns option, which lets you specify the exact fields to be displayed in the output. This is particularly helpful when querying nodes or deployments.
Querying Nodes with Custom Columns
For instance, to fetch node health information like memory, CPU, and disk pressures, you can run the following command:
kubectl get nodes -o custom-columns="Name:.metadata.name,InternalIP:.status.addresses[0].address,Kernel:.status.nodeInfo.kernelVersion,MemoryPressure:.status.conditions[0].status,DiskPressure:.status.conditions[1].status,PIDPressure:.status.conditions[2].status,Ready:.status.conditions[3].status"
This command extracts key health metrics such as memory pressure, disk pressure, and readiness for each node in your cluster.
Querying Deployments with Custom Columns
Similarly, when querying deployments, the -o custom-columns flag can help retrieve specific details. To see the environment variables of a deployment, you might use the following:
kubectl get deployments --all-namespaces -o custom-columns="Name:.metadata.name,Namespace:.metadata.namespace,Env:.spec.template.spec.containers[].env"
This will return the environment variables for each container within your deployments, allowing you to monitor critical configuration settings.
Streamlining Queries with jq
While kubectl is great for simple queries, the jq utility offers advanced filtering and querying capabilities for working with JSON data. jq allows you to filter, map, and transform JSON data quickly.
Example: Querying Deployment Environment Variables with jq
If you want to retrieve environment variables from multiple deployments, you can use jq to parse the JSON output and extract the desired information. For instance:
kubectl get deployments --all-namespaces -o json | jq -r '.items[] | { namespace: .metadata.namespace, name: .metadata.name, env: .spec.template.spec.containers[].env}'
This query extracts the deployment name, namespace, and environment variables, and formats them into a clean output. You can even save this query in a file for reuse.
Storing Queries for Reusability
You can also store your jq queries in files, which makes it easier to share them across teams or store them in version control. For example, you can save your query to a file named jq-deployments-envs.txt, which contains the following:
.items | [ map(.) | .[] | { namespace: .metadata.namespace, name: .metadata.name, env: .spec.template.spec.containers[].env }]
And execute it with:
kubectl get deployments --all-namespaces -o json | jq -f jq-deployments-envs.txt
Using Custom Views for Kubernetes Cluster Management
By combining kubectl’s custom columns with jq’s filtering capabilities, you can create a range of custom views for your Kubernetes cluster. These views can focus on node health, deployment environments, or even resource limits and constraints. Saving these queries in files makes it easier to share them with your team or use them in automation scripts.
ZippyOPS: Streamlining Kubernetes Operations
If you’re looking for advanced Kubernetes management, ZippyOPS provides consulting, implementation, and managed services that help you optimize your Kubernetes operations. With expertise in DevOps, DevSecOps, Cloud, and Automated Ops, ZippyOPS can help you create custom solutions that enhance the performance and security of your Kubernetes clusters. We specialize in offering tailored services such as DataOps, MLOps, and Infrastructure optimization, all aimed at improving operational efficiency.
To learn more about ZippyOPS services, visit our Services page or explore our Solutions and Products.
For detailed insights and updates, don’t forget to check out our YouTube playlist.
Conclusion
In conclusion, creating custom views for Kubernetes cluster objects can greatly enhance your ability to monitor and manage your resources effectively. By leveraging kubectl‘s custom-columns and the powerful filtering capabilities of jq, you can build precise queries that focus on the data you need, saving both time and effort. For organizations looking to scale and optimize Kubernetes operations, ZippyOPS provides a comprehensive suite of services designed to improve operational workflows, security, and overall efficiency.
For expert Kubernetes solutions or consulting services, reach out to us at sales@zippyops.com.



