github.com/gocrane/crane@v0.11.0/docs/tutorials/resource-recommendation.md (about)

     1  # Resource Recommendation
     2  
     3  Resource recommendation allows you to obtain recommended values for resources in a cluster and use them to improve the resource utilization of the cluster.
     4  
     5  ## Difference between VPA
     6  
     7  Resource recommendations are a lightweight implementation of VPA and are more flexible.
     8  
     9  1. Algorithm: The algorithm model adopts the Moving Window algorithm of VPA, and supports to customization algo args , providing higher flexibility
    10  2. Support batch analysis: With the ResourceSelector, users can batch analyze multiple workloads without creating VPA objects one by one
    11  3. More portable: It is difficult to use VPA's Auto mode in production because it will cause container reconstruction when updating container resource configuration. Resource recommendation provides suggestions to users and leaves the decision of change to users
    12  
    13  ## Create Resource Analytics
    14  
    15  Create an **Resource** `Analytics` to give recommendation for deployment: `nginx-deployment` as a sample.
    16  
    17  
    18  === "Main"
    19  
    20        ```bash
    21        kubectl apply -f https://raw.githubusercontent.com/gocrane/crane/main/examples/analytics/nginx-deployment.yaml
    22        kubectl apply -f https://raw.githubusercontent.com/gocrane/crane/main/examples/analytics/analytics-resource.yaml
    23        kubectl get analytics
    24        ```
    25  
    26  === "Mirror"
    27  
    28        ```bash
    29        kubectl apply -f https://gitee.com/finops/crane/raw/main/examples/analytics/nginx-deployment.yaml
    30        kubectl apply -f https://gitee.com/finops/crane/raw/main/examples/analytics/analytics-resource.yaml
    31        kubectl get analytics
    32        ```
    33  
    34  The created `Analytics` yaml is following:
    35  
    36  ```yaml title="analytics-resource.yaml"  hl_lines="7 24 11-14 28-31"
    37  apiVersion: analysis.crane.io/v1alpha1
    38  kind: Analytics
    39  metadata:
    40    name: nginx-resource
    41  spec:
    42    type: Resource                        # This can only be "Resource" or "Replicas".
    43    completionStrategy:
    44      completionStrategyType: Periodical  # This can only be "Once" or "Periodical".
    45      periodSeconds: 86400                # analytics selected resources every 1 day
    46    resourceSelectors:                    # defines all the resources to be select with
    47      - kind: Deployment
    48        apiVersion: apps/v1
    49        name: nginx-deployment
    50  ```
    51  
    52  The output is:
    53  
    54  ```bash
    55  NAME             AGE
    56  nginx-resource   16m
    57  ```
    58  
    59  You can get view analytics status by running:
    60  
    61  ```bash
    62  kubectl get analytics nginx-resource -o yaml
    63  ```
    64  
    65  The output is similar to:
    66  
    67  ```yaml
    68  apiVersion: analysis.crane.io/v1alpha1
    69  kind: Analytics
    70  metadata:
    71    name: nginx-resource
    72    namespace: default
    73  spec:
    74    completionStrategy:
    75      completionStrategyType: Periodical
    76      periodSeconds: 86400
    77    resourceSelectors:
    78      - apiVersion: apps/v1
    79        kind: Deployment
    80        labelSelector: {}
    81        name: nginx-deployment
    82    type: Resource
    83  status:
    84    conditions:
    85      - lastTransitionTime: "2022-05-15T14:38:35Z"
    86        message: Analytics is ready
    87        reason: AnalyticsReady
    88        status: "True"
    89        type: Ready
    90    lastUpdateTime: "2022-05-15T14:38:35Z"
    91    recommendations:
    92      - lastStartTime: "2022-05-15T14:38:35Z"
    93        message: Success
    94        name: nginx-resource-resource-w45nq
    95        namespace: default
    96        targetRef:
    97          apiVersion: apps/v1
    98          kind: Deployment
    99          name: nginx-deployment
   100          namespace: default
   101        uid: 750cb3bd-0b87-4f87-acbe-57e621af0a1e 
   102  ```
   103  
   104  ## Recommendation: Analytics result
   105  
   106  You can get recommendations that created by above `Analytics` by running.
   107  
   108  ```bash
   109  kubectl get recommend -l analysis.crane.io/analytics-name=nginx-resource -o yaml
   110  ```
   111  
   112  The output is similar to:
   113  
   114  ```yaml 
   115  apiVersion: v1
   116  items:
   117  - apiVersion: analysis.crane.io/v1alpha1
   118    kind: Recommendation
   119    metadata:
   120      creationTimestamp: "2022-06-15T15:26:25Z"
   121      generateName: nginx-resource-resource-
   122      generation: 1
   123      labels:
   124        analysis.crane.io/analytics-name: nginx-resource
   125        analysis.crane.io/analytics-type: Resource
   126        analysis.crane.io/analytics-uid: 9e78964b-f8ae-40de-9740-f9a715d16280
   127        app: nginx
   128      name: nginx-resource-resource-t4xpn
   129      namespace: default
   130      ownerReferences:
   131      - apiVersion: analysis.crane.io/v1alpha1
   132        blockOwnerDeletion: false
   133        controller: false
   134        kind: Analytics
   135        name: nginx-resource
   136        uid: 9e78964b-f8ae-40de-9740-f9a715d16280
   137      resourceVersion: "2117439429"
   138      selfLink: /apis/analysis.crane.io/v1alpha1/namespaces/default/recommendations/nginx-resource-resource-t4xpn
   139      uid: 8005e3e0-8fe9-470b-99cf-5ce9dd407529
   140    spec:
   141      adoptionType: StatusAndAnnotation
   142      completionStrategy:
   143        completionStrategyType: Once
   144      targetRef:
   145        apiVersion: apps/v1
   146        kind: Deployment
   147        name: nginx-deployment
   148        namespace: default
   149      type: Resource
   150    status:
   151      recommendedValue: |
   152        resourceRequest:
   153          containers:
   154          - containerName: nginx
   155            target:
   156              cpu: 100m
   157              memory: 100Mi
   158  kind: List
   159  metadata:
   160    resourceVersion: ""
   161    selfLink: ""
   162  ```
   163  
   164  The `status.recommendedValue.ResourceRequest` is recommended by crane's recommendation engine.
   165  
   166  ## Batch recommendation
   167  
   168  Use a sample to show how to recommend all Deployments and StatefulSets by one `Analytics`:
   169  
   170  ```yaml
   171  apiVersion: analysis.crane.io/v1alpha1
   172  kind: Analytics
   173  metadata:
   174    name: workload-resource
   175    namespace: crane-system               # The Analytics in Crane-system will select all resource across all namespaces.
   176  spec:
   177    type: Resource                        # This can only be "Resource" or "Replicas".
   178    completionStrategy:
   179      completionStrategyType: Periodical  # This can only be "Once" or "Periodical".
   180      periodSeconds: 86400                # analytics selected resources every 1 day
   181    resourceSelectors:                    # defines all the resources to be select with
   182      - kind: Deployment
   183        apiVersion: apps/v1
   184      - kind: StatefulSet
   185        apiVersion: apps/v1
   186  ```
   187  
   188  1. when using `crane-system` as your namespace,`Analytics` selected all namespaces,when namespace not equal `crane-system`,`Analytics` selected the resource that in `Analytics` namespace 
   189  2. resourceSelectors defines the resource to analysis,kind and apiVersion is mandatory,name is optional
   190  3. resourceSelectors supoort any resource that are [Scale Subresource](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#scale-subresource)
   191  
   192  
   193  ## Resource Recommendation Algorithm model
   194  
   195  ### Inspecting
   196  
   197  Workload with not pods: if the workload has no pods exist means that it's not a available workload.
   198  
   199  ### Advising
   200  
   201  VPA's Moving Window algorithm was used to calculate the CPU and Memory of each container and give the corresponding recommended values