繁体   English   中英

通过 api 使用的 pvc 存储

[英]Used storage of a pvc via api

我试图从由fabric8io 通过java kubernetes-client 安装到pod 的PVC 中获取space used 直到现在我还没有找到获得这个指标的方法。 所以问题是如何显示指标,例如:

pvcs: [{ 
  name: pvc-abc,
  requests: 500Mi,
  usedStorage: ????   // <---- looking for this metric
}]

通过使用kubernetesClient.persistentVolumeClaims()仅显示 PVC 的容量

PersistentVolumeClaim(apiVersion=v1, kind=PersistentVolumeClaim,..... capacity={storage=500Mi}, conditions=[], phase=Bound, additionalProperties={}), additionalProperties={})

PVC 是一种抽象,表示对存储的请求,并且根本不存储诸如磁盘使用情况之类的信息。 作为更高级别的抽象,它根本不关心消费者如何使用底层存储。

由于您使用 kubernetes-client 使用 java 库,请参阅下面的示例代码:

try {
    String result = apiInstance.connectGetNamespacedPodExec(name, namespace, command, container, stderr, stdin, stdout, tty);
    System.out.println(result);
} catch (ApiException e) {
    System.err.println("Exception when calling CoreV1Api#connectGetNamespacedPodExec");
    e.printStackTrace();
}

命令将是: ["/bin/sh"]和 args: ["-c", "while true; do du -sh /data; sleep 10;done"] 它运行一个简单的 bash 脚本——一个无限的 while 循环,它将当前磁盘使用情况打印到标准输出,它可以用 kubectl 日志读取,而无需使用 kubectl exec 并附加到 Pod。 如果您不需要循环并且只想执行一次,您可以随意修改命令。

这是供您参考的完整示例代码:

// Import classes:
//import io.kubernetes.client.ApiClient;
//import io.kubernetes.client.ApiException;
//import io.kubernetes.client.Configuration;
//import io.kubernetes.client.auth.*;
//import io.kubernetes.client.apis.CoreV1Api;

ApiClient defaultClient = Configuration.getDefaultApiClient();

// Configure API key authorization: BearerToken
ApiKeyAuth BearerToken = (ApiKeyAuth) defaultClient.getAuthentication("BearerToken");
BearerToken.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//BearerToken.setApiKeyPrefix("Token");

CoreV1Api apiInstance = new CoreV1Api();
String name = "name_example"; // String | name of the Pod
String namespace = "namespace_example"; // String | object name and auth scope, such as for teams and projects
String command = "command_example"; // String | Command is the remote command to execute. argv array. Not executed within a shell.
String container = "container_example"; // String | Container in which to execute the command. Defaults to only container if there is only one container in the pod.
Boolean stderr = true; // Boolean | Redirect the standard error stream of the pod for this call. Defaults to true.
Boolean stdin = true; // Boolean | Redirect the standard input stream of the pod for this call. Defaults to false.
Boolean stdout = true; // Boolean | Redirect the standard output stream of the pod for this call. Defaults to true.
Boolean tty = true; // Boolean | TTY if true indicates that a tty will be allocated for the exec call. Defaults to false.
try {
    String result = apiInstance.connectGetNamespacedPodExec(name, namespace, command, container, stderr, stdin, stdout, tty);
    System.out.println(result);
} catch (ApiException e) {
    System.err.println("Exception when calling CoreV1Api#connectGetNamespacedPodExec");
    e.printStackTrace();
}

但是,如果您只想使用 fabric8io 库,那么您可以在这里找到 Kubectl 命令的等效项:

kubectl exec my-pod -- ls / --> PodExecEquivalent.java

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM