diff --git a/.github/workflows/build-on-release.yml b/.github/workflows/build-on-release.yml index f3c3ae10..d8a91237 100644 --- a/.github/workflows/build-on-release.yml +++ b/.github/workflows/build-on-release.yml @@ -18,7 +18,7 @@ jobs: - name: Set up Python uses: actions/setup-python@v2 with: - python-version: '3.9' + python-version: '3.11' - name: Install dependencies run: | @@ -78,8 +78,17 @@ jobs: env: GITHUB_REF_NAME: ${{ github.ref_name }} + - name: Build with PyInstaller + if: matrix.os == 'macos-latest' + shell: bash + run: | + pyinstaller --target-architecture universal2 krr.py + mkdir -p ./dist/krr/grapheme/data + cp $(python -c "import grapheme; print(grapheme.__path__[0] + '/data/grapheme_break_property.json')") ./dist/krr/grapheme/data/grapheme_break_property.json + cp ./intro.txt ./dist/krr/intro.txt - name: Build with PyInstaller + if: matrix.os == 'ubuntu-latest' shell: bash run: | pyinstaller krr.py diff --git a/README.md b/README.md index eec9c60a..012b19bf 100644 --- a/README.md +++ b/README.md @@ -18,17 +18,20 @@
Installation . - Usage - · How KRR works . Slack Integration + . + KRR UI on Robusta Cloud
+ Usage + · Report Bug · Request Feature · Support +
Like KRR? Please ⭐ this repository to show your support!

