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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
<a name="readme-top"></a>
<!-- [![Contributors][contributors-shield]][contributors-url]
[![Forks][forks-shield]][forks-url]
[![Stargazers][stars-shield]][stars-url]
[![Issues][issues-shield]][issues-url]
[![MIT License][license-shield]][license-url]
[![LinkedIn][linkedin-shield]][linkedin-url] -->
<!-- PROJECT LOGO -->
<br />
<div align="center">
<a href="https://github.com/robusta-dev/krr">
<img src="images/logo.png" alt="Logo" width="320" height="320">
</a>
<h3 align="center">Robusta KRR</h3>
<p align="center">
Prometheus-based Kubernetes Resource Recommendations
<br />
<a href="#getting-started"><strong>Usage docs Β»</strong></a>
<br />
<br />
<a href="https://github.com/robusta-dev/krr/issues">Report Bug</a>
Β·
<a href="https://github.com/robusta-dev/krr/issues">Request Feature</a>
Β·
<a href="https://robustacommunity.slack.com/archives/C054QUA4NHE">Slack Channel</a>
</p>
</div>
<!-- TABLE OF CONTENTS -->
<!-- <details>
<summary>Table of Contents</summary>
<ol>
<li>
<a href="#about-the-project">About The Project</a>
<ul>
<li><a href="#built-with">Built With</a></li>
</ul>
</li>
<li>
<a href="#getting-started">Getting Started</a>
<ul>
<li><a href="#prerequisites">Prerequisites</a></li>
<li><a href="#installation">Installation</a></li>
</ul>
</li>
<li><a href="#usage">Usage</a></li>
<li><a href="#roadmap">Roadmap</a></li>
<li><a href="#contributing">Contributing</a></li>
<li><a href="#license">License</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#acknowledgments">Acknowledgments</a></li>
</ol>
</details> -->
<!-- ABOUT THE PROJECT -->
## About The Project
![Product Name Screen Shot][product-screenshot]
Robusta KRR (Kubernetes Resource Recommender) is a CLI tool for optimizing resource allocation in Kubernetes clusters. It gathers pod usage data from Prometheus and recommends requests and limits for CPU and memory. This reduces costs and improves performance.
### Features
- No Agent Required: Robusta KRR is a CLI tool that runs on your local machine. It does not require running Pods in your cluster.
- Prometheus Integration: Gather resource usage data using built-in Prometheus queries, with support for custom queries coming soon.
- Extensible Strategies: Easily create and use your own strategies for calculating resource recommendations.
- Future Support: Upcoming versions will support custom resources (e.g. GPUs) and custom metrics.
### Resource Allocation Statistics
According to a recent [Sysdig study](https://sysdig.com/blog/millions-wasted-kubernetes/), on average, Kubernetes clusters have:
- 69% unused CPU
- 18% unused memory
By right-sizing your containers with KRR, you can save an average of 69% on cloud costs.
### How it works
#### Metrics Gathering
Robusta KRR uses the following Prometheus queries to gather usage data:
- CPU Usage:
```
sum(irate(container_cpu_usage_seconds_total{{namespace="{object.namespace}", pod="{pod}", container="{object.container}"}}[{step}]))
```
- Memory Usage:
```
sum(container_memory_working_set_bytes{job="kubelet", metrics_path="/metrics/cadvisor", image!="", namespace="{object.namespace}", pod="{pod}", container="{object.container}"})
```
[_Need to customize the metrics? Tell us and we'll add support._](https://github.com/robusta-dev/krr/issues/new)
#### Algorithm
By default, we use a _simple_ strategy to calculate resource recommendations. It is calculated as follows (_The exact numbers can be customized in CLI arguments_):
- For CPU, we set a request at the 99th percentile with no limit. Meaning, in 99% of the cases, your CPU request will be sufficient. For the remaining 1%, we set no limit. This means your pod can burst and use any CPU available on the node - e.g. CPU that other pods requested but arenβt using right now.
- For memory, we take the maximum value over the past week and add a 5% buffer.
#### Prometheus connection
Find about how KRR tries to find the default prometheus to connect <a href="#prometheus-auto-discovery">here</a>.
### Difference with Kubernetes VPA
| Feature π οΈ | Robusta KRR π | Kubernetes VPA π |
| --------------------------- | ---------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- |
| Resource Recommendations π‘ | β
CPU/Memory requests and limits | β
CPU/Memory requests and limits |
| Installation Location π | β
Not required to be installed inside the cluster, can be used on your own device, connected to a cluster | β Must be installed inside the cluster |
| Workload Configuration π§ | β
No need to configure a VPA object for each workload | β Requires VPA object configuration for each workload |
| Immediate Results β‘ | β
Gets results immediately (given Prometheus is running) | β Requires time to gather data and provide recommendations |
| Reporting π | β
Detailed CLI Report, web UI in [Robusta.dev](https://home.robusta.dev/) | β Not supported |
| Extensibility π§ | β
Add your own strategies with few lines of Python | :warning: Limited extensibility |
| Custom Metrics π | π Support in future versions | β Not supported |
| Custom Resources ποΈ | π Support in future versions (e.g., GPU) | β Not supported |
| Explainability π | π Support in future versions (Robusta will send you additional graphs) | β Not supported |
| Autoscaling π | π Support in future versions | β
Automatic application of recommendations |
### Robusta UI integration
If you are using [Robusta SaaS](https://platform.robusta.dev/), then KRR is integrated starting from [v0.10.15](https://github.com/robusta-dev/robusta/releases/tag/0.10.15). You can view all your recommendations (previous ones also), filter and sort them by either cluster, namespace or name.
More features (like seeing graphs, based on which recommendations were made) coming soon. [Tell us what you need the most!](https://github.com/robusta-dev/krr/issues/new)
![Robusta UI Screen Shot][ui-screenshot]
<p align="right">(<a href="#readme-top">back to top</a>)</p>
<!-- GETTING STARTED -->
## Getting Started
### Installation
<!-- _Depending on your operating system, select the appropriate installation method._
#### Linux
```sh
sudo apt install robusta-krr
````
#### MacOS
```sh
brew install robusta-krr
```
#### Windows
```sh
choco install robusta-krr
```
#### Debian
```sh
sudo apt install robusta-krr
```
#### Docker
`````sh
docker pull robusta/krr
````
#### Manual
-->
1. Make sure you have [Python 3.9](https://www.python.org/downloads/) (or greater) installed
2. Clone the repo:
```sh
git clone https://github.com/robusta-dev/krr
```
3. Navigate to the project root directory (`cd ./krr`)
4. Install requirements:
```sh
pip install -r requirements.txt
```
5. Run the tool:
```sh
python krr.py --help
```
<p align="right">(<a href="#readme-top">back to top</a>)</p>
<!-- USAGE EXAMPLES -->
## Usage
Straightforward usage, to run the simple strategy:
```sh
python krr.py simple
```
If you want only specific namespaces (default and ingress-nginx):
```sh
python krr.py simple -n default -n ingress-nginx
```
By default krr will run in the current context. If you want to run it in a different context:
```sh
python krr.py simple -c my-cluster-1 -c my-cluster-2
```
If you want to get the output in JSON format (--logtostderr is required so no logs go to the result file):
```sh
python krr.py simple --logtostderr -f json > result.json
```
If you want to get the output in YAML format:
```sh
python krr.py simple --logtostderr -f yaml > result.yaml
```
If you want to see additional debug logs:
```sh
python krr.py simple -v
```
More specific information on Strategy Settings can be found using
```sh
python krr.py simple --help
```
<p align="right">(<a href="#readme-top">back to top</a>)</p>
<!-- Port-forwarding -->
## Prometheus auto-discovery
By default, KRR will try to auto-discover the running Prometheus by scanning those labels:
```python
"app=kube-prometheus-stack-prometheus"
"app=prometheus,component=server"
"app=prometheus-server"
"app=prometheus-operator-prometheus"
"app=prometheus-msteams"
"app=rancher-monitoring-prometheus"
"app=prometheus-prometheus"
```
If none of those labels result in finding Prometheus, you will get an error and will have to pass the working url explicitly (using the `-p` flag).
<p align="right">(<a href="#readme-top">back to top</a>)</p>
## Example of using port-forward for Prometheus
If your prometheus is not auto-connecting, you can use `kubectl port-forward` for manually forwarding Prometheus.
For example, if you have a Prometheus Pod called `kube-prometheus-st-prometheus-0`, then run this command to port-forward it:
```sh
kubectl port-forward pod/kube-prometheus-st-prometheus-0 9090
```
Then, open another terminal and run krr in it, giving an explicit prometheus url:
```sh
python krr.py simple -p http://127.0.0.1:9090
```
<p align="right">(<a href="#readme-top">back to top</a>)</p>
<!-- CUSTOM -->
## Creating a Custom Strategy/Formatter
Look into the `examples` directory for examples on how to create a custom strategy/formatter.
<p align="right">(<a href="#readme-top">back to top</a>)</p>
<!-- BUILDING -->
## Building
_We are planning to use pyinstaller to build binaries for distribution. Right now you can build the binaries yourself, but we're not distributing them yet._
1. Install the project manually (see above)
2. Navigate to the project root directory
3. Install poetry (https://python-poetry.org/docs/#installing-with-the-official-installer)
4. Install requirements with dev dependencies:
```sh
poetry install --group dev
```
5. Build the binary:
```sh
poetry run pyinstaller krr.py
```
6. The binary will be located in the `dist` directory. Test that it works:
```sh
cd ./dist/krr
./krr --help
```
<p align="right">(<a href="#readme-top">back to top</a>)</p>
<!-- TESTING -->
## Testing
_We use pytest to run tests._
1. Install the project manually (see above)
2. Navigate to the project root directory
3. Install poetry (https://python-poetry.org/docs/#installing-with-the-official-installer)
4. Install dev dependencies:
```sh
poetry install --group dev
```
5. Install robusta_krr as editable dependency:
```sh
pip install -e .
```
6. Run the tests:
```sh
poetry run pytest
```
<p align="right">(<a href="#readme-top">back to top</a>)</p>
<!-- CONTRIBUTING -->
## Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
Don't forget to give the project a star! Thanks again!
1. Fork the Project
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the Branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request
<p align="right">(<a href="#readme-top">back to top</a>)</p>
<!-- LICENSE -->
## License
Distributed under the MIT License. See `LICENSE.txt` for more information.
<p align="right">(<a href="#readme-top">back to top</a>)</p>
<!-- CONTACT -->
## Contact
If you have any questions, feel free to contact support@robusta.dev
Project Link: [https://github.com/robusta-dev/krr](https://github.com/robusta-dev/krr)
<p align="right">(<a href="#readme-top">back to top</a>)</p>
<!-- MARKDOWN LINKS & IMAGES -->
<!-- https://www.markdownguide.org/basic-syntax/#reference-style-links -->
[contributors-shield]: https://img.shields.io/github/contributors/othneildrew/Best-README-Template.svg?style=for-the-badge
[contributors-url]: https://github.com/othneildrew/Best-README-Template/graphs/contributors
[forks-shield]: https://img.shields.io/github/forks/othneildrew/Best-README-Template.svg?style=for-the-badge
[forks-url]: https://github.com/othneildrew/Best-README-Template/network/members
[stars-shield]: https://img.shields.io/github/stars/othneildrew/Best-README-Template.svg?style=for-the-badge
[stars-url]: https://github.com/othneildrew/Best-README-Template/stargazers
[issues-shield]: https://img.shields.io/github/issues/othneildrew/Best-README-Template.svg?style=for-the-badge
[issues-url]: https://github.com/othneildrew/Best-README-Template/issues
[license-shield]: https://img.shields.io/github/license/othneildrew/Best-README-Template.svg?style=for-the-badge
[license-url]: https://github.com/othneildrew/Best-README-Template/blob/master/LICENSE.txt
[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=for-the-badge&logo=linkedin&colorB=555
[linkedin-url]: https://linkedin.com/in/othneildrew
[product-screenshot]: images/screenshot.jpeg
[ui-screenshot]: images/ui_screenshot.jpeg
|