Enable Kubernetes Metrics Server on Docker Desktop

Sujay Pillai - Sep 20 '21 - - Dev Community

The steps below in this blog will help you setup Kubernetes Metrics Server on Docker Desktop which provides a standalone instance of Kubernetes running as a Docker container.

Kubernetes Metrics Server is a scalable, efficient source of container resource metrics for Kubernetes built-in autoscaling pipelines. Metrics Server collects resource metrics from Kubelets and exposes them in Kubernetes apiserver through Metrics API for use by Horizontal Pod Autoscaler and Vertical Pod Autoscaler.

Metrics Server offers:

  • A single deployment that works on most clusters
  • Scalable support up to 5,000 node clusters
  • Resource efficiency: Metrics Server uses 1m core of CPU and 3 MB of memory per node

You can use Metrics Server for:

  • CPU/Memory based horizontal autoscaling
  • Automatically adjusting/suggesting resources needed by containers

Prerequisites

Once you have enabled the Kubernetes on Docker Desktop, and if you run the below commands you should see messages like:



kubectl top node 
error: Metrics API not available


Enter fullscreen mode Exit fullscreen mode


kubectl top pod -A
error: Metrics API not available


Enter fullscreen mode Exit fullscreen mode

Metrics server isn't included with Docker Desktop's installation of Kubernetes and to install it we will have to download the latest components.yaml file from Metrics-Server releases page and open it in your text editor.

If you try to execute the command kubectl apply -f components.yaml you will see the pods get created but with some errors as highlighted below:

MetricsServer_Setup

Add the line --kubelet-insecure-tls under the args section as shown below [L136] :
MetricsServer_Setup02

Execute the command kubectl apply -f components.yaml to apply the changes:
MetricsServer_Setup03

Now if you execute the kubectl top node & kubectl top pod -A commands you should see the output:



$ kubectl top node 
NAME             CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
docker-desktop   1310m        32%    1351Mi          71%       


Enter fullscreen mode Exit fullscreen mode


$ kubectl top pod -A
NAMESPACE     NAME                                     CPU(cores)   MEMORY(bytes)   
cpu-example   cpu-demo                                 1003m        1Mi             
kube-system   coredns-f9fd979d6-g2rfx                  9m           9Mi             
kube-system   coredns-f9fd979d6-wndgm                  6m           9Mi             
kube-system   etcd-docker-desktop                      35m          36Mi            
kube-system   kube-apiserver-docker-desktop            55m          325Mi           
kube-system   kube-controller-manager-docker-desktop   41m          47Mi            
kube-system   kube-proxy-s72fj                         1m           25Mi            
kube-system   kube-scheduler-docker-desktop            9m           17Mi            
kube-system   metrics-server-56c59cf9ff-jndxd          10m          14Mi            
kube-system   storage-provisioner                      4m           5Mi             
kube-system   vpnkit-controller                        1m           15Mi 


Enter fullscreen mode Exit fullscreen mode

You can also use Kubernetes Dashboard to view the above data (and more information) in a web UI. It allows users to manage applications running in the cluster and troubleshoot them, as well as manage the cluster itself.

To deploy Dashboard, execute following command:



$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.1.0/aio/deploy/recommended.yaml


Enter fullscreen mode Exit fullscreen mode

To access Dashboard from your local workstation you must create a secure channel to your Kubernetes cluster. Run the following command:



$ kubectl proxy


Enter fullscreen mode Exit fullscreen mode

Get the token for login to dashboard using the below command:



$ kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret |grep default-token | awk '{print $1}')


Enter fullscreen mode Exit fullscreen mode

To access the HTTPS endpoint of dashboard go to:



http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/


Enter fullscreen mode Exit fullscreen mode

Login to the dashboard using the token from above step and you should see a dashboard as below:
Metrics_Dashboard

This setting should only be used for the local Docker Desktop Kubernetes cluster, and not recommended for any hosted or production clusters.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .