我用fabric8 kubernetes Java客户端API编写了一个示例,以设置容器上的GPU资源要求。我遇到以下运行时错误:
spec.containers[0].resources.requests[gpu]: Invalid value: "gpu": must be a standard resource type or fully qualified,
spec.containers[0].resources.requests[gpu]: Invalid value: "gpu": must be a standard resource for containers.
fabric8 jar的版本是4.3.0(最新)。到目前为止,fabric8似乎还不支持gpu资源要求,因为我删除了“ addToRequests(“ gpu”,newquantity(“ 1”))“这一行,它可以正常工作。
然后如何在Java / Scala应用程序中启用GPU资源要求?
该示例的整个源代码如下:
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.exam.docker.kubernetes.examples;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.client.*;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class PodResExamples {
private static final Logger logger = LoggerFactory.getLogger(PodResExamples.class);
public static void main(String[] args) {
String master = "http://127.0.0.1:8080/";
if (args.length == 1) {
master = args[0];
}
String ns = "thisisatest";
String serviceName = "cuda-vector-add-"+ UUID.randomUUID();
Config config = new ConfigBuilder().withMasterUrl(master).build();
try (KubernetesClient client = new DefaultKubernetesClient(config)) {
try {
if(client.namespaces().withName(ns).get() == null) {
log("Create namespace:", client.namespaces().create(new NamespaceBuilder().withNewMetadata().withName(ns).endMetadata().build()));
}
String imageStr = "k8s.gcr.io/cuda-vector-add:v0.1";
String cmd = "";
final ResourceRequirements resources = new ResourceRequirementsBuilder()
.addToRequests("cpu", new Quantity("2"))
.addToRequests("memory", new Quantity("10Gi"))
.addToRequests("gpu", new Quantity("1"))
.build();
Container container = new ContainerBuilder().withName(serviceName)
.withImage(imageStr).withImagePullPolicy("IfNotPresent")
.withArgs(cmd)
.withResources(resources)
.build();
Pod createdPod = client.pods().inNamespace(ns).createNew()
.withNewMetadata()
.withName(serviceName)
.addToLabels("podres", "cuda-vector")
.endMetadata()
.withNewSpec()
.addToContainers(container)
.withRestartPolicy("Never")
.endSpec().done();
log("Created pod cuda-vector-add:", createdPod);
final CountDownLatch watchLatch = new CountDownLatch(1);
try (final Watch ignored = client.pods().inNamespace(ns).withLabel("podres").watch(new Watcher<Pod>() {
@Override
public void eventReceived(final Action action, Pod pod) {
if (pod.getStatus().getPhase().equals("Succeeded")) {
logger.info("Pod cuda-vector is completed!");
logger.info(client.pods().inNamespace(ns).withName(pod.getMetadata().getName()).getLog());
watchLatch.countDown();
} else if (pod.getStatus().getPhase().equals("Pending")) {
logger.info("Pod cuda-vector is Pending!");
}
}
@Override
public void onClose(final KubernetesClientException e) {
logger.info("Cleaning up pod.");
}
})) {
watchLatch.await(30, TimeUnit.SECONDS);
} catch (final KubernetesClientException | InterruptedException e) {
e.printStackTrace();
logger.error("Could not watch pod", e);
}
} catch (KubernetesClientException e) {
logger.error(e.getMessage(), e);
} finally {
log("Pod cuda-vector log: \n", client.pods().inNamespace(ns).withName(serviceName).getLog());
client.namespaces().withName(ns).delete();
}
}
}
private static void log(String action, Object obj) {
logger.info("{}: {}", action, obj);
}
private static void log(String action) {
logger.info(action);
}
}
答案 0 :(得分:1)
请参考Kubernetes Docs,您可以尝试使用nvidia.com/gpu
而不是gpu
:
apiVersion: v1
kind: Pod
metadata:
name: cuda-vector-add
spec:
containers:
- name: cuda-vector-add
image: "k8s.gcr.io/cuda-vector-add:v0.1"
resources:
limits:
nvidia.com/gpu: 1 # requesting 1 GPU
如果您的应用程序改用AMD GPU,请尝试amd.com/gpu
重要说明:除非您将限制设置为等于请求,否则您无法设置GPU请求。