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/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ClusterManagementCommandsGroup(["`Cluster Management Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicsGroup(["`Basics`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/logs("`Logs`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/ClusterManagementCommandsGroup -.-> kubernetes/top("`Top`") kubernetes/BasicsGroup -.-> kubernetes/initialization("`Initialization`") subgraph Lab Skills kubernetes/describe -.-> lab-11358{{"`Kubernetes Display Resource Usage`"}} kubernetes/logs -.-> lab-11358{{"`Kubernetes Display Resource Usage`"}} kubernetes/get -.-> lab-11358{{"`Kubernetes Display Resource Usage`"}} kubernetes/top -.-> lab-11358{{"`Kubernetes Display Resource Usage`"}} kubernetes/initialization -.-> lab-11358{{"`Kubernetes Display Resource Usage`"}} end

Start the Minikube Cluster

Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.

  1. Navigate to your working directory:

    Open the terminal and navigate to the default project folder:

    cd /home/labex/project
  2. Start Minikube:

    Start Minikube to initialize a Kubernetes cluster:

    minikube start
    • This command sets up a single-node Kubernetes cluster on your local machine.
    • Minikube may take a few minutes to start depending on your system's performance.
  3. Verify Minikube is running:

    Check the status of the Minikube cluster:

    minikube status
    • Look for components like kubelet and apiserver listed as Running.
    • If the cluster is not running, rerun minikube start.

If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.

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