我是库伯内斯的新手,我想知道是否有更好的属性来准确描述Pod的状态

Kubernetes version: Kubernetes v1.27.3
java client maven:
<dependency>
    <groupId>io.kubernetes</groupId>
    <artifactId>client-java</artifactId>
    <version>18.0.1</version>
</dependency>

我正在具有两个 node 的Kubernetes集群中进行测试.我正在关闭从 node 上的Kubelet服务,以模拟从 node 的连接中断,从而允许将从 node 的Pod自动调度到主 node

当我使用以下命令时:

kubectl get po -n my-namespace -o wide

我们可以看到

nginx1-74499f547c-gbdzf                     1/1     Running       0              6h10m   10.244.0.55   kylin-master     <none>           <none>
nginx1-74499f547c-xndkm                     1/1     Terminating   0              8h      10.244.2.24   kylin-worker02   <none>           <none>

显然,nginx1-74499f547c-xndkm的状态是Terminating

但是,当我使用以下代码检索Pod并遍历它以找到同名的Pod时

CoreV1Api coreV1Api = new CoreV1Api();
V1PodList v1PodList;
try{
    v1PodList = coreV1Api.listNamespacedPod(tenxOpenApiConfig.getTeamSpace(), null, null, null, info, null, null, null, null, null, null);
    for (V1Pod item : v1PodList.getItems()) {
       // .....
    }
} 
// ......

enter image description here

豆荚的名字是nginx1-74499f547c-xndkm,但item.status.phase="Running".

我不明白为什么会发生这样的事情,这是个漏洞吗?尽管如此,status.phase并不适合表示Pod状态.

期待您的帮助.

推荐答案

TL;dr
Kubernetes Pods and containers doesn't have Terminating as status, and kubectl prints the status based on multiple fields in the Pod object to be more concise (not just pod.status.phase, like pod.metadata.deletionTimestamp). Let me explain below in more details.

More details
When a pod is being deleted for any reason (e.g. Node failure or manual deletion), it doesn't happen immediately as it could be disruptive to the application running inside, so kubernetes gives a TERM(aka. SIGTERM) signal to the containers inside the pod first to give it some time to terminate gracefully. After some time (defined in pod.spec.terminationGracePeriodSeconds, defaults to 30) if the containers aren't terminated yet, it kills them. During this period, containers are actually running, so that's why kubernetes reports the containers and the pods as running. However, when the pod is being deleted, kubernetes sets pod.metadata.deletionTimestamp to the time the delete was issued.
Kubectl is smart enough to detect this and show the status of Pod as Terminating when it sees this.

Documentation
There is a nice documentation about this here.

让我重点介绍一个有用的段落:

注意:删除Pod时,会通过一些kubectl命令将其显示为终止.此终止状态不属于Pod阶段.Pod会被授予一个优雅终止的期限,默认为30秒.您可以使用标志--force强制终止Pod.

Additional advice
You can see the pod mostly* as you see it in the code by using -o yaml. Example:

kubectl get pod -n my-namespace -o yaml
  • 我之所以说*主要是因为kubectl在查看任何对象(包括Pod)时省略了.metadata.managedFields,因为它们可能对您没有用处.如果您想让kubectl显示它,您可以在kubectl命令中添加--show-managed-fields.我提到这一点只是为了补充答案,但如果你正在启动Kubernetes,我建议忽略它们.有关于它们的文档here.

希望这能有所帮助:)

Java相关问答推荐

如何在Spring Boot中创建500错误的响应正文?

我可以在regex中的字符类中放置断言吗?

Java自定义ThreadPool—暂停任务提交并取消当前排队任务

如何在Java中声明未使用的变量?

RESTful框架类字段是安全的还是不安全的

对于几乎不涉及逻辑的请求,您是否应该使用命令模式?

Java中实现的归并排序算法给出ArrayIndexOutOfBound异常

将响应转换为带值的键

格式中的特定回录键-值对

带有Health Check的Spring Boot BuildPack打破了本机映像构建过程

Quarkus:运行时出现EnumConstantNotPresentException

如何在字节数组中反转UTF-8编码?

在打开搜索结果时,如何让Eclipse打开整个文件?

Java类型推断:为什么要编译它?

控制器建议异常处理

将基于实例编号的对象列表拆分为新的对象列表

当我将JTextField的getText函数与相等的String进行比较时;t返回true

如何通过gradle命令行从build.gradle获得Java targetCompatibility

Spring Mapstruct如何获取Lazy初始化实体字段的信息?

类型安全:从 JSONArray 到 ArrayList> 的未经判断的转换