kubectl-sql
kubectl-sql is a kubectl plugin that use SQL like language to query the Kubernetes cluster manager
More docs
Install
Using go get
command:
GO111MODULE=on go get -v github.com/yaacov/kubectl-sql/cmd/kubectl-sql
Using Fedora Copr:
dnf copr enable yaacov/kubesql
dnf install kubectl-sql
From source:
git clone git@github.com:yaacov/kubectl-sql.git
cd kubectl-sql
make
Plugin
kubectl-sql can be used as a stand alone cli tool kubectl-sql
, or as a kubectl
plugin sql
( requires kubectl
v1.12 or higher ).
Installing kubectl-sql
as a plugin requires both kubectl
and kubectl-sql
to be installed in the current PATH
.
# For example, if user PATH include $HOME/.local/bin
# this line will install kubectl-sql in the users PATH
cp kubectl-sql ~/.local/bin/
# Using kubectl-sql as a kubectl plugin.
kubectl sql get pods where "name ilike 'test-%'"
--output flag |
Print format |
table |
Table |
name |
Names only |
yaml |
YAML |
json |
JSON |
What does it do ?
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. It is also posible to find connected resources useing the
join
command.
For other ways to select Kubernetes resources see #Alternatives.
Examples
More kubectl-sql examples
# Get all pods from current namespace scope, that has a name starting with "virt-" and
# IP that ends with ".84"
kubectl-sql get pods where "name ~= '^virt-' and status.podIP ~= '[.]84$'"
AMESPACE NAME PHASE hostIP CREATION_TIME(RFC3339)
default virt-launcher-test-bdw2p-lcrwx Running 192.168.126.56 2020-02-12T14:14:01+02:00
...
# Get all persistant volume clames that are less then 20Gi, and output as json.
kubectl-sql -o json get pvc where "spec.resources.requests.storage < 20Gi"
...
# Display non running pods by nodes for all namespaces.
kubectl-sql join nodes,pods on \
"nodes.status.addresses.1.address = pods.status.hostIP and not pods.phase ~= 'Running'" -A
...
# Filter replica sets with less ready-replicas then replicas"
kubectl-sql --all-namespaces get rs where "status.readyReplicas < status.replicas"
Alternatives
jq
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)
kubectl --field-selector
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/