how to read the CPU utilization in k8s LENS - kubernetes

It may sound like a naive question, I am running some load testing on one of the deployments on k8s. So to get an idea of the CPU utilization, I opened LENS HPA and CPU utilization is being shown like this
can anyone please tell me how to understand this number, earlier it was 380/50% for CPU.
I just want to get an idea of what does this number means, if it is 380/50, is my CPU not big enough?

It means probably the same as the output from the kubectl describe hpa {hpa-name}:
$ kubectl describe hpa php-apache
Name: php-apache
...
Metrics: ( current / target )
resource cpu on pods (as a percentage of request): 60% (120m) / 50%
It means that CPU has consumption increased to to x % of the request - good example and explanation in the Kubernetes docs:
Within a minute or so, you should see the higher CPU load; for example:
NAME REFERENCE TARGET MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache/scale 305% / 50% 1 10 1 3m
and then, more replicas. For example:
NAME REFERENCE TARGET MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache/scale 305% / 50% 1 10 7 3m
Here, CPU consumption has increased to 305% of the request.
So in your example (380%/50%) it means that you setup HPA to maintain an average CPU utilization across pods to 50% (by increasing and decreasing number of replicas - updating the deployment) and CPU consumption has increased to 380% so the deployment will be resized automatically.
Also check:
Horizontal Pod Autoscaling
HorizontalPodAutoscaler Walkthrough

Related

why k8s deployment hpa doesn't calculate istio/sidecar 's cpu request

i set hpa for my deployment/app, for example, CPU 80%.
my app deployment has two containers, one is app for traffic, the other is automatically injected istio-proxy.
when i get hpa during running traffic, i found something unexpected for the hpa result.
the cpu request of istio-proxy is 2G.
the cpu request of app is 4G.
the cpu consumed of istio-proxy is 1G.
the cpu consumed of app is 4G.
so, i expected the hpa of this pod (including 2 containers) is (1+2)/(2+4) = 50%.
but the actual result is close to (1+2)/4 = 75%.
it seems the istio-proxy's cpu request is excluded from calculating cpu utilization of hpa.
as i know, k8s get cpu requests from deployment, but actually for this sidecar auto injection case, the deployment yaml doesn't have any istio-proxy container information.
i guess that's why the istio-proxy cpu request is excluded.
but is that the expected behavior or a bug ?
I think as of 1.19, the hpa works on an average value of all containers in the pods. The exact logic is here : https://github.com/kubernetes/kubernetes/blob/v1.9.0/pkg/controller/podautoscaler/metrics/utilization.go#L49
currentUtilization = int32((metricsTotal * 100) / requestsTotal)
As per the above logic HPA is calculating pod cpu utilization as total cpu usage of all containers in pod divided by total request

HPA not scaling down

I hope you can shed some light on this.
I am facing the same issue as described here: Kubernetes deployment not scaling down even though usage is below threshold
My configuration is almost identical.
I have checked the hpa algorithm, but I cannot find an explanation for the fact that I am having only one replica of my-app3.
Any hints?
kubectl get hpa -A
NAMESPACE NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
my-ns1 my-app1 Deployment/my-app1 49%/75%, 2%/75% 1 10 2 20h
my-ns2 my-app2 Deployment/my-app2 50%/75%, 10%/75% 1 10 2 22h
my-ns2 my-app3 Deployment/my-app3 47%/75%, 10%/75% 1 10 1 22h
kubectl top po -A
NAMESPACE NAME CPU(cores) MEMORY(bytes)
my-ns1 pod-app1-8d694bc8f-mkbrh 1m 76Mi
my-ns1 pod-app1-8d694bc8f-qmlnw 1m 72Mi
my-ns2 pod-app2-59d895d96d-86fgm 1m 77Mi
my-ns2 pod-app2-59d895d96d-zr67g 1m 73Mi
my-ns2 pod-app3-6f8cbb68bf-vdhsd 1m 47Mi
Posting this answer as it could be beneficiary for the community members on why exactly Horizontal Pod Autoscaler decided not to scale the amount of replicas in this particular setup.
The formula for amount of replicas workload will have is:
desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]
Following on the describe of HPA:
NAMESPACE NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
my-ns1 my-app1 Deployment/my-app1 49%/75%, 2%/75% 1 10 2 20h
my-ns2 my-app2 Deployment/my-app2 50%/75%, 10%/75% 1 10 2 22h
my-ns2 my-app3 Deployment/my-app3 47%/75%, 10%/75% 1 10 1 22h
HPA decides on the amount of replicas on the premise of their current amount.
A side note: In the setup that uses multiple metrics (for example CPU and RAM) it will use the higher metric and act accordingly.
Also please consider that downscaling has a cooldown.
Calculation on each of the Deployments
ceil[] - round a number up:
ceil(4,55) = 5
ceil(4,01) = 5
app1:
Replicas = ceil[2 * (49 / 75)]
Replicas = ceil[2 * 0,6533..]
Replicas = ceil[1,3066..]
Replicas = 2
This example shows that there will be no changes to be amount of replicas.
Amount of replicas would go:
Up when the currentMetricValue (49) would exceed the desiredMetricValue (75)
Down when the currentMetricValue (49) would be less than half of the desiredMetricValue (75)
app2 is in the same situation as app1 so it can be skipped
app3:
Replicas = ceil[1 * (49 / 75)]
Replicas = ceil[1 * 0,6266..]
Replicas = ceil[0,6266..]
Replicas = 1
This example also shows that there will be no changes to be amount of replicas.
Amount of replicas would go:
Up when the currentMetricValue (47) would exceed the desiredMetricValue (75)
Additional resources:
Kubernetes.io: Docs: Tasks: Run application: Horizontal pod autoscale
Indeed from my research it seems that the HPA algorithm works in this way:
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#algorithm-details
Do not know the reason why my-app3 was assigned one replica and the other two apps two replicas, but according to the algorithm it is not needed to scale out at this time.

How Kubernetes Horizontal Pod Autoscaler calculates CPU percentage?

I set up my cluster and I want my deployments to scale up when the first pod uses 75% of one cpu (core). I did this with hpa and everything is working but I noticed that the hpa percentage is strange.
Based on what I know 1 cpu = 1000 milliunits and what I see in kubectl top pods is pod-A using 9m but what I see in kubectl get hpa is pod-A 9%/75% which doesn't make sense, 9% of 1000 is 90 and not 9.
I want to know how hpa is calculating the percentage and how shall I configure it so when I reach 75% of one cpu it scales up?
To the horizontal pod autoscaler 100% of a metric (cpu or memory) is the amount set in resource requests. So if you pod requests 100m cpu, 9m is 9% and it would scale out on 75m.
Double check if you really have requested 1 (or 1000m) cpu by issuing kubectl describe pod <pod-name>.

Kuberenetes hpa patch command not working

I have Kuberenetes cluster hosted in Google Cloud.
I deployed my deployment and added an hpa rule for scaling.
kubectl autoscale deployment MY_DEP --max 10 --min 6 --cpu-percent 60
waiting a minute and run kubectl get hpa command to verify my scale rule - As expected, I have 6 pods running (according to min parameter).
$ kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
MY_DEP Deployment/MY_DEP <unknown>/60% 6 10 6 1m
Now, I want to change the min parameter:
kubectl patch hpa MY_DEP -p '{"spec":{"minReplicas": 1}}'
Wait for 30 minutes and run the command:
$ kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
MY_DEP Deployment/MY_DEP <unknown>/60% 1 10 6 30m
expected replicas: 1, actual replicas: 6
More information:
You can assume that the system has no computing anything (0% CPU
utilization).
I waited for more than an hour. Nothing changed.
The same behavior is seen when i deleted the scaling rule and deployed it
again. The replicas parameter has not changed.
Question:
If I changed the MINPODS parameter to "1" - why I still have 6 pods? How to make Kubernetes to actually change the min pods in my deployment?
If I changed the MINPODS parameter to "1" - why I still have 6 pods?
I believe the answer is because of the <unknown>/60% present in the output. The fine manual states:
Please note that if some of the pod's containers do not have the relevant resource request set, CPU utilization for the pod will not be defined and the autoscaler will not take any action for that metric
and one can see an example of 0% / 50% in the walkthrough page. Thus, I would believe that since kubernetes cannot prove what percentage of CPU is being consumed -- neither above nor below the target -- it takes no action for fear of making whatever the situation is worse.
As for why there is a <unknown>, I would hazard a guess it's the dreaded heapster-to-metrics-server cutover that might be obfuscating that information from the kubernetes API. Regrettably, I don't have first-hand experience testing that theory, in order to offer you concrete steps beyond "see if your cluster is collecting metrics in a place that kubernetes can see them."

Kubernetes autoscaler targetCPUUtilizationPercentage

I'm defining this autoscaler with kubernetes and GCE and I'm wondering what exactly should I specify for targetCPUUtilizationPercentage. That target points to what exactly? Is it the total CPU in my cluster? When the pods referenced in this autoscaler consume more than targetCPUUtilizationPercentage what happens?
The CPU utilization is the average CPU usage of a all pods in a deployment across the last minute divided by the requested CPU of this deployment. If the mean of the pods' CPU utilization is higher than the target you defined, the your replicas will be adjusted.
You can read more about this topic here.
This is average cpu utilisation of all the pods, so if you have given CPU as 200 in the resource requests and targetCPUUtilizationPercentage as 80%, then at 160 value of threshold, it will scale out the pod. It will create a new repliace.