我正在使用 Kubernetes-client 的 Java 客户端,在 Kubernetes 集群上创建 Deployments。以下是代码:
Deployment deployment = new DeploymentBuilder()
.withNewMetadata()
.withName("first-deployment")
.endMetadata()
.withNewSpec()
.withReplicas(3)
.withNewTemplate()
.withNewMetadata()
.addToLabels(namespaceID, "hello-world-example")
.endMetadata()
.withNewSpec()
.addNewContainer()
.withName("nginx-one")
.withImage("nginx")
.addNewPort()
.withContainerPort(80)
.endPort()
.withResources(resourceRequirements)
.endContainer()
.endSpec()
.endTemplate()
.endSpec()
.build();
deployment = client.extensions().deployments().inNamespace(namespace).create(deployment);
我增加了3分钟的等待时间,然后测试pod的状态
PodList podList = client.pods().withLabel(namespaceID, "hello-world-example").list();
System.out.println("Number of pods " + podList.getItems().size());
for (Pod pod : podList.getItems()) {
System.out.println("Name " + pod.getMetadata().getName()
+ " Status " + pod.getStatus().getPhase()
+ " Reason " + pod.getStatus().getReason()
+ " Containers " + pod.getSpec().getContainers().get(0).getResources().getLimits());
}
这将返回以下状态
Name first-deployment-2418943216-9915m Status Pending Reason null Containers null
Name first-deployment-2418943216-fnk21 Status Pending Reason null Containers null
Name first-deployment-2418943216-zb5hr Status Pending Reason null Containers null
然而,如果我从命令行输入kubectl get pods --all-namespaces
,它返回pod状态为running。我是否使用了正确的API?我错过了什么吗?
kubectl get pods --all-namespaces
的结果是否返回与您的 API 调用相同的 Pod 名称,只是状态、原因和容器引用不同来确定。 - heckj