Kubernetes Java API for Metrics

Viewed 892

I am getting the pod metrics through kubectl command

āžœ  kubectl top pods
NAME                CPU(cores)   MEMORY(bytes)
api-6bd876fc8b-85dnx   72m          553Mi
ui-67b794cf8d-gcrg5    0m           1Mi

but I want to try through Java client https://github.com/kubernetes-client/java/

Is there any support to get the Metrics for Pods and Nodes through kubernetes-client ?

3 Answers

as blue man said, it is now possible to get this metrics
Java Solution

PodMetricsList list = new Metrics(client).getPodMetrics("playground");

for(PodMetrics podMetrics:list.getItems()){
    for(ContainerMetrics containerMetrics:podMetrics.getContainers()){
                System.out.println(containerMetrics.getUsage());
    }
}

Kotlin Solution

val client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
Metrics(client).getPodMetrics(
        "name-space"
).items.forEach {
    it.containers.forEach {metrics ->
        println(metrics.usage)
    }
}

This should give the metrics you are looking for.

Sample Output

{cpu=Quantity{number=0.011135729, format=DECIMAL_SI}, memory=Quantity{number=831680512, format=BINARY_SI}}

{cpu=Quantity{number=0.008742460, format=DECIMAL_SI}, memory=Quantity{number=781041664, format=BINARY_SI}}
Related