How to get all pods without jobs

12/9/2019

Is it possible to retrieve all pods without taking jobs?

kubectl get pods 
pod1                                      1/1     Running     1          28d
pod2                                      1/1     Running     1          28d
pods3                                     0/1     Completed   0          30m
pod4                                      0/1     Completed   0          30m

I don't want to see jobs, but only the other pod.
I don't want to fetch them basing on "Running State" because I would like to verify if all deployment I am trying to install are "deployed".
Basing on that I wanted to use the following command, but it is fetching also jobs I am trying to exclude:

kubectl wait --for=condition=Ready pods --all --timeout=600s 
-- Prisco
filter
kubectl
kubernetes
list

3 Answers

12/10/2019

If using a bit of scripting is OK...this one-liner should return the names of all "non-Jobs" pods in all namespaces:

for p in `kubectl get pods --all-namespaces -o=jsonpath="{range .items[*]}{.metadata.name}{';'}{.metadata.ownerReferences[?(@.kind != 'Job')].name}{'\n'}{end}"`; do v_owner_name=$(echo $p | cut -d';' -f2); if [ ! -z "$v_owner_name" ]; then v_pod_name=$(echo $p | cut -d';' -f1); echo $v_pod_name; fi; done

Using the above as a foundation, the following aims to return all "non-Jobs" pods in Ready status:

for p in `kubectl get pods --all-namespaces -o=jsonpath="{range .items[*]}{.metadata.name}{';'}{'Ready='}{.status.conditions[?(@.type == 'Ready')].status}{';'}{.metadata.ownerReferences[?(@.kind != 'Job')].name}{'\n'}{end}"`; do v_owner_name=$(echo $p | cut -d';' -f3); if [ ! -z "$v_owner_name" ]; then v_pod_name=$(echo $p | cut -d';' -f1,2); echo $v_pod_name; fi; done

This doc explains (arguably - to some degree) the JSONPath support in kubectl.

-- apisim
Source: StackOverflow

12/13/2019

If your question is -

I would like to verify if all deployment I am trying to install are "deployed"

Then this is not the right way of checking Pods status in Kubernetes. Please check the replicas and readyReplicas for your deployment.

kubectl get deployment <deployment-Name> -ojson | jq -r '.status | { desired: .replicas, ready: .readyReplicas }'

Output:-

{
  "desired": 1,
  "ready": 1
}

Here I am using jq (It's very handy) utility to parse the stuff

-- nagendra547
Source: StackOverflow

12/9/2019

Add a special label (e.g. kind=pod) to your job pods. Then use kubectl get pods -l kind!=pod.

-- Dávid Molnár
Source: StackOverflow