kubectl-sql is a kubectl plugin that use SQL like language to query the Kubernetes cluster manager
Using krew plugin manager to install:
# Available for linux-amd64
kubectl krew install sql
kubectl sql --help
Using Fedora Copr:
# Available for F41 and F42 (linux-amd64)
dnf copr enable yaacov/kubesql
dnf install kubectl-sql
From source:
# Clone code
git clone [email protected]:yaacov/kubectl-sql.git
cd kubectl-sql
# Build kubectl-sql
make
# Install into local machine PATH
sudo install ./kubectl-sql /usr/local/bin/
kubectl-sql let you select Kubernetes resources based on the value of one or more resource fields, using human readable easy to use SQL like query language.
# Get pods in namespace "openshift-multus" that hase name containing "cni"
kubectl-sql "select name, status.phase as phase, status.podIP as ip \
from openshift-multus/pods \
where name ~= 'cni' and (ip ~= '5$' or phase = 'Running')"
KIND: Pod COUNT: 2
name phase ip
multus-additional-cni-plugins-7kcsd Running 10.130.10.85
multus-additional-cni-plugins-kc8sz Running 10.131.6.65
...
# Get all persistant volume clames that are less then 20Gi, and output as json.
kubectl-sql -o json "select * from pvc where spec.resources.requests.storage < 20Gi"
...
# Get only first 10 pods ordered by name
kubectl-sql "SELECT name, status.phase FROM */pods ORDER BY name LIMIT 10"
--output flag | Print format |
---|---|
table | Table |
name | Names only |
yaml | YAML |
json | JSON |
jq
is a lightweight and flexible command-line JSON processor. It is possible to
pipe the kubectl command output into the jq
command to create complicated searches ( Illustrated jq toturial )
https://stedolan.github.io/jq/manual/#select(boolean_expression)
Field selectors let you select Kubernetes resources based on the value of one or more resource fields. Here are some examples of field selector queries.
https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/