|
| 1 | +# kubesql |
| 2 | + |
| 3 | +kubesql is a tool to use sql to query the resources of kubernetes. |
| 4 | + |
| 5 | +The resources of kubernetes such as nodes, pods and so on are handled as the |
| 6 | + |
| 7 | +For example, all pods are easily to list from apiserver. But the number of pods on each node is not easy to caculate. |
| 8 | + |
| 9 | +With kubesql, a sql statement can achieve it like this. |
| 10 | + |
| 11 | +``` |
| 12 | +[root@localhost kubesql]# kubesql "select hostIp, count(*) from pods group by hostIp" |
| 13 | ++----------+----------------+ |
| 14 | +| count(*) | hostIP | |
| 15 | ++----------+----------------+ |
| 16 | +| 9 | None | |
| 17 | +| 4 | 22.2.22.222 | |
| 18 | +| 14 | 11.1.111.11 | |
| 19 | ++----------+----------------+ |
| 20 | +``` |
| 21 | + |
| 22 | +How many pod are pending |
| 23 | + |
| 24 | +``` |
| 25 | +[root@localhost kubesql]# kubesql "select count(*) from pods where phase = 'Pending'" |
| 26 | ++----------+ |
| 27 | +| count(*) | |
| 28 | ++----------+ |
| 29 | +| 29 | |
| 30 | ++----------+ |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | +# compoments |
| 35 | + |
| 36 | +kubesql has three compoments. |
| 37 | + |
| 38 | +- kubesql-watch: Watch the events from kube-apiserver, and write it to sqlite3. |
| 39 | +- kubesql-server: Provide a http api for query. Accepts the sql query , execute the query in sqlite3 and return the query result. |
| 40 | +- kubesql-client: Send the query sql to kubesql-server and get the result, then print the result in table format. |
| 41 | + |
| 42 | +``` |
| 43 | ++----------------+ watch +---------------+ +---------+ |
| 44 | +| kube-apiserver | -------> | kubesql-watch | --> | sqlite3 | |
| 45 | ++----------------+ +---------------+ +---------+ |
| 46 | + ^ |
| 47 | + | |
| 48 | + | |
| 49 | ++----------------+ http +---------------+ | |
| 50 | +| kubesql-client | -------> | kubsql-server | ------+ |
| 51 | ++----------------+ +---------------+ |
| 52 | +``` |
| 53 | + |
| 54 | +# install and deploy |
| 55 | + |
| 56 | +## manualy install and deploy |
| 57 | + |
| 58 | +install |
| 59 | + |
| 60 | +``` |
| 61 | +//check out the code |
| 62 | +pip install requirements.txt |
| 63 | +python setup.py install |
| 64 | +cp -r etc/kubesql /etc |
| 65 | +``` |
| 66 | + |
| 67 | +check the config of `/etc/kubesql/config`, and modify the kubeconfig. kubeconfig is for kubesql-watch to connect to the apiserver. |
| 68 | + |
| 69 | +``` |
| 70 | +nohup kubesql-watch & |
| 71 | +nohup kubesql-server & |
| 72 | +``` |
| 73 | + |
| 74 | + |
| 75 | +# Usage |
| 76 | + |
| 77 | +kubesql command is short for kubesql-client. It is used to send the query and show the result in table. |
| 78 | + |
| 79 | +``` |
| 80 | +[root@localhost kubesql]# kubesql -h |
| 81 | +usage: kubesql [-h] [-t TABLE] [-a] [sql] |
| 82 | +
|
| 83 | +positional arguments: |
| 84 | + sql execte the sql. |
| 85 | +
|
| 86 | +optional arguments: |
| 87 | + -h, --help show this help message and exit |
| 88 | + -t TABLE, --table TABLE |
| 89 | + increase output verbosity |
| 90 | + -a, --all show all tables |
| 91 | +``` |
| 92 | + |
| 93 | +`kubesql -a` can list the tables currently supported. |
| 94 | + |
| 95 | +``` |
| 96 | +[root@localhost kubesql]# kubesql -a |
| 97 | ++------------+ |
| 98 | +| table_name | |
| 99 | ++------------+ |
| 100 | +| pods | |
| 101 | +| nodes | |
| 102 | ++------------+ |
| 103 | +``` |
| 104 | + |
| 105 | +And `kubesql -t {table_name}` can list the columns for `table_name` currently supported. |
| 106 | + |
| 107 | +``` |
| 108 | +[root@localhost kubesql]# kubesql -t nodes |
| 109 | ++-------------------------+-----+------------+---------+----+-----------+ |
| 110 | +| name | cid | dflt_value | notnull | pk | type | |
| 111 | ++-------------------------+-----+------------+---------+----+-----------+ |
| 112 | +| name | 0 | None | 0 | 0 | char(200) | |
| 113 | +| uid | 1 | None | 0 | 0 | char(200) | |
| 114 | +| creationTimestamp | 2 | None | 0 | 0 | datetime | |
| 115 | +| deletionTimestamp | 3 | None | 0 | 0 | datetime | |
| 116 | +| zone | 4 | None | 0 | 0 | char(200) | |
| 117 | +| allocatable_cpu | 5 | None | 0 | 0 | char(200) | |
| 118 | +| allocatable_memory | 6 | None | 0 | 0 | char(200) | |
| 119 | +| allocatable_pods | 7 | None | 0 | 0 | char(200) | |
| 120 | +| capacity_cpu | 8 | None | 0 | 0 | char(200) | |
| 121 | +| capacity_memory | 9 | None | 0 | 0 | char(200) | |
| 122 | +| capacity_pods | 10 | None | 0 | 0 | char(200) | |
| 123 | +| architecture | 11 | None | 0 | 0 | char(200) | |
| 124 | +| containerRuntimeVersion | 12 | None | 0 | 0 | char(200) | |
| 125 | +| kubeProxyVersion | 13 | None | 0 | 0 | char(200) | |
| 126 | +| kubeletVersion | 14 | None | 0 | 0 | char(200) | |
| 127 | +| operatingSystem | 15 | None | 0 | 0 | char(200) | |
| 128 | +| osImage | 16 | None | 0 | 0 | char(200) | |
| 129 | ++-------------------------+-----+------------+---------+----+-----------+ |
| 130 | +``` |
0 commit comments