return 以整数表示的持久卷 (pv) 容量,而不是 Gi、Mi、Ki、G、M、K 等
return persistent volume (pv) capacity in integer instead of Gi, Mi, Ki, G, M, K etc
我想计算集群中持久卷 (PV) 分配的总字节数。使用以下内容:
$ kubectl get pv -A -o json
我可以获得一个包含所有集群的 PV 的 JSON 列表,对于 items[]
列表中的每个 PV,可以读取 spec.capacity.storage
键来访问必要的信息。
请参见下面的示例:
{
"apiVersion": "v1",
"kind": "PersistentVolume",
"spec": {
"accessModes": [
"ReadWriteOnce"
],
"capacity": {
"storage": "500Gi"
},
"claimRef": {
"apiVersion": "v1",
"kind": "PersistentVolumeClaim",
"name": "s3-storage-minio",
"namespace": "default",
"resourceVersion": "515932",
},
"persistentVolumeReclaimPolicy": "Delete",
"volumeMode": "Filesystem",
},
"status": {
"phase": "Bound"
}
},
但是,返回值可以用不同的后缀表示(存储为普通整数或使用以下后缀之一的定点数:E、P、T、G、M、K。或者类似地,二次幂等价物:Ei、Pi、Ti、Gi、Mi、Ki)。
是否有使用 kubectl 以整数格式(或任何其他格式但在所有 PV 之间保持一致)请求容量的巧妙方法?
否则,在Bash中将不同的后缀转换为共同的后缀看起来不是很简单。
在此先感谢您的帮助。
我还没有找到一种方法来仅使用 kubectl
.
来转换 .spec.capacity.storage
中的值
我已经设法用 Python 创建了一个代码,它是 Kubernetes 库来提取数据并计算所有使用的 PV
的大小。请将此代码视为示例而不是生产就绪:
from kubernetes import client, config
import re
config.load_kube_config() # use .kube/config
v1 = client.CoreV1Api()
multiplier_dict = {"k": 1000, "Ki": 1024, "M": 1000000, "Mi": 1048576 , "G": 1000000000, "Gi": 1073741824} # and so on ...
size = 0
# for i in v1.list_persistent_volume_claim_for_all_namespaces(watch=False).items: # PVC
for i in v1.list_persistent_volume(watch=False).items: # PV
x = i.spec.capacity["storage"] # PV
# x = i.spec.resources.requests["storage"] # PVC
y = re.findall(r'[A-Za-z]+|\d+', x)
print(y)
# try used if no suffix (like Mi) is used
try:
if y[1] in multiplier_dict:
size += multiplier_dict.get(y[1]) * int(y[0])
except IndexError:
size += int(y[0])
print("The size in bytes of all PV's is: " + str(size))
以集群为例,该集群具有以下 PV
:
$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-6b5236ec-547f-4f96-8448-e3dbe01c9039 500Mi RWO Delete Bound default/pvc-four hostpath 4m13s
pvc-86d178bc-1673-44e0-9a89-2efb14a1d22c 512M RWO Delete Bound default/pvc-three hostpath 4m15s
pvc-89b64f93-6bf4-4987-bdda-0356d19d6f59 1G RWO Delete Bound default/pvc-one hostpath 4m15s
pvc-a3455e77-0db0-4cab-99c9-c72721a65632 10Ki RWO Delete Bound default/pvc-six hostpath 4m14s
pvc-b47f92ef-f627-4391-943f-efa4241d0811 10k RWO Delete Bound default/pvc-five hostpath 4m13s
pvc-c3e13d78-9047-4899-99e7-0b2667ce4698 1Gi RWO Delete Bound default/pvc-two hostpath 4m15s
pvc-c57fe2b0-013a-412b-bca9-05050990766a 10 RWO Delete Bound default/pvc-seven hostpath 113s
代码将产生以下输出:
['500', 'Mi']
['512', 'M']
['1', 'G']
['10', 'Ki']
['10', 'k']
['1', 'Gi']
['10']
The size in bytes of all PV's is: 3110050074
添加到整个答案请记住,PVC
和实际 PV
大小的请求可能存在差异。请在这方面参考您选择的存储文档。
pvc.yaml
:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100M
部分$ kubectl get pvc -o yaml
输出:
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100M # <-- REQUEST
<-- REDACTED -->
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi # <-- SIZE OF PV
phase: Bound
其他资源:
我想计算集群中持久卷 (PV) 分配的总字节数。使用以下内容:
$ kubectl get pv -A -o json
我可以获得一个包含所有集群的 PV 的 JSON 列表,对于 items[]
列表中的每个 PV,可以读取 spec.capacity.storage
键来访问必要的信息。
请参见下面的示例:
{
"apiVersion": "v1",
"kind": "PersistentVolume",
"spec": {
"accessModes": [
"ReadWriteOnce"
],
"capacity": {
"storage": "500Gi"
},
"claimRef": {
"apiVersion": "v1",
"kind": "PersistentVolumeClaim",
"name": "s3-storage-minio",
"namespace": "default",
"resourceVersion": "515932",
},
"persistentVolumeReclaimPolicy": "Delete",
"volumeMode": "Filesystem",
},
"status": {
"phase": "Bound"
}
},
但是,返回值可以用不同的后缀表示(存储为普通整数或使用以下后缀之一的定点数:E、P、T、G、M、K。或者类似地,二次幂等价物:Ei、Pi、Ti、Gi、Mi、Ki)。
是否有使用 kubectl 以整数格式(或任何其他格式但在所有 PV 之间保持一致)请求容量的巧妙方法?
否则,在Bash中将不同的后缀转换为共同的后缀看起来不是很简单。
在此先感谢您的帮助。
我还没有找到一种方法来仅使用 kubectl
.
.spec.capacity.storage
中的值
我已经设法用 Python 创建了一个代码,它是 Kubernetes 库来提取数据并计算所有使用的 PV
的大小。请将此代码视为示例而不是生产就绪:
from kubernetes import client, config
import re
config.load_kube_config() # use .kube/config
v1 = client.CoreV1Api()
multiplier_dict = {"k": 1000, "Ki": 1024, "M": 1000000, "Mi": 1048576 , "G": 1000000000, "Gi": 1073741824} # and so on ...
size = 0
# for i in v1.list_persistent_volume_claim_for_all_namespaces(watch=False).items: # PVC
for i in v1.list_persistent_volume(watch=False).items: # PV
x = i.spec.capacity["storage"] # PV
# x = i.spec.resources.requests["storage"] # PVC
y = re.findall(r'[A-Za-z]+|\d+', x)
print(y)
# try used if no suffix (like Mi) is used
try:
if y[1] in multiplier_dict:
size += multiplier_dict.get(y[1]) * int(y[0])
except IndexError:
size += int(y[0])
print("The size in bytes of all PV's is: " + str(size))
以集群为例,该集群具有以下 PV
:
$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-6b5236ec-547f-4f96-8448-e3dbe01c9039 500Mi RWO Delete Bound default/pvc-four hostpath 4m13s
pvc-86d178bc-1673-44e0-9a89-2efb14a1d22c 512M RWO Delete Bound default/pvc-three hostpath 4m15s
pvc-89b64f93-6bf4-4987-bdda-0356d19d6f59 1G RWO Delete Bound default/pvc-one hostpath 4m15s
pvc-a3455e77-0db0-4cab-99c9-c72721a65632 10Ki RWO Delete Bound default/pvc-six hostpath 4m14s
pvc-b47f92ef-f627-4391-943f-efa4241d0811 10k RWO Delete Bound default/pvc-five hostpath 4m13s
pvc-c3e13d78-9047-4899-99e7-0b2667ce4698 1Gi RWO Delete Bound default/pvc-two hostpath 4m15s
pvc-c57fe2b0-013a-412b-bca9-05050990766a 10 RWO Delete Bound default/pvc-seven hostpath 113s
代码将产生以下输出:
['500', 'Mi']
['512', 'M']
['1', 'G']
['10', 'Ki']
['10', 'k']
['1', 'Gi']
['10']
The size in bytes of all PV's is: 3110050074
添加到整个答案请记住,PVC
和实际 PV
大小的请求可能存在差异。请在这方面参考您选择的存储文档。
pvc.yaml
:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100M
部分$ kubectl get pvc -o yaml
输出:
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100M # <-- REQUEST
<-- REDACTED -->
status:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi # <-- SIZE OF PV
phase: Bound
其他资源: