Oct 23rd, ‘24/6 min read

Understanding Kubernetes Metrics Server: Your Go-to Guide

Learn how the Kubernetes Metrics Server helps monitor resource usage like CPU and memory, ensuring smooth cluster performance and scalability.

Understanding Kubernetes Metrics Server: Your Go-to Guide

When working with Kubernetes, keeping track of resource usage is crucial for your cluster's performance.

The Kubernetes Metrics Server collects key data like CPU and memory usage and shares it with the Kubernetes API server through the Metrics API. This way, you can ensure everything runs smoothly!

This guide will help you understand what the Metrics Server does, how to set it up, and how it fits into the bigger Kubernetes picture.

What Exactly Is the Kubernetes Metrics Server?

Think of the Kubernetes Metrics Server as the cluster's resource usage hub. It gathers metrics like CPU and memory use from containers, pods, and nodes. The Kubernetes control plane then uses this data to make important decisions, like scheduling and autoscaling.

Here are a few things to keep in mind about the Metrics Server:

  • It's a lightweight tool with short-term, in-memory metrics storage.
  • It's not built for storing metrics long-term or handling complex queries.
  • It plays a critical role in autoscaling your workloads in Kubernetes.
📝
If you're looking to learn more about kubectl exec, check out our blog that covers key commands, examples, and best practices.

Metrics Server vs. Other Solutions

When comparing the Metrics Server with other Kubernetes monitoring tools, it's important to note that each one has a different focus.

Kube State Metrics vs. Metrics Server

  • The Metrics Server is all about tracking real-time resource usage (like CPU and memory) for pods and nodes.
  • Kube State Metrics, on the other hand, focuses on the metadata of Kubernetes objects—things like the status of deployments, nodes, and pods.

Prometheus vs. Metrics Server

  • The Metrics Server gives you real-time data for resource usage, which is essential for autoscaling.

Prometheus, meanwhile, goes beyond that. It offers long-term storage, more advanced querying options, and alerting features, making it a go-to for more complex monitoring needs.

Setting Up the Metrics Server

Installation
You can easily install the Metrics Server using kubectl:

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

Or, if you prefer Helm:

helm repo add metrics-server https://kubernetes-sigs.github.io/metrics-server/ 
helm upgrade --install metrics-server metrics-server/metrics-server

Configuration
The Metrics Server can be fine-tuned with command-line flags or a configuration file. A common setup might look like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: metrics-server-config
  namespace: kube-system
data:
  args:
    - --kubelet-insecure-tls
    - --kubelet-preferred-address-types=InternalIP
📖
For a closer look at monitoring ephemeral storage metrics in Kubernetes, take a moment to explore our blog, where we break down the key details and tips.

Accessing Metrics
Once the Metrics Server is up and running, you can view metrics using the kubectl top command:

kubectl top nodes
kubectl top pods --all-namespaces

You can also access the Metrics API directly if needed:

kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes"

Horizontal Pod Autoscaling (HPA)
The Metrics Server plays a key role in Horizontal Pod Autoscaling (HPA). Here's an example of how HPA can scale a deployment based on CPU usage:

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: php-apache
  minReplicas: 1
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        targetAverageUtilization: 50

In this setup, the HPA adjusts the number of pods in the php-apache deployment to keep CPU utilization around 50%.

Troubleshooting Metrics Server Issues


Here are some common problems with the Metrics Server and how you can solve them:

  • Metrics not available: First, check if the Metrics Server pod is up and running, and look at the logs for any errors.
kubectl get pods -n kube-system | grep metrics-server
kubectl logs -n kube-system metrics-server-<pod-id>
  • Certificate issues: If you're running into TLS certificate problems, try using the --kubelet-insecure-tls flag.
  • Resource constraints: Make sure the Metrics Server has enough CPU and memory to do its job. You can define resource requests and limits like this:
resources:
  requests:
    cpu: 100m
    memory: 200Mi
  limits:
    cpu: 200m
    memory: 500Mi

Metrics Server in Different Environments

  • K3s Metrics Server: If you're using K3s, the lightweight Kubernetes distribution, good news—Metrics Server is included by default, so there's no extra setup required.
  • GKE Metrics Server: On Google Kubernetes Engine (GKE), the Metrics Server is automatically enabled for clusters running Kubernetes 1.12 or later.
  • Minikube Metrics Server: If you're using Minikube for local development, just run this command to enable the Metrics Server:
