HPA not able to fetch metrics from Prometheus in Kubernetes - kubernetes

I have a two node Kubernetes cluster i.e one master node and two worker nodes. For monitoring purpose, I have deployed Prometheus and Grafana. Now, I want to autoscale pods based on CPU usage. But even after configuring Grafana and Prometheus, I am getting the following error ---
Name: php-apache
Namespace: default
Labels: <none>
Annotations: <none>
CreationTimestamp: Mon, 17 Jun 2019 12:33:01 +0530
Reference: Deployment/php-apache
Metrics: ( current / target )
resource cpu on pods (as a percentage of request): <unknown> / 50%
Min replicas: 1
Max replicas: 10
Deployment pods: 1 current / 0 desired
Conditions:
Type Status Reason Message
---- ------ ------ -------
AbleToScale True SucceededGetScale the HPA controller was able to get the target's current scale
ScalingActive False FailedGetResourceMetric the HPA was unable to compute the replica count: unable to get metrics for resource cpu: unable to fetch metrics from resource metrics API: the server could not find the requested resource (get pods.metrics.k8s.io)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedGetResourceMetric 112s (x12408 over 2d4h) horizontal-pod-autoscaler unable to get metrics for resource cpu: unable to fetch metrics from resource metrics API: the server could not find the requested resource (get pods.metrics.k8s.io)
Can anybody let me know why Kubernetes is not fetching metrics from Prometheus ?

Kubernetes retrieves metrics from either the metrics.k8s.io API (normally implemented by the metrics-server which can be seperatly installed) or the custom.metrics.k8s.io API (which can be any type of metric and is normally provided by third parties). To use prometheus in HPA for kubernetes the Prometheus Adapter for the custom metrics API needs to be installed.
A walkthrough for the setup can be found here.

heapster is now depracted : https://github.com/kubernetes-retired/heapster
To enable auto-scaling on your cluster you can use HPA(horizontal pod auto-scaler) and you can also install metrics server to check all metrics.
To install metrics server on kubernetes you can follow this guide also :
amazon : https://docs.aws.amazon.com/eks/latest/userguide/metrics-server.html
https://github.com/kubernetes-incubator/metrics-server
https://medium.com/#cagri.ersen/kubernetes-metrics-server-installation-d93380de008

You don't need custom metrics to use HPA for auto-scaling pods based on their CPU usage.
As #Blokje5 mentioned earlier, you just need to install 'kube-state-metrics'.
The most convenient way to do it is with a dedicated helm chart (kube-state-metrics).
Hint: use override parameters with 'helm install' to create ServiceMonitor object for 'kube-state-metrics' Pod, to allow Prometheus to discover a new target for metrics scraping, e.g.:
helm install stable/kube-state-metrics --set prometheus.monitor.enabled=true
Remark: Pay attention to the 'serviceMonitorSelector' defined in your existing Prometheus resource object/configuration, so that it matches the ServiceMonitor definition for 'kube-state-metrics'. This is to make available Pods' metrics in Prometheus console.

Related

Unable able to see Pods CPU and Memory Utilization and graphs are missing Kubernetes dashboard

K8s VERSION = v1.18.6
I have deployed the Kubernetes dashboard using the following command and added a privileged user with which I logged into the dashboard.
but not able to see Pods CPU and Memory Utilization graphs are missing Kubernetes dashboard
The Kubernetes Metrics Server is an aggregator of resource usage data in your cluster,
To deploy the Metrics Server
Deploy the Metrics Server with the following command:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.6/components.yaml
Verify that the metrics-server deployment is running the desired number of pods with the following command.
kubectl get deployment metrics-server -n kube-system
Output
NAME READY UP-TO-DATE AVAILABLE AGE
metrics-server 1/1 1 1 6m
Also you can validate by below command:
kubectl top nodes
to see node cpu utilisation if it works, it should then come up in Dashboard as well.
Resource usage metrics are only available for K8s clusters once Metrics Server has been installed.

Kubernetes Prometheus metric for HPA (horizontal pod autoscaler) `currentCPUUtilizationPercentage`?

