Kubernetes Display Resource Usage

KubernetesKubernetesBeginner
Practice Now

Introduction

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ClusterManagementCommandsGroup(["`Cluster Management Commands`"]) kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/ClusterManagementCommandsGroup -.-> kubernetes/top("`Top`") subgraph Lab Skills kubernetes/apply -.-> lab-11358{{"`Kubernetes Display Resource Usage`"}} kubernetes/top -.-> lab-11358{{"`Kubernetes Display Resource Usage`"}} end

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

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

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

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

Create the pod using the following command:

kubectl apply -f myapp-pod.yaml

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

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.

Other Kubernetes Tutorials you may like