@@ -60,7 +63,7 @@ ## About The Project -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. +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**. ### Data Integrations @@ -83,7 +86,7 @@ _View instructions for: [Seeing recommendations in a UI](#free-ui-for-krr-recomm - **Prometheus Integration**: Get recommendations based on the data you already have - **Explainability**: Understand how recommendations were calculated - **Extensible Strategies**: Easily create and use your own strategies for calculating resource recommendations. -- **Free SaaS Platform**: See why KRR recommends what it does, by using the [free Robusta SaaS platform](https://home.robusta.dev/). +- **Free SaaS Platform**: See why KRR recommends what it does, by using the [free Robusta SaaS platform](https://platform.robusta.dev/signup/?utm_source=github&utm_medium=krr-readme). - **Future Support**: Upcoming versions will support custom resources (e.g. GPUs) and custom metrics. ### Why Use KRR? @@ -95,11 +98,29 @@ According to a recent [Sysdig study](https://sysdig.com/blog/millions-wasted-kub By right-sizing your containers with KRR, you can save an average of 69% on cloud costs. -Read more about [how KRR works](#how-krr-works) and [KRR vs Kubernetes VPA](#difference-with-kubernetes-vpa) +Read more about [how KRR works](#how-krr-works) + +## 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 | +| Explainability 📖 | ✅ See graphs explaining the recommendations | ❌ Not supported | +| Custom Metrics 📏 | 🔄 Support in future versions | ❌ Not supported | +| Custom Resources 🎛️ | 🔄 Support in future versions (e.g., GPU) | ❌ Not supported | +| Autoscaling 🔀 | 🔄 Support in future versions | ✅ Automatic application of recommendations | +| Default History 🕒 | 14 days | 8 days | +| Supports HPA 🔥 | ✅ Enable using `--allow-hpa` flag | ❌ Not supported | + -## Installation +## Installation ### Requirements @@ -120,6 +141,7 @@ If you have a different setup, make sure the following metrics exist: _Note: If one of last three metrics is absent KRR will still work, but it will only consider currently-running pods when calculating recommendations. Historic pods that no longer exist in the cluster will not be taken into consideration._ + ### Installation Methods
@@ -156,6 +178,18 @@ krr simple You can install using brew (see above) on [WSL2](https://docs.brew.sh/Homebrew-on-Linux), or install from source (see below).
+
+ Airgapped Installation (Offline Environments) + +You can download pre-built binaries from Releases or use the prebuilt Docker container. For example, the container for version 1.8.3 is: + +``` +us-central1-docker.pkg.dev/genuine-flight-317411/devel/krr:v1.8.3 +``` + +We do **not** recommend installing KRR from source in airgapped environments due to the headache of installing Python dependencies. Use one of the above methods instead and contact us (via Slack, GitHub issues, or email) if you need assistance. +
+
From Source @@ -200,8 +234,23 @@ Setup KRR for...

(back to top)

+ +## Free KRR UI on Robusta SaaS + +We highly recommend using the [free Robusta SaaS platform](https://platform.robusta.dev/signup/?utm_source=github&utm_medium=krr-readme). You can: + +- Understand individual app recommendations with app usage history +- Sort and filter recommendations by namespace, priority, and more +- Give devs a YAML snippet to fix the problems KRR finds +- Analyze impact using KRR scan history + + + + + + ## Usage
@@ -315,6 +364,16 @@ krr simple --logtostderr -f yaml > result.yaml
+
+ Prometheus Authentication + +KRR supports all known authentication schemes for Prometheus, VictoriaMetrics, Coralogix, and other Prometheus compatible metric stores. + +Refer to `krr simple --help`, and look at the flags `--prometheus-url`, `--prometheus-auth-header`, `--prometheus-headers` `--prometheus-ssl-enabled`, `--coralogix-token`, and the various `--eks-*` flags. + +If you need help, contact us on Slack, email, or by opening a GitHub issue. +
+
Debug mode If you want to see additional debug logs: @@ -347,7 +406,7 @@ Robusta KRR uses the following Prometheus queries to gather usage data: [_Need to customize the metrics? Tell us and we'll add support._](https://github.com/robusta-dev/krr/issues/new) -Get a free breakdown of KRR recommendations in the [Robusta SaaS](#optional-free-saas-platform). +Get a free breakdown of KRR recommendations in the [Robusta SaaS](#free-krr-ui-on-robusta-saas). ### Algorithm @@ -363,21 +422,6 @@ Find about how KRR tries to find the default Prometheus to connect (back to top)

-## 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 | -| Explainability 📖 | ✅ See graphs explaining the recommendations | ❌ Not supported | -| Custom Metrics 📏 | 🔄 Support in future versions | ❌ Not supported | -| Custom Resources 🎛️ | 🔄 Support in future versions (e.g., GPU) | ❌ Not supported | -| Autoscaling 🔀 | 🔄 Support in future versions | ✅ Automatic application of recommendations | -| Default History 🕒 | 14 days | 8 days | @@ -545,13 +589,16 @@ For discovering Prometheus it scans services for those labels:
Free UI for KRR recommendations -With the [free Robusta SaaS platform](https://home.robusta.dev/) you can: +We highly recommend using the [free Robusta SaaS platform](https://platform.robusta.dev/signup/?utm_source=github&utm_medium=krr-readme). You can: -- See why KRR recommends what it does +- Understand individual app recommendations with app usage history - Sort and filter recommendations by namespace, priority, and more -- Copy a YAML snippet to fix the problems KRR finds +- Give dev's a YAML snippet to fix the problems KRR finds +- Analyze impact using KRR scan history -![Robusta UI Screen Shot][ui-screenshot] + + +
diff --git a/robusta_krr/core/abstract/strategies.py b/robusta_krr/core/abstract/strategies.py index 0005a227..b63b2cc7 100644 --- a/robusta_krr/core/abstract/strategies.py +++ b/robusta_krr/core/abstract/strategies.py @@ -111,24 +111,15 @@ def __init__(self, settings: _StrategySettings): self.settings = settings def __str__(self) -> str: - return self._display_name.title() - - @property - def _display_name(self) -> str: - return getattr(self, "display_name", self.__class__.__name__.lower().removeprefix("strategy")) + return self.display_name.title() @property def description(self) -> Optional[str]: """ Generate a description for the strategy. - You can use the settings in the description by using the format syntax. - Also you can use Rich's markdown syntax to format the description. + You can use Rich's markdown syntax to format the description. """ - - if self.__doc__ is None: - return None - - return f"[b]{self} Strategy[/b]\n\n" + dedent(self.__doc__.format_map(self.settings.dict())).strip() + raise NotImplementedError() # Abstract method that needs to be implemented by subclass. # This method is intended to calculate resource recommendation based on history data and kubernetes object data. diff --git a/robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py b/robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py index 9c9ac924..440611eb 100644 --- a/robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py +++ b/robusta_krr/core/integrations/prometheus/metrics_service/prometheus_metrics_service.py @@ -41,6 +41,7 @@ def find_metrics_url(self, *, api_client: Optional[ApiClient] = None) -> Optiona "app=rancher-monitoring-prometheus", "app=prometheus-prometheus", "app.kubernetes.io/name=prometheus,app.kubernetes.io/component=server", + "app=stack-prometheus", ] ) diff --git a/robusta_krr/core/runner.py b/robusta_krr/core/runner.py index 8e08521c..7ae4a40d 100644 --- a/robusta_krr/core/runner.py +++ b/robusta_krr/core/runner.py @@ -293,7 +293,7 @@ async def _collect_result(self) -> Result: return Result( scans=scans, - description=self._strategy.description, + description=f"[b]{self._strategy.display_name.title()} Strategy[/b]\n\n{self._strategy.description}", strategy=StrategyData( name=str(self._strategy).lower(), settings=self._strategy.settings.dict(), diff --git a/robusta_krr/main.py b/robusta_krr/main.py index 5c2d01aa..74563217 100644 --- a/robusta_krr/main.py +++ b/robusta_krr/main.py @@ -186,9 +186,8 @@ def run_strategy( openshift: bool = typer.Option( False, "--openshift", - help="Used when running by Robusta inside an OpenShift cluster.", + help="Connect to Prometheus with a token read from /var/run/secrets/kubernetes.io/serviceaccount/token - recommended when running KRR inside an OpenShift cluster", rich_help_panel="Prometheus Openshift Settings", - hidden=True, ), cpu_min_value: int = typer.Option( 10, diff --git a/robusta_krr/strategies/simple.py b/robusta_krr/strategies/simple.py index 3cecd18e..c56e6ecb 100644 --- a/robusta_krr/strategies/simple.py +++ b/robusta_krr/strategies/simple.py @@ -1,3 +1,4 @@ +import textwrap from datetime import timedelta import numpy as np @@ -24,7 +25,7 @@ class SimpleStrategySettings(StrategySettings): - cpu_percentile: float = pd.Field(99, gt=0, le=100, description="The percentile to use for the CPU recommendation.") + cpu_percentile: float = pd.Field(95, gt=0, le=100, description="The percentile to use for the CPU recommendation.") memory_buffer_percentage: float = pd.Field( 15, gt=0, description="The percentage of added buffer to the peak memory usage for memory recommendation." ) @@ -70,21 +71,7 @@ def history_range_enough(self, history_range: tuple[timedelta, timedelta]) -> bo class SimpleStrategy(BaseStrategy[SimpleStrategySettings]): - """ - CPU request: {cpu_percentile}% percentile, limit: unset - Memory request: max + {memory_buffer_percentage}%, limit: max + {memory_buffer_percentage}% - History: {history_duration} hours - Step: {timeframe_duration} minutes - - All parameters can be customized. For example: `krr simple --cpu_percentile=90 --memory_buffer_percentage=15 --history_duration=24 --timeframe_duration=0.5` - - This strategy does not work with objects with HPA defined (Horizontal Pod Autoscaler). - If HPA is defined for CPU or Memory, the strategy will return "?" for that resource. - You can override this behaviour by passing the --allow-hpa flag - - Learn more: [underline]https://github.com/robusta-dev/krr#algorithm[/underline] - """ - + display_name = "simple" rich_console = True @@ -102,6 +89,27 @@ def metrics(self) -> list[type[PrometheusMetric]]: return metrics + @property + def description(self): + s = textwrap.dedent(f"""\ + CPU request: {self.settings.cpu_percentile}% percentile, limit: unset + Memory request: max + {self.settings.memory_buffer_percentage}%, limit: max + {self.settings.memory_buffer_percentage}% + History: {self.settings.history_duration} hours + Step: {self.settings.timeframe_duration} minutes + + All parameters can be customized. For example: `krr simple --cpu_percentile=90 --memory_buffer_percentage=15 --history_duration=24 --timeframe_duration=0.5` + """) + + if not self.settings.allow_hpa: + s += "\n" + textwrap.dedent(f"""\ + This strategy does not work with objects with HPA defined (Horizontal Pod Autoscaler). + If HPA is defined for CPU or Memory, the strategy will return "?" for that resource. + You can override this behaviour by passing the --allow-hpa flag + """) + + s += "\nLearn more: [underline]https://github.com/robusta-dev/krr#algorithm[/underline]" + return s + def __calculate_cpu_proposal( self, history_data: MetricsPodData, object_data: K8sObjectData ) -> ResourceRecommendation: