Overview
Workload Autoscaler automatically scales your workload requests up or down to ensure optimal performance and cost-effectiveness.
Getting started
To start using workload optimization, you need to install the Workload Autoscaler component in addition to the custom resource definitions for the recommendation objects. You can do this by getting the install script from our API or using our console once you visit the workload optimization page.
Note that your cluster must be running in automated optimization mode, as workload optimization relies on the cluster controller to create the recommendation objects in the cluster.
Metrics collection and recommendation generation
CAST AI needs to process metrics to generate recommendations, so you need to install a metrics server.
Recommendations are regenerated every 30 minutes. The default configuration is maximum usage over 24 hours with 10% overhead for memory and 80th percentile usage over 24 hours for CPU.
Note: All generated recommendations will consider the current requests/limits.
Applying recommendations automatically
Once the recommendation lands in the cluster, the Workload Autoscaler component is notified that a recommendation has been created or updated.
Next, Workload Autoscaler:
- works as an admission webhook for pods - when pods are created matching the recommendation target, it modifies the pod to have its requests/limits set to what is defined in the recommendation.
- finds the controller and triggers an update to cause the pods controlled by the controller to be re-created (for example, for a deployment object, it adds an annotation to the pod template).
Workload Autoscaler currently supports deployments and rollouts. By default, deployments are updated immediately, which may result in the restart of pods.
Rollouts are updated in a deferred manner. Workload Autoscaler waits for pods to restart naturally before applying new recommendations. Example scenarios include a new service release or a pod dying because of a business or technical error.
How to enable Workload Autoscaler
Scaling policies
Scaling policies allow you to manage all your workloads centrally, with the ability to apply the same settings to multiple workloads simultaneously. Moreover, you can create your custom policy with different settings and apply it to multiple workloads simultaneously.
When you start using our Workload Autoscaler component, all of your workloads will automatically have a default scaling policy applied to them at first, using our default settings. When a new workload appears in the cluster, it will automatically be assigned to the default policy.
If the default scaling policy is suitable for your workloads, you can enable scaling in two ways:
- Globally via the scaling policy by enabling Automatically Optimize Workloads β this will enable scaling only for the workloads we have enough data about. Workloads that arenβt ready will be checked later and enabled once the platform has enough data. When this setting is enabled on the default scaling policy, every new workload created in the cluster will be scaled automatically once the platform has enough data.
- Directly from the workload, β once enabled, autoscaling will start immediately (depending on the autoscaler mode chosen at the policy level).
How to configure recommendations
You can configure recommendations via the API to add additional overhead for a particular resource or change the function used to select the baseline for the recommendation.
For example, you can configure the MAX function to be used for CPU and set the overhead to 20%. This means that the CPU recommendation would be the maximum observed CPU usage over 24 hours plus 20% overhead.
You can find the optimization settings in the scaling policies. You can carry out the following configuration tasks:
- Scale recommendations by adding overhead.
- Fine-tune the percentile values for CPU and memory recommendations.
- Specify the optimization threshold.
You can fine-tune the following settings in the scaling policies:
- Automatically Optimize Workloads β the policy allows you to specify whether our recommendations should be automatically applied to all workloads associated with the scaling policy. This feature enables automation only when there is enough data available to make informed recommendations.
- Recommendation percentile β this section determines which percentile CAST AI will recommend considering the last day of usage. The recommendation will be the average target percentile across all pods spanning the recommendation period. Setting the percentile to 100% will no longer use the average of all pods but the maximum observed value over the period.
- Overhead β it marks how much extra resource should be added on top of the recommendation. By default, it's set to 10% for memory and 0% for CPU.
- Autoscaler mode - this can be set to immediate or deferred.
- Optimization threshold β when automation is enabled and Workload Autoscaler works in immediate mode, this value sets the difference between the current pod requests and the new recommendation so that the recommendation is applied immediately. The default value for both memory and CPU is 10%.
Immediate vs. deferred scaling mode
If the autoscaler mode is set to immediate, it will check if a new recommendation meets the user-set optimization thresholds. It won't be applied if the recommendation doesnβt meet these thresholds. If it does pass the threshold, Workload Autoscaler will automatically modify pod requests per the recommendation.
Moreover, Workload Autoscaler will also apply new recommendations upon natural pod restarts, such as a new service release or when a pod dies due to a business or technical error. This helps to avoid unnecessary pod restarts.
If the scaling mode is set to deferred, Workload Autoscaler will not initiate a forced restart of the pod. Instead, it will apply the recommendation whenever external factors initiate pod restarts.
System overrides for scaling mode
Notice
In certain scenarios, the Workload Autoscaler may override the chosen scaling mode to ensure optimal performance and prevent potential issues.
Here are some cases where the system may default to deferred mode, even if immediate mode is selected:
- Hard node requirements: Workloads with certain specific node constraints are set to deferred mode. This includes:
- Specific pod anti-affinity: If a workload has pod anti-affinity rules that use the
kubernetes.io/hostname
as thetopologyKey
within arequiredDuringSchedulingIgnoredDuringExecution
block. - Host network usage: Pods that require the use of the host's network.
- Specific pod anti-affinity: If a workload has pod anti-affinity rules that use the
These constraints are considered hard node requirements. Using deferred mode in these cases prevents potential scheduling conflicts and resource issues that could arise from immediate pod restarts.
- Rollouts: Workloads of the Rollout kind are always set to deferred mode, as they have their own update mechanisms that could conflict with immediate scaling.
These overrides help maintain cluster stability and prevent scenarios where immediate scaling could lead to increased costs or resource conflicts. Check your workload configurations for these conditions if you notice unexpected deferred scaling behavior.
Mark of recommendation confidence
The "Recommendations Confidence" column can include a mark indicating low confidence in the recommended values.
If an orange mark appears, we don't have sufficient data on workload resource usage to generate trusted recommendations. You can start using Workload Autoscaler if you enable it from a workload level, but we advise waiting at least one week before enrolling your workloads in workload autoscaling.
This mark can appear next to workloads that have run too short for CAST AI to gather enough data and generate accurate recommendations. Workloads that have this mark and belong to a scaling policy that has the "Auto enable" option turned on won't be optimized unless we will get enough data.
How to create a new scaling policy?
Scaling policies are a great tool for managing multiple workloads at once. Some workloads may require a higher overhead, while others would be unnecessary. To create a policy, navigate to Scaling policies and click Create a scaling policy.
Set your desired settings and choose workloads from the list. After everything is set, save the configuration.
Once you have all the required scaling policies, you can switch the policies for your workloads. You can do that in batches or for individual workloads:
- To change a policy for batch workloads, select your workloads in the table, click Assign the policy, choose the policy you want to use, and save your changes.
- To change a policy at the workload level, open the workload drawer, choose a new policy in the drop-down list, and save the changes.
When policy is changed, new configuration settings will impact a new recommendation. The newest data will show new values on workload recommendation graphs.
Enabling Workload Autoscaler for a single workload
To enable optimization for a single workload:
- Select the workload you want to optimize.
- In the drawer that opens, you can change the settings, review the past 7 days' historical usage and recommendations, and request data.
- Once youβve made the review, click the Turn Optimization On button and save the changes.
OOM event handling
Despite careful monitoring and historical data analysis, out-of-memory (OOM) events can occur due to sudden workload spikes or application-level issues.
CAST AI Workload Autoscaler has a robust system for handling possible OOM events in Kubernetes clusters. This feature prevents OOMs by dynamically adjusting memory allocations based on historical data and recent events.
Detection
The system detects OOM events by monitoring pod container statuses.
- The CAST AI Agent collects pod data through regular snapshots.
- Pod container termination states are extracted from these snapshots:
{ "name": "data-analysis-service", "lastState": { "terminated": { "exitCode": 137, "reason": "OOMKilled", "startedAt": "2023-08-16T13:51:08Z", "finishedAt": "2023-08-16T13:58:43Z", "containerID": "containerd://8f7e9bc23a1d5f6g987h654i321j987k654l321m987n654o321p987q654r" } } }
- When an
OOMKilled
state is detected, an OOM event is emitted. - The OOM event is stored in the database to ensure each event is handled only once.
When a container is terminated with the reason OOMKilled
, it triggers the OOM handling process.
Handling
When an OOM event is detected, the system takes the following actions:
- The OOM event handler watches for new OOM events.
- Upon receiving an event, it adjusts the configuration based on the previous workload state.
- A new recommendation is generated with increased memory overhead.
Memory Overhead Adjustment
The system uses an incremental approach to adjust memory overhead:
- Initial OOM event: memory overhead is increased by 10% (x1.1).
- If no further OOM events occur, the overhead slowly decreases back to the original allocation (x1) over time.
- If another OOM event occurs during this decrease:
- The system starts from the current overhead increase.
- An additional 10% is added on top of the current value.
For example:
- If the current overhead is 5% (x1.05) when an OOM occurs again, it will be increased to 15% (x1.15).
- This process can continue up to a maximum of 2.5x the original allocation.
Recommendation regeneration
After adjusting the configuration, the OOM handler invokes the recommendation generator with the latest settings. This process does the following:
- Generates a new recommendation based on the adjusted configuration.
- Emits a
recommendation generated
event. - Applies the new recommendation asynchronously.
Handling memory pressure events
The Workload Autoscaler has built-in logic to handle memory pressure events and prevent pod evictions due to out-of-memory (OOM) issues. This feature is particularly important in tightly packed clusters where increased pod memory usage might trigger eviction events before the OOM kill, and pods might enter eviction loops due to unaddressed memory pressure.
How it works
When a memory pressure eviction event is detected, the autoscaler follows this process:
- Check if the pod experiencing the eviction is managed by CAST AI.
- Determine if this pod is the one causing the memory pressure.
- Verify if the pod's memory usage data is available in the event.
- If all conditions are met, create a system override for minimum memory adjustment.
Minimum memory adjustment calculation
- Pod's memory usage at the time of the eviction event
- Plus the configured memory overhead (as defined in the vertical scaling policy)
This adjustment is applied when generating the next recommendation for the pod and remains in effect for 8 hours to ensure stability.
Criteria for adjustment
It's important to note that this mechanism only applies to pods that meet the following criteria:
Criterion | Requirement |
---|---|
Management | CAST AI must manage the pod |
Cause | The pod must be directly causing memory pressure |
Data Availability | Memory usage data must be available in the eviction event |
Notice
No automatic adjustment is made for pods that don't meet all these criteria
This targeted approach ensures that the autoscaler efficiently addresses the root cause of memory pressure events.
Updated 3 days ago