-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
512 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# kubesql | ||
|
||
kubesql is a tool to use sql to query the resources of kubernetes. | ||
|
||
The resources of kubernetes such as nodes, pods and so on are handled as the | ||
|
||
For example, all pods are easily to list from apiserver. But the number of pods on each node is not easy to caculate. | ||
|
||
With kubesql, a sql statement can achieve it like this. | ||
|
||
``` | ||
[root@localhost kubesql]# kubesql "select hostIp, count(*) from pods group by hostIp" | ||
+----------+----------------+ | ||
| count(*) | hostIP | | ||
+----------+----------------+ | ||
| 9 | None | | ||
| 4 | 22.2.22.222 | | ||
| 14 | 11.1.111.11 | | ||
+----------+----------------+ | ||
``` | ||
|
||
How many pod are pending | ||
|
||
``` | ||
[root@localhost kubesql]# kubesql "select count(*) from pods where phase = 'Pending'" | ||
+----------+ | ||
| count(*) | | ||
+----------+ | ||
| 29 | | ||
+----------+ | ||
``` | ||
|
||
|
||
# compoments | ||
|
||
kubesql has three compoments. | ||
|
||
- kubesql-watch: Watch the events from kube-apiserver, and write it to sqlite3. | ||
- kubesql-server: Provide a http api for query. Accepts the sql query , execute the query in sqlite3 and return the query result. | ||
- kubesql-client: Send the query sql to kubesql-server and get the result, then print the result in table format. | ||
|
||
``` | ||
+----------------+ watch +---------------+ +---------+ | ||
| kube-apiserver | -------> | kubesql-watch | --> | sqlite3 | | ||
+----------------+ +---------------+ +---------+ | ||
^ | ||
| | ||
| | ||
+----------------+ http +---------------+ | | ||
| kubesql-client | -------> | kubsql-server | ------+ | ||
+----------------+ +---------------+ | ||
``` | ||
|
||
# install and deploy | ||
|
||
## manualy install and deploy | ||
|
||
install | ||
|
||
``` | ||
//check out the code | ||
pip install requirements.txt | ||
python setup.py install | ||
cp -r etc/kubesql /etc | ||
``` | ||
|
||
check the config of `/etc/kubesql/config`, and modify the kubeconfig. kubeconfig is for kubesql-watch to connect to the apiserver. | ||
|
||
``` | ||
nohup kubesql-watch & | ||
nohup kubesql-server & | ||
``` | ||
|
||
|
||
# Usage | ||
|
||
kubesql command is short for kubesql-client. It is used to send the query and show the result in table. | ||
|
||
``` | ||
[root@localhost kubesql]# kubesql -h | ||
usage: kubesql [-h] [-t TABLE] [-a] [sql] | ||
positional arguments: | ||
sql execte the sql. | ||
optional arguments: | ||
-h, --help show this help message and exit | ||
-t TABLE, --table TABLE | ||
increase output verbosity | ||
-a, --all show all tables | ||
``` | ||
|
||
`kubesql -a` can list the tables currently supported. | ||
|
||
``` | ||
[root@localhost kubesql]# kubesql -a | ||
+------------+ | ||
| table_name | | ||
+------------+ | ||
| pods | | ||
| nodes | | ||
+------------+ | ||
``` | ||
|
||
And `kubesql -t {table_name}` can list the columns for `table_name` currently supported. | ||
|
||
``` | ||
[root@localhost kubesql]# kubesql -t nodes | ||
+-------------------------+-----+------------+---------+----+-----------+ | ||
| name | cid | dflt_value | notnull | pk | type | | ||
+-------------------------+-----+------------+---------+----+-----------+ | ||
| name | 0 | None | 0 | 0 | char(200) | | ||
| uid | 1 | None | 0 | 0 | char(200) | | ||
| creationTimestamp | 2 | None | 0 | 0 | datetime | | ||
| deletionTimestamp | 3 | None | 0 | 0 | datetime | | ||
| zone | 4 | None | 0 | 0 | char(200) | | ||
| allocatable_cpu | 5 | None | 0 | 0 | char(200) | | ||
| allocatable_memory | 6 | None | 0 | 0 | char(200) | | ||
| allocatable_pods | 7 | None | 0 | 0 | char(200) | | ||
| capacity_cpu | 8 | None | 0 | 0 | char(200) | | ||
| capacity_memory | 9 | None | 0 | 0 | char(200) | | ||
| capacity_pods | 10 | None | 0 | 0 | char(200) | | ||
| architecture | 11 | None | 0 | 0 | char(200) | | ||
| containerRuntimeVersion | 12 | None | 0 | 0 | char(200) | | ||
| kubeProxyVersion | 13 | None | 0 | 0 | char(200) | | ||
| kubeletVersion | 14 | None | 0 | 0 | char(200) | | ||
| operatingSystem | 15 | None | 0 | 0 | char(200) | | ||
| osImage | 16 | None | 0 | 0 | char(200) | | ||
+-------------------------+-----+------------+---------+----+-----------+ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[kubesql] | ||
port=415 | ||
ip=127.0.0.1 | ||
db_path=/dev/shm/kubesql.db | ||
param_path=/etc/kubesql/params | ||
kubeconfig_path=/etc/kubeconfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
pod | ||
metadata.name | ||
metadata.uid | ||
metadata.namespace | ||
metadata.creationTimestamp | ||
metadata.deletionTimestamp | ||
spec.nodeName | ||
spec.schedulerName | ||
status.hostIP | ||
status.phase | ||
status.podIP | ||
status.reason | ||
status.startTime | ||
--- | ||
node | ||
metadata.name | ||
metadata.uid | ||
metadata.creationTimestamp | ||
metadata.deletionTimestamp | ||
status.allocatable.cpu allocatable_cpu | ||
status.allocatable.memory allocatable_memory | ||
status.allocatable.pods allocatable_pods | ||
status.capacity.cpu capacity_cpu | ||
status.capacity.memory capacity_memory | ||
status.capacity.pods capacity_pods | ||
status.nodeInfo.architecture | ||
status.nodeInfo.containerRuntimeVersion | ||
status.nodeInfo.kubeProxyVersion | ||
status.nodeInfo.kubeletVersion | ||
status.nodeInfo.operatingSystem | ||
status.nodeInfo.osImage |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import prettytable as pt | ||
import argparse | ||
import httplib | ||
import json | ||
from kubesql import utils | ||
|
||
cfg = utils.load_config() | ||
|
||
def get_kube_sql(sql): | ||
conn = httplib.HTTPConnection("%s:%s" % (cfg.get("ip"), cfg.get("port"))) | ||
params = {'sql': sql} | ||
headers = {'Content-type': 'application/json'} | ||
conn.request("POST", "/sql", headers=headers, body=json.dumps(params)) | ||
try: | ||
response = conn.getresponse() | ||
# print params, response.status, response.reason | ||
data = response.read() | ||
return json.loads(data) | ||
except Exception, e: | ||
print e, params | ||
conn.close() | ||
|
||
|
||
def print_json_as_table(result): | ||
if result: | ||
tb = pt.PrettyTable() | ||
row = result[0] | ||
tb.field_names = row.keys() | ||
for row in result: | ||
row_value = [] | ||
for field_name in tb.field_names: | ||
row_value.append(row.get(field_name)) | ||
tb.add_row(row_value) | ||
tb.align = "l" | ||
print(tb) | ||
|
||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("sql", nargs="?", type=str, help="execte the sql.") | ||
parser.add_argument("-t", "--table", help="increase output verbosity") | ||
parser.add_argument("-a", "--all", action='store_true', help="show all tables") | ||
args = parser.parse_args() | ||
|
||
|
||
def main(): | ||
if args.sql: | ||
result = get_kube_sql(args.sql) | ||
elif args.table: | ||
result = get_kube_sql('PRAGMA table_info(%s)' % args.table) | ||
elif args.all: | ||
result = get_kube_sql('SELECT name as table_name FROM sqlite_master WHERE type="table"') | ||
print_json_as_table(result) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.