Ad Hoc Kubernetes Queries

1/15/2019

Is there a way to easily query Kubernetes resources in an intuitive way? Basically I want to run queries to extract info about objects which match my criteria. Currently I face an issue where my match labels isn't quite working and I would like to run the match labels query manually to try and debug my issue.

Basically in a pseudo code way:

Select * from pv where labels in [red,blue,green]

Any third party tools who do something like this? Currently all I have to work with is the search box on the dashboard which isn't quite robust enough.

-- mr haven
kubernetes

1 Answer

1/15/2019

You could use kubectl with JSONPath (https://kubernetes.io/docs/reference/kubectl/jsonpath/). More information on JSONPath: https://github.com/json-path/JsonPath

It allows you to query any resource property, example:

kubectl get pods -o=jsonpath='{$.items[?(@.metadata.namespace=="default")].metadata.name}'

This would list all pod names in namespace "default". Your pseudo code would be something along the lines:

kubectl get pv -o=jsonpath='{$.items[?(@.metadata.label in ["red","blue","green"])]}'
-- Markus Dresch
Source: StackOverflow