Kubernetes Java API for Java

dy2hfwbg  于 2023-10-17  发布在  Kubernetes
关注(0)|答案(4)|浏览(103)

我正在通过kubectl命令获取pod指标

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

但我想尝试通过Java客户端https://github.com/kubernetes-client/java/
是否支持通过kubernetes-client获取Pod和Nodes的配置文件?

z2acfund

z2acfund1#

官方的Java客户端库中不包括资源管理器API。所以你必须查询API服务器,如下所示:

GET /apis/metrics/v1alpha1/namespaces/{namespace}/pods/{pod}

它将返回当前度量的结果。
以下是一些能够添加更多自定义指标的参考:

希望这能回答你的问题。

pkwftd7m

pkwftd7m2#

正如蓝人所说,现在有可能获得这些指标,
Java解决方案

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

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

ghg1uchk3#

Kotlin解决方案

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

这应该给给予您正在寻找的指标。
示例输出

{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}}
mbskvtky

mbskvtky4#

您可以通过更改以下参数来测试Kubernetes集群。(应该在集群上安装metrics server)
ApiClient client;//根据需要创建客户端

import com.google.gson.JsonElement;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.Pair;
import io.kubernetes.client.util.Config;
import okhttp3.Call;
import org.springframework.boot.SpringApplication;

String apiPath = "    ";
        Object localVarPostBody = null;
        List<Pair> localVarCollectionQueryParams = new ArrayList <>();
        List < Pair > localVarQueryParams = new ArrayList <>();
        localVarQueryParams.addAll(client.parameterToPair("pretty", "true"));
        Map <String, String> localVarHeaderParams = new HashMap <>();
        Map<String, String> localVarCookieParams = new HashMap <>();
        Map<String, Object> localVarFormParams = new HashMap <>();
        String[] localVarAccepts = new String[]{"application/json"};
        String localVarAccept = client.selectHeaderAccept(localVarAccepts);
        if (localVarAccept != null) {
            localVarHeaderParams.put("Accept", localVarAccept);
        }
        String[] localVarContentTypes = new String[0];
        String localVarContentType = client.selectHeaderContentType(localVarContentTypes);
        localVarHeaderParams.put("Content-Type", localVarContentType);
        String[] localVarAuthNames = new String[]{"BearerToken"};
        Call call = client.buildCall(apiPath, "GET", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAuthNames, null);
        JsonElement element = (JsonElement)client.execute(call, JsonElement.class).getData();
        System.out.println(element);

apiPath = /apis/metrics.k8s.io/v1beta1/namespaces/{namespace}/pod/{pod},/apis/metrics.k8s.io/v1beta1/pods
根据pod name下的container vice,可以得到“cpu”和“memory”的使用情况。

<dependency>
    <groupId>io.kubernetes</groupId>
    <artifactId>client-java</artifactId>
    <version>11.0.2</version>
    <scope>compile</scope>
</dependency>

相关问题