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 <pod_name> <container_name> 可以轻松阅读,无需使用kubectl exec 并附加到Pod:
$ kubectl logs nginx-deployment-56bb5c87f6-dqs5h busybox
20.0K /data
20.0K /data
20.0K /data
您可以通过kubectl apply -f申请
我想它也可以更有效地用于配置某种磁盘使用监控。