๐Ÿ“ฆ brodul / presentation_duckdb_k8s

๐Ÿ“„ slides.md ยท 269 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
## Use duckDb to analyze k8s clusters
### Andraz Brodnik - brodul

---

## What is Kubernetes? โ˜ธ

Scheduler with an CRUD API ...

- Create
- Read
- Update
- Delete

with an HTTP API call

---

## How do we answer complex questions? 

- How many STS pods are in an AZ (AWS) or a Zone (GCP)?
- Can we delete the node without disrupting customer in namespace `duckinc`

---

# Meet DuckDb ๐Ÿฆ†

---

- In process
- RDBMS -> Relational Database Management System
- OLAP -> Online analytical processing

---

## Simple 4 step solution ๐Ÿ““

---

## Step 1 - Get the data

`kubectl get pods -A` 

---

<img alt="image" src="https://github.com/user-attachments/assets/70711567-fc53-4146-9da2-34a9e77c4d3e" />

---

## How do we read k8s data? 

`YAML` or `json`

```shell
kubectl get pods -A -o json > pods.json
kubectl get nodes -o json > nodes.json
```
---
#### nodes.json
```json
{
  "apiVersion": "v1",
  "kind": "List",
  "items": [
    {
      "apiVersion": "v1",
      "kind": "Node",
      "metadata": {
        "name": "node-a1",
        "labels": {
          "topology.kubernetes.io/zone": "us-central1-a"
        }
      }
    },
```
---
#### pods.json
```json
{
  "apiVersion": "v1",
  "kind": "List",
  "items": [
    {
      "apiVersion": "v1",
      "kind": "Pod",
      "metadata": {
        "name": "web-server-1",
        "namespace": "production"
      },
      "spec": {
        "nodeName": "node-a1"
      },
```
---

## Step 2 - Flatten the data ๐Ÿ”จ

---
#### pods.json
```json
{
  "apiVersion": "v1",
  "kind": "List",
  "items": [
    {
      "apiVersion": "v1",
      "kind": "Pod",
      "metadata": {
        "name": "web-server-1",
        "namespace": "production"
      },
      "spec": {
        "nodeName": "node-a1"
      },
```
---

```shell
jq '
  .items
  | map({
      podName: .metadata.name,
      namespace: .metadata.namespace,
      nodeName: .spec.nodeName
    })
' < pods.json > flat_pods.json
```

---
#### flat_pods.json
```json
[
  {
    "podName": "web-server-1",
    "namespace": "production",
    "nodeName": "node-a1"
  },
  {
    "podName": "api-service-1",
    "namespace": "production",
    "nodeName": "node-b1"
  },
```
---

#### nodes.json
```json
{
  "apiVersion": "v1",
  "kind": "List",
  "items": [
    {
      "apiVersion": "v1",
      "kind": "Node",
      "metadata": {
        "name": "node-a1",
        "labels": {
          "topology.kubernetes.io/zone": "us-central1-a"
        }
      }
    },
```

---
```json
jq '
  .items
  | map({
      az: .metadata.labels."topology.kubernetes.io/zone",
      nodeName: .metadata.name
    })
' < nodes.json > flat_nodes.json
```

---
#### flat_nodes.json
```json
[
  {
    "az": "us-central1-a",
    "nodeName": "node-a1"
  },
  {
    "az": "us-central1-b",
    "nodeName": "node-b1"
  }
]
```

---


## Step 3 - Create a table

```shell
duckdb my.db
```
---
```sql
CREATE TABLE pods AS
SELECT * FROM read_json_auto('flat_pods.json');
CREATE TABLE nodes AS
SELECT * FROM read_json_auto('flat_nodes.json');
```
---
```
D SELECT * FROM pods;
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚    podName    โ”‚ namespace  โ”‚ nodeName โ”‚
โ”‚    varchar    โ”‚  varchar   โ”‚ varchar  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ web-server-1  โ”‚ production โ”‚ node-a1  โ”‚
โ”‚ api-service-1 โ”‚ production โ”‚ node-a1  โ”‚
โ”‚ db-1          โ”‚ database   โ”‚ node-b1  โ”‚
โ”‚ cache-1       โ”‚ cache      โ”‚ node-b1  โ”‚
โ”‚ job-runner-1  โ”‚ batch-jobs โ”‚ node-b1  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```
---
```
D SELECT * FROM nodes;
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚      az       โ”‚ nodeName โ”‚
โ”‚    varchar    โ”‚ varchar  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ us-central1-a โ”‚ node-a1  โ”‚
โ”‚ us-central1-b โ”‚ node-b1  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```
---
## Step 4 - Join and query tables for the answer ๐Ÿงช

... with ChatGpt 

---
```sql
SELECT 
    p.podName,
    p.namespace,
    p.nodeName,
    n.az
FROM 
    pods p
JOIN 
    nodes n ON p.nodeName = n.nodeName;
```
---
```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚    podName    โ”‚ namespace  โ”‚ nodeName โ”‚      az       โ”‚
โ”‚    varchar    โ”‚  varchar   โ”‚ varchar  โ”‚    varchar    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ web-server-1  โ”‚ production โ”‚ node-a1  โ”‚ us-central1-a โ”‚
โ”‚ api-service-1 โ”‚ production โ”‚ node-a1  โ”‚ us-central1-a โ”‚
โ”‚ db-1          โ”‚ database   โ”‚ node-b1  โ”‚ us-central1-b โ”‚
โ”‚ cache-1       โ”‚ cache      โ”‚ node-b1  โ”‚ us-central1-b โ”‚
โ”‚ job-runner-1  โ”‚ batch-jobs โ”‚ node-b1  โ”‚ us-central1-b โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```
---
- Join PVs, PVCs and Pods
- You can create tables from CSV, JSON ...  
- All the created tables will persist in your file `my.db`
---

<img  alt="image" src="https://github.com/user-attachments/assets/8937adcc-e072-47db-9104-907ce8ef1b02" />