繁体   English   中英

如何使用fabric8 kubernetes Java和Scala客户端API设置容器上的资源要求

[英]How to set resource requirements on a container with the fabric8 kubernetes Java & Scala client API

fabric8 kubernetes Java和Scala客户端API非常适合与kubernetes(或OpenShift)进行通信,但是其文档非常稀疏。 向在kubernetes容器中运行的容器添加资源需求的代码示例是什么?

如果您正在使用用于Java和Scala的fabric8 kubernetes-client API,则以下代码段演示了如何向在pod中运行的容器添加资源要求。 这段代码是从Scala复制而来的,但是Java代码非常相似:

// other fabric8 imports not included; just focusing on resource
// requirements logic in this example
import io.fabric8.kubernetes.api.model.Quantity
import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder

// Use Java style Map (as opposed to Scala's Map class)
val reqMap: java.util.Map[String, Quantity] =
  new java.util.HashMap[String, Quantity]()

// add CPU and memory requirements to the map
reqMap.put("cpu", new Quantity("1"))
reqMap.put("memory", new Quantity("1500Mi"))

// Build a ResourceRequirements object from the map
val reqs = new ResourceRequirementsBuilder()
  .withRequests(reqMap)
  .build()

// pass the ResourceRequirements object to the container spec
val pod = new PodBuilder()
  .withNewMetadata()
  .withName(podName)
  .endMetadata()
  .withNewSpec()
  .withRestartPolicy("OnFailure")
  .addNewContainer()
  .withName(containerName)
  .withImage(containerImage)
  .withImagePullPolicy("Always")
  .withResources(reqs)            // <-- resource reqs here
  .withCommand(commandName)
  .withArgs(commandArguments)
  .endContainer()
  .endSpec()
  .build()

// create the new pod with resource requirements via the 
// fabric8 kube client:
client.pods().inNamespace(nameSpace).withName(podName).create(pod)
    if(EmptyUtil.isNotNull(template.getComputeRequest()) ||
            EmptyUtil.isNotNull(template.getComputeLimit())) {
        containerBuilder.withResources(buildResources(template.getComputeRequest(), template.getComputeLimit()));
    }


private ResourceRequirements buildResources(InstanceType request, InstanceType limit){
    _logger.info("Building computeResources");
    ResourceRequirementsBuilder requirementsBuilder = new ResourceRequirementsBuilder(isValidationEnabled);

    if(EmptyUtil.isNotNull(request)){
        requirementsBuilder.withRequests(K8ComputeResourceUtil.buildCompute(request));
    }

    if(EmptyUtil.isNotNull(limit)){
        requirementsBuilder.withLimits(K8ComputeResourceUtil.buildCompute(limit));
    }

    return requirementsBuilder.build();
}



public static Map<String, Quantity> buildCompute(InstanceType compute){

    Map<String, Quantity> computeResourceMap = new HashMap<>();

    if(EmptyUtil.isNotNull(compute.getCpu())) {
        computeResourceMap.putAll(buildCpu(compute.getCpu()));
    }

    if(EmptyUtil.isNotNull(compute.getMemory())) {
        computeResourceMap.putAll(buildMemory(compute.getMemory()));
    }

    return computeResourceMap;
}

private static Map<String, Quantity> buildCpu(Float cpu){
    Map<String, Quantity> cpuMap = new HashMap<>();
    try {
        Quantity cpuQuantity = new Quantity();
        if (EmptyUtil.isNotNull(cpu)) {
            cpuQuantity.setAmount(String.valueOf(cpu));
            cpuMap.put(K8Constants.CPU, cpuQuantity);
        }
    } catch (NumberFormatException nfe){
        _logger.error("Failed to convert cpu '{}'", cpu, nfe);
    }

    return cpuMap;
}

private static Map<String, Quantity> buildMemory(Integer memory){
    Map<String, Quantity> cpuMap = new HashMap<>();
    try {
        Quantity cpu = new Quantity();
        if (EmptyUtil.isNotNull(memory)) {
            cpu.setAmount(String.valueOf(memory));
            cpuMap.put(K8Constants.MEMORY, cpu);
        }
    } catch (NumberFormatException nfe){
        _logger.error("Failed to convert memory '{}'", memory, nfe);
    }

    return cpuMap;
}

在这里,我有一些构建器来构建cpu和内存。 这仅仅是为了了解流程。 您可以使用整数或字符串形式指定cpu /内存值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM