如何用Java的KubernetesClient检查Pod创建前的剩余配额及CPU限制
KubernetesClient 相关问题解决方案
1. 判断Namespace中是否仅剩100Mi资源空间
要判断Namespace的剩余资源空间(以内存或存储为例),需获取该Namespace下的ResourceQuota及其使用数据,计算已使用量与配额的差值:
import io.kubernetes.client.openapi.models.V1ResourceQuota; import io.kubernetes.client.openapi.models.V1ResourceQuotaStatus; import io.kubernetes.client.custom.Quantity; import java.util.Map; // 替换为目标Namespace名称 String namespace = "your-namespace"; long requiredRemaining = 100 * 1024 * 1024; // 100Mi对应的字节数 V1ResourceQuotaList quotaList = kubernetesClient.resourceQuotas().inNamespace(namespace).list(); for (V1ResourceQuota quota : quotaList.getItems()) { V1ResourceQuotaStatus status = quota.getStatus(); if (status == null || status.getUsed() == null || status.getHard() == null) { continue; } // 检查内存剩余(以requests.memory为例) checkRemainingResource(status.getUsed(), status.getHard(), "requests.memory", requiredRemaining); // 检查存储剩余(以requests.storage为例) checkRemainingResource(status.getUsed(), status.getHard(), "requests.storage", requiredRemaining); } // 工具方法:计算并判断指定资源的剩余量 private void checkRemainingResource(Map<String, String> usedMap, Map<String, String> hardMap, String resourceType, long required) { String used = usedMap.get(resourceType); String hard = hardMap.get(resourceType); if (used == null || hard == null) { return; } long usedBytes = Quantity.fromString(used).getNumber().longValue(); long hardBytes = Quantity.fromString(hard).getNumber().longValue(); long remainingBytes = hardBytes - usedBytes; if (remainingBytes <= required) { System.out.printf("该Namespace的%s剩余不足100Mi%n", resourceType); } }
2. 通过KubernetesClient获取limits.cpu值并检查CPU配额
创建Pod前检查CPU配额,需对比目标Namespace的CPU配额上限、已使用量与新Pod的CPU需求:
import io.kubernetes.client.custom.Quantity; String namespace = "your-namespace"; // 新Pod需要的CPU资源,示例为0.5核 String requiredCpu = "500m"; boolean hasEnoughQuota = false; V1ResourceQuotaList quotaList = kubernetesClient.resourceQuotas().inNamespace(namespace).list(); for (V1ResourceQuota quota : quotaList.getItems()) { V1ResourceQuotaStatus status = quota.getStatus(); if (status == null || status.getUsed() == null || status.getHard() == null) { continue; } String usedCpu = status.getUsed().get("limits.cpu"); String hardCpu = status.getHard().get("limits.cpu"); if (usedCpu == null || hardCpu == null) { continue; } double usedValue = Quantity.fromString(usedCpu).getNumber().doubleValue(); double hardValue = Quantity.fromString(hardCpu).getNumber().doubleValue(); double requiredValue = Quantity.fromString(requiredCpu).getNumber().doubleValue(); if (hardValue - usedValue >= requiredValue) { hasEnoughQuota = true; break; } } if (hasEnoughQuota) { System.out.println("CPU配额充足,可创建Pod"); } else { System.out.println("CPU配额不足,无法创建Pod"); }
3. 实现类似kubectl describe quota的功能,检查剩余100Mi配额
kubectl describe quota会展示配额的硬限制、已使用量和剩余量,可通过ResourceQuota的status字段实现该逻辑,并重点检查目标资源的剩余量:
import io.kubernetes.client.openapi.models.V1ResourceQuota; import io.kubernetes.client.openapi.models.V1ResourceQuotaStatus; import io.kubernetes.client.custom.Quantity; import java.util.Map; String namespace = "your-namespace"; String targetResource = "requests.memory"; // 要检查的资源类型 long requiredRemaining = 100 * 1024 * 1024; V1ResourceQuotaList quotaList = kubernetesClient.resourceQuotas().inNamespace(namespace).list(); for (V1ResourceQuota quota : quotaList.getItems()) { System.out.println("Quota名称: " + quota.getMetadata().getName()); System.out.println("---------------------------"); V1ResourceQuotaStatus status = quota.getStatus(); if (status == null || status.getHard() == null) { System.out.println("无配额数据"); continue; } Map<String, String> hardMap = status.getHard(); Map<String, String> usedMap = status.getUsed(); // 遍历所有资源,输出类似kubectl describe的格式 for (Map.Entry<String, String> hardEntry : hardMap.entrySet()) { String resource = hardEntry.getKey(); String hardValue = hardEntry.getValue(); String usedValue = usedMap != null ? usedMap.get(resource) : "0"; Quantity hardQty = Quantity.fromString(hardValue); Quantity usedQty = Quantity.fromString(usedValue); Quantity remainingQty = hardQty.subtract(usedQty); System.out.printf("%-20s 硬限制: %-10s 已使用: %-10s 剩余: %-10s%n", resource, hardValue, usedValue, remainingQty.toSuffixedString()); // 检查目标资源剩余是否达标 if (resource.equals(targetResource)) { long remainingBytes = remainingQty.getNumber().longValue(); if (remainingBytes >= requiredRemaining) { System.out.println("✅ 剩余资源满足100Mi要求"); } else { System.out.println("❌ 剩余资源不足100Mi"); } } } System.out.println(); }
注意事项
- 确保KubernetesClient已正确初始化,并拥有
resourcequotas资源的读取权限 - 资源量解析依赖官方
Quantity类,避免自行解析出现单位转换错误 - 若Namespace未配置ResourceQuota,需根据业务需求补充无配额场景的处理逻辑
内容的提问来源于stack exchange,提问作者Tanya Bhandari
相关产品推荐
相关产品推荐

