Now that we know how the Kubernetes Metrics Server integrates into your cluster, let’s look at installing it. We will
be using minikube to work through the demos in this article. If you are unfamiliar with or need to install minikube,
you can follow the steps
here and
return to the article when it is up and running.
Set up a test cluster
Let’s create a local testing cluster using minikube. First, start minikube on your local machine:
$ minikube start --memory 8000 --cpus 2 --nodes 2
After a few minutes, verify that your cluster is ready:
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane 2m16s v1.27.4
minikube-m02 Ready <none> 38s v1.27.4
Now let’s have a look at what happens if you try to run kubectl top
:
$ kubectl top nodes
error: Metrics API not available
As anticipated, this command fails. We need to install the Metrics Server first.
Install the Kubernetes Metrics Server
The easiest way to install the Metrics Server is by applying the
official manifest file
to your cluster. Another way is to use the
official Helm chart, which gives you
more control over server deployment. If you are familiar with Helm charts, then the easiest way to install is via the
Helm chart; otherwise, using the official manifest file is straightforward.
The example below installs the
Metrics Server via the official manifest file:
$ kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
This next step patches the deployment to accept insecure certificates. This is needed because we are running a test
cluster. In production, you should ensure that the Kubelet certificates are signed by the cluster CA:
$ kubectl -n kube-system patch deployment/metrics-server --type=json --patch='[{"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": "--kubelet-insecure-tls"}]'
After a few seconds, the deployment will restart. Verify that the kubectl top
command works as follows:
$ kubectl top nodes
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
minikube 100m 10% 1552Mi 20%
Use Kubectl top to view usage
Once the metrics server is installed, you can view real-time usage for the top pods and nodes using the
kubectl top sub-command. Here are some examples.
View the usage for the top nodes:
$ kubectl top nodes
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
minikube 155m 7% 1442Mi 18%
minikube-m02 72m 3% 1039Mi 13%
View the usage for the top pods across all namespaces:
$ kubectl top pods -A
NAMESPACE NAME CPU(cores) MEMORY(bytes)
kube-system coredns-7db6d8ff4d-ks4lr 4m 12Mi
kube-system etcd-minikube 51m 34Mi
kube-system kindnet-bfl8s 1m 7Mi
kube-system kindnet-rg6kv 1m 7Mi
kube-system kube-apiserver-minikube 118m 184Mi
kube-system kube-controller-manager-minikube 35m 41Mi
kube-system kube-proxy-hkrml 1m 10Mi
kube-system kube-proxy-splqc 1m 10Mi
kube-system kube-scheduler-minikube 5m 14Mi
kube-system metrics-server-d994c478f-mzkd2 17m 16Mi
kube-system storage-provisioner 4m 7Mi
You can also break down usage at the container level:
kubectl top pods -A --containers
NAMESPACE POD NAME CPU(cores) MEMORY(bytes)
kube-system coredns-7db6d8ff4d-ks4lr coredns 5m 12Mi
kube-system etcd-minikube etcd 51m 34Mi
kube-system kindnet-bfl8s kindnet-cni 1m 7Mi
kube-system kindnet-rg6kv kindnet-cni 1m 7Mi
kube-system kube-apiserver-minikube kube-apiserver 149m 184Mi
kube-system kube-controller-manager-minikube kube-controller-manager 40m 41Mi
kube-system kube-proxy-hkrml kube-proxy 1m 10Mi
kube-system kube-proxy-splqc kube-proxy 1m 10Mi
kube-system kube-scheduler-minikube kube-scheduler 7m 14Mi
kube-system metrics-server-d994c478f-mzkd2 metrics-server 11m 16Mi
kube-system storage-provisioner storage-provisioner 4m 7Mi