Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current resource usage of a pod in Kubernetes with Go client

Tags:

go

kubernetes

The kubernetes go client has tons of methods and I can't find how I can get the current CPU & RAM usage of a specific (or all pods).

Can someone tell me what methods I need to call to get the current usage for pods & nodes?

My NodeList:

nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})

Kubernetes Go Client: https://github.com/kubernetes/client-go

Metrics package: https://github.com/kubernetes/kubernetes/tree/master/staging/src/k8s.io/metrics

As far as I got the metrics server implements the Kubernetes metrics package in order to fetch the resource usage from pods and nodes, but I couldn't figure out where & how they do it: https://github.com/kubernetes-incubator/metrics-server

like image 759
kentor Avatar asked Oct 11 '18 15:10

kentor


People also ask

How do I check CPU usage in Kubernetes cluster?

Get Node CPU usage and memory usage of each node – Kubectl The Simple resource-capacity command with kubectl would return the CPU requests and limits and memory requests and limits of each Node available in the cluster. You can use the --sort cpu. limit flag to sort by the CPU limit.


2 Answers

here is an example.

package main

import (
    "fmt"

    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/tools/clientcmd"
    metrics "k8s.io/metrics/pkg/client/clientset/versioned"
)

func main() {
    var kubeconfig, master string //empty, assuming inClusterConfig
    config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
    if err != nil {
        panic(err)
    }

    mc, err := metrics.NewForConfig(config)
    if err != nil {
        panic(err)
    }
    podMetrics, err := mc.MetricsV1beta1().PodMetricses(metav1.NamespaceAll).List(metav1.ListOptions{})
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    for _, podMetric := range podMetrics.Items {
        podContainers := podMetric.Containers
        for _, container := range podContainers {
            cpuQuantity, ok := container.Usage.Cpu().AsInt64()
            memQuantity, ok := container.Usage.Memory().AsInt64()
            if !ok {
                return
            }
            msg := fmt.Sprintf("Container Name: %s \n CPU usage: %d \n Memory usage: %d", container.Name, cpuQuantity, memQuantity)
            fmt.Println(msg)
        }

    }
}
like image 32
captainchhala Avatar answered Oct 11 '22 12:10

captainchhala


It is correct that go-client does not have support for metrics type, but in the metrics package there is a pregenerated client that can be used for fetching metrics objects and assign them right away to the appropriate structure. The only thing you need to do first is to generate a config and pass it to metrics client. So a simple client for metrics would look like this:

package main


import (
    "k8s.io/client-go/tools/clientcmd"
    metrics "k8s.io/metrics/pkg/client/clientset/versioned"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)



func main() {
    var kubeconfig, master string //empty, assuming inClusterConfig
    config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
    if err != nil{
        panic(err)
    }

    mc, err := metrics.NewForConfig(config)
    if err != nil {
        panic(err)
    }

    mc.MetricsV1beta1().NodeMetricses().Get("your node name", metav1.GetOptions{})
    mc.MetricsV1beta1().NodeMetricses().List(metav1.ListOptions{})
    mc.MetricsV1beta1().PodMetricses(metav1.NamespaceAll).List(metav1.ListOptions{})
    mc.MetricsV1beta1().PodMetricses(metav1.NamespaceAll).Get("your pod name", metav1.GetOptions{})
}

Each of the above methods from metric client returns an appropriate structure (you can check those here) and an error (if any) which you should process according to your requirements.

like image 161
Ottovsky Avatar answered Oct 11 '22 12:10

Ottovsky