4 minute read
Recommendation Framework provides the ability to automatically analyze various resources in Kubernetes cluster and make recommendations for optimization。
Crane’s recommendation module periodically detects cluster resource configuration problems and provides optimization suggestions. Framework provides a variety of recommender to implement the optimization and recommendation for different resources. If you want to know how Crane makes Recommendations, or if you want to try to implement a custom Recommender, or change the recommender’s implements, this article will help you know How it does.
The following are typical use cases for recommendation framework:
This is a sample for RecommendationRule: workload-rule.yaml。
apiVersion: analysis.crane.io/v1alpha1
kind: RecommendationRule
metadata:
name: workloads-rule
spec:
runInterval: 24h # Run every 24 hours
resourceSelectors: # configuration for resources
- kind: Deployment
apiVersion: apps/v1
- kind: StatefulSet
apiVersion: apps/v1
namespaceSelector:
any: true # scan all namespace
recommenders: # Replicas and Resource recommenders for Workload
- name: Replicas
- name: Resource
In this example:
runInterval
format is an time interval, for example, 1h or 1m. If this parameter is set to null, the analysis is run only once.resourceSelector
selects resources in k8s by kind, apiVersion, name. If no name is specified, it indicates all resources based on namespaceSelector
.namespaceSelector
defines the namespace of the resource to be analyzed. any: true
indicates that all namespaces are selectedrecommenders
define which recommender should be used to analyze the resources. Currently, supported types: recommendersrecommenders
need to match, such as the Resource recommended default only support Deployments and StatefulSets. Please refer to the Recommender docs to know which resources it supportskubectl apply -f workload-rules.yaml
This example will analysis Resource and Replicas for Deployments and StatefulSets in all namespace.。
kubectl get recommend workloads-rule-replicas-7djlk -o yaml
apiVersion: analysis.crane.io/v1alpha1
kind: Recommendation
metadata:
annotations:
analysis.crane.io/last-start-time: "2023-07-24 11:43:58"
analysis.crane.io/message: 'Failed to run recommendation flow in recommender Replicas:
Replicas CalculatePodTemplateRequests cpu failed: missing request for cpu'
analysis.crane.io/run-number: "59"
creationTimestamp: "2023-06-01T11:37:16Z"
Recommendation
You can filter Recommendation
through these labels,like kubectl get recommend -l analysis.crane.io/recommendation-rule-name=workloads-rule
In general, the namespace of Recommendation
is equal to the namespace of the recommended resource. But Recommendation for idle nodes is excluded, which are in the root namespace of crane, and the default root namespace is Crane-system.
For resource/replicas recommendation and recommendedInfo, users can PATCH status.recommendedinfo to workload to update the resource configuration, for example:
patchData=`kubectl get recommend workloads-rule-replicas-rckvb -n default -o jsonpath='{.status.recommendedInfo}'`;kubectl patch Deployment php-apache -n default --patch "${patchData}"
For idle nodes, users can offline or reduce the capacity of idle nodes based on their requirements.
Currently, Crane support these Recommenders:
Recommender framework defines a set of workflow, The workflow execution sequence according to the process, the process is divided into four stages: Filter, Prepare, Recommend, Observe. Recommender performs recommends functions by implementing these four stages.
If you want to implement or extend a Recommender, please refer to :How to develop recommender
RecommendationConfiguration defines the configuration for recommender. It will deploy a ConfigMap in crane root namespace: recommendation-configuration.
The following is a sample for RecommendationConfiguration.
apiVersion: v1
kind: ConfigMap
metadata:
name: recommendation-configuration
namespace: crane-system
data:
config.yaml: |-
apiVersion: analysis.crane.io/v1alpha1
kind: RecommendationConfiguration
recommenders:
- name: Replicas
acceptedResources: # Accepted resources
- kind: Deployment
apiVersion: apps/v1
- kind: StatefulSet
apiVersion: apps/v1
config: # settings for recommender
workload-min-replicas: "1"
- name: Resource
acceptedResources: # Accepted resources
- kind: Deployment
apiVersion: apps/v1
- kind: StatefulSet
apiVersion: apps/v1
Users can modify ConfigMap and rolling update Crane to make new configuration works.
The more historical data stored in a monitoring system, such as Prometheus, the more accurate the recommendation will be. More than two weeks’s data is recommended for production. Predictions about new apps are often inaccurate.