Answer a question

Is there a way to check whether a pod status is in the completed state? I have a Pod that I only wanted to use once (where init containers didn't quite serve my purpose) and want to write a check to wait for Completed status.

I am able to get it for Running, Pending, but not for Completed.

Running:

[user@sandbox gcp_kubernetes_installation]$ kubectl get pods --field-selector=status.phase=Running -n mynamespace
NAME                                READY   STATUS    RESTARTS   AGE
mssql-deployment-795dfcf9f7-l2b44   1/1     Running   0          6m
data-load-pod    1/1     Running   0          5m

Pending:

[user@sandbox gcp_kubernetes_installation]$ kubectl get pods --field-selector=status.phase=Pending -n mynamespace
NAME                    READY   STATUS    RESTARTS   AGE
app-deployment-0   0/1     Pending   0          5m

Completed:

[user@sandbox gcp_kubernetes_installation]$ kubectl get pod -n namespace
NAME                                READY   STATUS      RESTARTS   AGE
mssql-deployment-795dfcf9f7-l2b44   1/1     Running     0          11m
data-load-data-load-pod    0/1     Completed   0          10m
app-deployment-0               0/1     Pending     0          10m
[user@sandbox gcp_kubernetes_installation]$ kubectl get pods --field-selector=status.phase=Completed -n namespace
No resources found.

I believe there may be a bug in the field-selector, but just wondering if there are any fixes or details on a workaround.

Answers

The correct status.phase for completed pods is Succeeded.

So, to filter only completed pods, you should use this:
kubectl get pod --field-selector=status.phase=Succeeded

Although, the use of bare pods is not recommended. Consider using a Job Controller:

A Job creates one or more Pods and ensures that a specified number of them successfully terminate. As pods successfully complete, the Job tracks the successful completions.

You can check job conditions and wait for them with this:
kubectl wait --for=condition=complete job/myjob

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