我试图从一个PVC中获得space used,该PVC是通过fabric8io的java客户端安装到一个吊舱上的。到目前为止,我还没有找到一个方法来得到这个指标。因此,我如何显示度量标准的问题如下:
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={})
发布于 2021-10-27 15:08:26
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并附加到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
通过对答复下的评论作出答复来扩展答复:
如果我们不讨论java和它的代码(我不擅长),我们将讨论Kubernetes的决定,那么我们可以部署一个单独的pod,让所有的PVC都在一个地方使用度量。
您可以创建一个运行在nginx容器旁边的sidecar,它运行那个简单的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应用它
我想它也可以更有效地用于配置某种类型的磁盘使用情况监控。
https://stackoverflow.com/questions/69720609
复制相似问题