On a Kubernetes cluster with CoreOS Prometheus Operator scraping all standard cluster metrics, what Prometheus metric would show me the currentCPUUtilizationPercentage value for a simple HPA (horizontal pod autoscaler)?
If I setup a simple hpa like:
$ kubectl autoscale deployment php-apache --cpu-percent=30 --min=3 --max=10
And then if I do kubectl get hpa php-apache -o yaml I see something like:
spec:
maxReplicas: 10
minReplicas: 3
targetCPUUtilizationPercentage: 30
...
status:
currentCPUUtilizationPercentage: 28
currentReplicas: 9
desiredReplicas: 9
...
I want to see that currentCPUUtilizationPercentage in Prometheus. I've done a bunch of Prometheus queries to look for this.
I've searched all metrics tagged {hpa="php-apache"} and I see many hpa metrics, but not the metric I'm looking or. I can see kube_hpa_spec_target_metric set to 30 and I can see current status metrics like kube_hpa_status_condition but not the current cpu metric value that I want to see.
I've searched all metrics tagged {metric_name="cpu"} and only see kube_hpa_spec_target_metric
I've searched all container and pod related metrics tagged {container="my-container-name"} and {pod=~"my-pod-prefix.*"} and I see several cpu related metrics like container_cpu_usage_seconds_total abnd container_spec_cpu_quota but nothing similar to or nothing that seems to be able to be used in calculating the currentCPUUtilizationPercentage value that I'm looking for?
FYI, this is on Kubernetes 1.17.x and using a recent version of CoreOS Prometheus Operator.
If I remember correctly currentCPUUtilizationPercentage is a k8 internal metrics for HPA and not exposed directly as a metrics you can scrape with Prometheus.
see https://github.com/kubernetes/community/blob/master/contributors/design-proposals/autoscaling/horizontal-pod-autoscaler.md#autoscaling-algorithm
you probably could scrape component of currentCPUUtilizationPercentage metrics and create custom metrics to see it in Prometheus.

not able to run hpa, get metrics to api metrics

I am trying to run horizontal pod autoscaler in kubernetes, want to auto scale my pods based on cpu default metrics.
For that I installed metrics server after that I can see metrics - metrics.k8s.io/v1beta1 (kubectl api-versions). Then I tried deploying prometheus-operator. But upon runnning kubectl top node/pod - error I am getting is
error: Metrics not available for pod default/web-deployment-658cd556f8-ztf6c, age: 35m23.264812635s" and "error: metrics not available yet"
Do I need to run heapster?
#batman, as you said enabling minikube metrics-server add-on is enough in case of using minikube.
In general case, if using metrics-server you edited the metrics server deployment by running: kubectl edit deployment metrics-server -n kube-system
Under spec: -> containers: add following flag:
spec:
containers:
- command:
- /metrics-server
- --kubelet-insecure-tls
As described on metrics-server github:
--kubelet-insecure-tls: skip verifying Kubelet CA certificates. Not recommended for production usage, but can be useful in test clusters
with self-signed Kubelet serving certificates.
Here you can find tutorial describing HPA using custom metrics and Prometheus.
In minikube, we have to enable metrics-server add-on.
minikube addons list
minikube addons enable metrics-server
Then create hpa, deployment and boom!!
Anyone has done autoscaling based on custom metrics? like based on no. of http requests?

kubernetes autoscale having issues with heapster

I have heapster installed on kubernetes, i am trying to autoscale my pods.. but i keep seeing the following:
unable to get metrics for resource cpu: unable to fetch metrics from resource metrics API: the server could not find the requested resource (get pods.metrics.k8s.io)
The heapster service itself
I1009 14:22:21.014890 1 heapster.go:73] Heapster version v1.4.2
I1009 14:22:21.015226 1 configs.go:61] Using Kubernetes client with master "https://kubernetes.default" and version v1
I1009 14:22:21.015244 1 configs.go:62] Using kubelet port 10250
I1009 14:22:21.030070 1 heapster.go:196] Starting with Metric Sink
I1009 14:22:21.042806 1 heapster.go:106] Starting heapster on port 8082
E1009 14:30:05.000311 1 kubelet.go:280] Node ip-xxxxxx.eu-west-1.compute.internal is not ready
E1009 14:30:05.000342 1 kubelet.go:280] Node ip-xxxxxx.eu-west-1.compute.internal is not ready
E1009 14:30:05.000351 1 kubelet.go:280] Node ip-xxxxxx.eu-west-1.compute.internal is not ready
E1009 14:30:05.000357 1 kubelet.go:280] Node ip-xxxxxx.eu-west-1.compute.internal is not ready
E1009 14:30:05.000363 1 kubelet.go:280] Node ip-xxxxxx.eu-west-1.compute.internal is not ready
E1009 14:30:05.000370 1 kubelet.go:280] Node ip-xxxxxx.eu-west-1.compute.internal is not ready
I want to have autoscaling working with kubernetes, anyone have any ideas?
I know heapster is deprecated, but as of now i cannot chnage or upgrade to metrics server.. so could do with some help
I have once seen similar error. The problem was RBAC issue but the error log was misleading. Make sure you have provided get permission for pods.metrics.k8s.io resource.
Check this similar question: Kubernetes Custom CRD: “Failed to list …: the server could not find the requested resource”
Note: Fetching metrics from Heapster is deprecated as of Kubernetes 1.11. Ref: Horizontal Pod Autoscaler

kubectl get hpa targets:unknow

I have installed kubeadm. Heapster show me metrics, but hpa no
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
httpd Deployment/httpd <unknown> / 2% 2 5 2 19m
kubeadm version
kubeadm version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.6", GitCommit:"7fa1c1756d8bc963f1a389f4a6937dc71f08ada2", GitTreeState:"clean", BuildDate:"2017-06-16T18:21:54Z", GoVersion:"go1.7.6", Compiler:"gc", Platform:"linux/amd64"}
docker version
Client:
Version: 1.11.2
API version: 1.23
Go version: go1.5.4
Git commit: b9f10c9
Built: Wed Jun 1 22:00:43 2016
OS/Arch: linux/amd64
You may have had to enable a metrics-server. Heapster is now deprecated. Also make sure you have Kubernetes version greater than 1.7. You can check this buy typing kubectl get nodes.
You can enable the metrics server by looking at the minikube addons.
minikube addons list gives you the list of addons.
minikube addons enable metrics-server enables metrics-server.
Wait a few minutes, then if you type kubectl get hpa the percentage for the TARGETS <unknown> should appear.
I found the solution:
kubectl describe hpa
failed to get cpu utilization: missing request for cpu on container httpd in pod default/httpd-796666570-2h1c6
Change the yaml of deployment and add:
resources:
requests:
cpu:400m
Then kubectl describe hpa
failed to get cpu utilization: unable to get metrics for resource cpu: no metrics returned from heapster
Wait a few minutes and all works fine.
In kubernetes it can say unknown for hpa. In this situation you should check several places.
In K8s 1.9 uses custom metrics. so In order to work your k8s cluster
with heapster you should check kube-controller-manager.
Add these parameters.
--horizontal-pod-autoscaler-use-rest-clients=false
--horizontal-pod-autoscaler-sync-period=10s
based on https://github.com/kubernetes/kubernetes/issues/57673
Case you should change your heapster deployment.
--source=kubernetes:https://kubernetes.default?kubeletPort=10250&kubeletHttps=true&insecure=true parameter is enough.
I found this link very informative https://blog.inkubate.io/deploy-kubernetes-1-9-from-scratch-on-vmware-vsphere/
you have to enable the metrics server which you can do it using the helm chart
helm chart is easy way to add the metrics server
helm install stable/metrics-server
wait for 3-4 minutes after pods started running and you open kubectl get hpa you can check there target is showing values.
Make sure your spec has this part properly configured:
metrics:
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage}}
In my case I had name: Memory with uppercase M and that cost me a day to find out.