DEV Community

Cover image for Kubernetes Resource Usage Monitoring
Labby for LabEx

Posted on

Kubernetes Resource Usage Monitoring

Introduction

This article covers the following tech skills:

Skills Graph

Resource usage is an important aspect of any application running in a Kubernetes cluster. By monitoring resource usage, you can identify performance issues, optimize your resources, and improve the overall efficiency of your applications. In this lab, we will explore how to display resource usage in a Kubernetes cluster using the metrics-server. We will cover CPU and Memory usage.

Enable the Metrics-Server

The metrics-server is a Kubernetes component that collects metrics from various Kubernetes objects and provides them in a consumable format for other Kubernetes components. Before we can display resource usage in our Kubernetes cluster, we need to enable the metrics-server.

minikube addons enable metrics-server
Enter fullscreen mode Exit fullscreen mode

This command will enable the metrics-server in your Kubernetes cluster.

Execute the following command to check whether the metrics-server is running:

kubectl get pods --namespace=kube-system | grep metrics-server
Enter fullscreen mode Exit fullscreen mode

Display Cpu and Memory Usage

To display CPU and Memory usage in a Kubernetes cluster, we will use the kubectl top command. This command allows you to see the resource usage of Kubernetes objects in real-time.

# Display CPU and Memory usage for all pods in a specific namespace
kubectl top pods --namespace=kube-system

# Display CPU and Memory usage for all nodes in the cluster
kubectl top nodes
Enter fullscreen mode Exit fullscreen mode

This command will display the current CPU and Memory usage statistics for all the pods in the specified namespace or all the nodes in the cluster.

Display Container Cpu and Memory Usage

To display the CPU and Memory usage of containers running inside pods, we will use the kubectl top command again.

Create a simple pod that will be used as the template for the replicas. Create a file called myapp-pod.yaml in /home/labex/project/ with the following contents:

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
spec:
  containers:
    - name: myapp-container
      image: nginx
      ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Create the pod using the following command:

kubectl apply -f myapp-pod.yaml
Enter fullscreen mode Exit fullscreen mode

Then, use the follow command to display CPU and Memory usage for a specific container inside a pod

kubectl top pod myapp-pod --namespace=default --containers=true
Enter fullscreen mode Exit fullscreen mode

This command will display the current CPU and Memory usage statistics for the specified container inside the specified pod.

Summary

Congratulations! You have successfully completed the Kubernetes Display Resource Usage lab. In this lab, you have learned how to display resource usage statistics for CPU and Memory in a Kubernetes cluster. By using the metrics-server, you can now monitor resource usage in your applications, optimize your resources, and improve the overall efficiency of your applications running in a Kubernetes cluster.

MindMap


πŸš€ Practice Now: Kubernetes Display Resource Usage


Want to Learn More?

Top comments (0)