minikube addons enable metrics-server
📖
To learn how to add cluster labels to Kubernetes metrics, check out our detailed guide that walks you through the process step by step.

Advanced Topics

High Availability: If you're using the Metrics Server in a production environment, you might want to run multiple replicas for better reliability.

Here’s how you can do it:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: metrics-server
  namespace: kube-system
spec:
  replicas: 2

Integration with Custom Metrics: The Metrics Server focuses on CPU and memory metrics, but if you need application-specific metrics, you can extend Kubernetes with custom metrics adapters.

To use custom metrics, you typically need a custom metrics adapter that collects and exposes the metrics you care about.

Here's an example of how you might define a Horizontal Pod Autoscaler (HPA) that scales based on a custom metric:

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: custom-metric-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: your-app-deployment
  minReplicas: 1
  maxReplicas: 5
  metrics:
  - type: Pods
    pods:
      metricName: custom_metric_name
      targetAverageValue: 50

In this example:

  • custom_metric_name is the name of the custom metric you're tracking.
  • The HPA will scale the your-app-deployment deployment to maintain a target average value of 50 for the custom metric.

You'll also need to install and configure a custom metrics adapter like Prometheus Adapter or k8s-prometheus-adapter to expose custom metrics for Kubernetes. This adapter acts as a bridge between your metric system and Kubernetes' custom metrics API.

Conclusion

The Kubernetes Metrics Server is a key piece in managing resources and autoscaling within your cluster. Knowing how to set it up, troubleshoot issues, and integrate it with other tools will help you keep your cluster running smoothly.

While the Metrics Server provides critical real-time data, combining it with more robust tools like Prometheus will give you the full monitoring and observability experience.

🤝
If you have any questions or just want to chat, feel free to hop into our Discord community! We have a dedicated channel where you can share your specific use case and connect with other developers. We’d love to hear from you!

FAQs

What is the Kubernetes Metrics Server?

It's a cluster-wide tool that collects CPU and memory data from your nodes and pods and then exposes that information through the Kubernetes API server using the Metrics API.

What's the difference between Kube State Metrics and Metrics Server? The Metrics Server focuses on real-time CPU and memory usage for autoscaling, while Kube State Metrics provides detailed metadata about the state of Kubernetes objects, like deployments and nodes.

Does Kubernetes use Prometheus?

Kubernetes doesn't include Prometheus by default, but it's a popular choice for monitoring Kubernetes clusters. While the Metrics Server gives you basic real-time data, Prometheus offers long-term storage, advanced querying, and alerting.

How does Horizontal Pod Autoscaling (HPA) work?

HPA automatically adjusts the number of pods in a deployment based on the metrics collected by the Metrics Server, like CPU usage. It scales the pods up or down to maintain a target metric value.

How can I access the Kubernetes Metrics Server API?
You can access it using kubectl with commands like:

kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes"  
kubectl top nodes  
kubectl top pods  

How do I troubleshoot Metrics Server issues?
Start by checking if the Metrics Server pod is running and reviewing the logs:

kubectl get pods -n kube-system | grep metrics-server  
kubectl logs -n kube-system <metrics-server-pod-name>  

Ensure it has the proper RBAC permissions and that the Metrics API is accessible:

kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes"  

How do I install and configure the Metrics Server?
You can install it using kubectl or Helm:

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml  
helm repo add metrics-server https://kubernetes-sigs.github.io/metrics-server/  
helm upgrade --install metrics-server metrics-server/metrics-server  

What is the K3s Metrics Server?
K3s includes the Metrics Server by default, so you don’t need to install anything extra.

What is the GKE Metrics Server?
If you're running GKE, the Metrics Server is automatically enabled for clusters on Kubernetes 1.12 and above.

How do I set up a local Kubernetes cluster using Minikube?
Start Minikube, and enable the Metrics Server with:

minikube start  
minikube addons enable metrics-server  

How do I monitor Docker metrics using Prometheus and Grafana?
You can install Prometheus and Grafana in your cluster, configure Prometheus to scrape Docker metrics, and set up Grafana dashboards for visualization. This setup provides a more comprehensive monitoring solution than Metrics Server alone.

Newsletter

Stay updated on the latest from Last9.

Authors

Anjali Udasi

Helping to make the tech a little less intimidating. I love breaking down complex concepts into easy-to-understand terms.

Handcrafted Related Posts