Fix “error: Metrics API not available” in Kubernetes
Are you encountering the “error: Metrics API not available” after setting up Metrics Server in your Kubernetes cluster? Metrics Server is used in Kubernetes to collect resource usage data from pods and nodes in the cluster. The metrics collected are accessed centrally via the Kubernetes API, and this allows other cluster components and users to query resource usage in the cluster. The use case can be Horizontal Pod Autoscaling.
To resolve “error: Metrics API not available” in Kubernetes, let’s first uninstall Metrics server if you have it modified.
kubectl delete -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
Fix by patching the deployment
Install Metrics server by running the following commands:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
Check metrics server deployment in yaml output:
kubectl -n kube-system get deployment metrics-server -o yaml
We will patch the deployment to have the following settings:
领英推荐
Execute the commands to patch the deployment.
kubectl patch deployment metrics-server -n kube-system --type='json' -p='[{"op": "add","path": "/spec/template/spec/hostNetwork","value": true},{"op": "replace","path": "/spec/template/spec/containers/0/args","value": ["--cert-dir=/tmp","--secure-port=4443","--kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname","--kubelet-use-node-status-port","--metric-resolution=15s","--kubelet-insecure-tls"]},{"op": "replace","path": "/spec/template/spec/containers/0/ports/0/containerPort","value": 4443}]'
After some seconds the pod status should be running and active:
$ kubectl -n kube-system get pods -l k8s-app=metrics-server
NAME READY STATUS RESTARTS AGE
metrics-server-58fb664478-n4rdj 1/1 Running 0 1m
Check the metrics API status:
$ kubectl get apiservices -l k8s-app=metrics-server
NAME SERVICE AVAILABLE AGE
v1beta1.metrics.k8s.io kube-system/metrics-server True 2m
Test if the metrics server is running by checking your kubernetes nodes utilization
$ kubectl top nodes
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
k8smas01.novalocal 183m 2% 2231Mi 16%
k8smas02.novalocal 376m 4% 1974Mi 14%
k8smas03.novalocal 289m 3% 1872Mi 13%