【问题标题】:Used storage of a pvc via api通过 api 使用 pvc 存储
【发布时间】:2021-12-11 16:18:34
【问题描述】:

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

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

通过使用kubernetesClient.persistentVolumeClaims()只显示一个PVC的容量

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

【问题讨论】:

  • 请提供您使用的云提供商还是裸机集群?
  • 它的裸机,通过 Rancher 创建的 k8s

标签: java kubernetes


【解决方案1】:

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 库,那么 here 您可以找到 Kubectl 命令的等效项:

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


通过回复答案下的评论来扩展答案:

如果我们不讨论 java 及其代码(我不擅长),我们将讨论 Kubernetes 决策,那么我们可以部署一个 pod,将所有 PVC 使用的指标放在一个地方。

您可以创建一个运行在 nginx 容器旁边的边车,该容器运行那个简单的 bash 脚本 - 一个无限的 while 循环(它可以是单个运行命令,没有循环),它的唯一功能是挂载相同的卷并检查它的当前使用情况并将当前磁盘使用情况打印到标准输出:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      volumes:
      - name: media
        persistentVolumeClaim:
          claimName: media
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: "/data"
          name: media
      - name: busybox
        image: busybox
        command: ["/bin/sh"]
        args: ["-c", "while true; do du -sh /data; sleep 10;done"]
        volumeMounts:
        - mountPath: "/data"
          name: media

使用kubectl logs &lt;pod_name&gt; &lt;container_name&gt; 可以轻松阅读,无需使用kubectl exec 并附加到Pod

$ kubectl logs nginx-deployment-56bb5c87f6-dqs5h busybox
20.0K   /data
20.0K   /data
20.0K   /data

您可以通过kubectl apply -f申请

我想它也可以更有效地用于配置某种磁盘使用监控。

【讨论】:

  • 非常感谢您抽出宝贵时间。因此,如果我理解正确,没有一种简单的方法可以在一个 NS 中使用所有 pvc。您是否有一个想法可以检查一个 NS 中所有 pvc 的指标。有RWO,所以将它安装到临时吊舱并执行du 命令是不可能的?!?
  • @imalik8088,在此处扩展回答您的问题的答案
  • 嗨@WytrzymałyWiktor 还没有,或者说更好。我有一个更动态的命名空间,其中部署正在使用 pvc 创建动态 pod,我需要分析这些 pvc。如果我有一些更新,我会用一些资源更新我的问题。
猜你喜欢
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 2021-09-18
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
  • 1970-01-01
相关资源
最近更新 更多