When I run:
kubectl get pods --field-selector=status.phase=Running
I see:
NAME READY STATUS RESTARTS AGE
k8s-fbd7b 2/2 Running 0 5m5s
testm-45gfg 1/2 Error 0 22h
I don't understand why this command gives me pod that are in Error status?
According to K8S api, there is no such thing STATUS=Error.
How can I get only the pods that are in this Error status?
When I run:
kubectl get pods --field-selector=status.phase=Failed
It tells me that there are no pods in that status.
Using the kubectl get pods --field-selector=status.phase=Failed command you can display all Pods in the Failed phase.
Failed means that all containers in the Pod have terminated, and at least one container has terminated in failure (see: Pod phase):
Failed - All containers in the Pod have terminated, and at least one container has terminated in failure. That is, the container either exited with non-zero status or was terminated by the system.
In your example, both Pods are in the Running phase because at least one container is still running in each of these Pods.:
Running - The Pod has been bound to a node, and all of the containers have been created. At least one container is still running, or is in the process of starting or restarting.
You can check the current phase of Pods using the following command:
$ kubectl get pod -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'
Let's check how this command works:
$ kubectl get pods
NAME READY STATUS
app-1 1/2 Error
app-2 0/1 Error
$ kubectl get pod -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'
app-1 Running
app-2 Failed
As you can see, only the app-2 Pod is in the Failed phase. There is still one container running in the app-1 Pod, so this Pod is in the Running phase.
To list all pods with the Error status, you can simply use:
$ kubectl get pods -A | grep Error
default app-1 1/2 Error
default app-2 0/1 Error
Additionally, it's worth mentioning that you can check the state of all containers in Pods:
$ kubectl get pod -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.containerStatuses[*].state}{"\n"}{end}'
app-1 {"terminated":{"containerID":"containerd://f208e2a1ff08c5ce2acf3a33da05603c1947107e398d2f5fbf6f35d8b273ac71","exitCode":2,"finishedAt":"2021-08-11T14:07:21Z","reason":"Error","startedAt":"2021-08-11T14:07:21Z"}} {"running":{"startedAt":"2021-08-11T14:07:21Z"}}
app-2 {"terminated":{"containerID":"containerd://7a66cbbf73985efaaf348ec2f7a14d8e5bf22f891bd655c4b64692005eb0439b","exitCode":2,"finishedAt":"2021-08-11T14:08:50Z","reason":"Error","startedAt":"2021-08-11T14:08:50Z"}}
You can simply grep the Error pods using the
kubectl get pods --all-namespces | grep Error
Remove all error pods from the cluster
kubectl delete pod `kubectl get pods --namespace <yournamespace> | awk '$3 == "Error" {print $1}'` --namespace <yournamespace>
Mostly Pod failures return explicit error states that can be observed in the status field
Error :
Your pod is crashed, it was able to schedule on node successfully but crashed after that. To debug it more you can use different methods or commands
kubectl describe pod <Pod name > -n <Namespace>
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-pod-replication-controller/#my-pod-is-crashing-or-otherwise-unhealthy
Here is an overkill go-template based attempt:
kubectl get pods -o go-template='{{range $index, $element := .items}}{{range .status.containerStatuses}}{{range .state }}{{if .reason }}{{if (eq .reason "Error") }}{{$element.metadata.name}} {{$element.metadata.namespace}}{{"\n"}}{{end}}{{end}}{{end}}{{end}}{{end}}'
job1-stn45 default
My pod status:
k get pod
NAME READY STATUS RESTARTS AGE
foo 1/1 Running 1 2d11h
nginx-0 1/1 Running 3 5d10h
nginx-2 1/1 Running 3 5d10h
nginx-1 1/1 Running 3 5d10h
job1-stn45 0/1 Error 0 113m
update-test-27145740-82z7s 0/1 ImagePullBackOff 0 96m
update-test-27145500-7f2l9 0/1 ImagePullBackOff 0 5h36m
Related
when I get all the pods kubectl get pod
and I get:
NAME READY STATUS RESTARTS AGE
balanced-7bbf659597-sk478 1/1 Running 0 5d13h
hello-minikube-68ff6fd96-xxbx8 1/1 Running 0 5d13h
mongo-express-5bf4b56f47-5s2b6 1/1 Running 8 (13h ago) 14h
mongodb-deployment-844789cd64-fjlgk 0/1 CrashLoopBackOff 101 (4m30s ago) 13h
but then when I try to get the logs of the failed pod kubectl logs mongodb-deployment-844789cd64-fjlgk nothing happens, and then when I describe the pod: kubectl describe mongodb-deployment-844789cd64-fjlgk I get this error:
error: the server doesn't have a resource type "mongodb-deployment-844789cd64-fjlgk"
Your pod isn't available when you try see the log, you must access to the previous log file using the following command
kubectl logs mongodb-deployment-844789cd64-fjlgk --previous
Using previous parameter kubectl will dump logs for a previous instantiation of your container
I can list pods in my prod namespace
kubectl get pods -n prod
NAME READY STATUS RESTARTS AGE
curl-pod 1/1 Running 1 (32m ago) 38m
web 1/1 Running 1 (33m ago) 38m
I got error
kubectl describe pods curl-pod
Error from server (NotFound): pods "curl-pod" not found
Get events show
Normal Scheduled pod/curl-pod Successfully assigned prod/curl-pod to minikube
Why?
kubernetes manages by namespace, so you must specify namespace otherwise kubernetes will use namespace default.
So, you must type:
kubectl describe pod/curl-pod -n prod
I started with Kubernetes tutorial and after intializeing the pod network
kubectl apply -f https://docs.projectcalico.org/v3.0/getting-started/kubernetes/installation/hosted/kubeadm/1.7/calico.yaml
I discovered that coredns is pending but rest of the services were running.
# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-74ff55c5b-gzn5l 0/1 Pending 0 19m
kube-system coredns-74ff55c5b-k2h5m 0/1 Pending 0 19m
the I tried to look at the logs:
# kubectl logs coredns-74ff55c5b-gzn5l
Error from server (NotFound): pods "coredns-74ff55c5b-gzn5l" not found
# kubectl logs coredns-74ff55c5b-k2h5m
Error from server (NotFound): pods "coredns-74ff55c5b-k2h5m" not found
I am trying to autoscale my StatefulSet on Kubernetes. In order to do so, I need to get the current number of pods.
When dealing with deployments:
kubectl describe deployments [deployment-name] | grep desired | awk '{print $2}' | head -n1
This outputs a number, which is the amount of current deployments.
However, when you run
kubectl describe statefulsets
We don't get back as much information. Any idea how I can get the current number of replicas of a stateful set?
kubectl get sts web -n default -o=jsonpath='{.status.replicas}'
This also works with .status.readyReplicas and .status.currentReplicas
From github.com/kubernetes:
// replicas is the number of Pods created by the StatefulSet controller.
Replicas int32
// readyReplicas is the number of Pods created by the StatefulSet controller that have a Ready Condition.
ReadyReplicas int32
// currentReplicas is the number of Pods created by the StatefulSet controller from the StatefulSet version
// indicated by
currentRevision.
CurrentReplicas int32
you should be running one of the below command
master $ kubectl get statefulsets
NAME DESIRED CURRENT AGE
web 4 4 2m
master $
master $ kubectl get sts
NAME DESIRED CURRENT AGE
web 4 4 2m
number of running pods
---------------------
master $ kubectl describe sts web|grep Status
Pods Status: 4 Running / 0 Waiting / 0 Succeeded / 0 Failed
another way
------------
master $ kubectl get sts --show-labels
NAME DESIRED CURRENT AGE LABELS
web 4 4 33s app=nginx
master $ kubectl get po -l app=nginx
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 56s
web-1 1/1 Running 0 55s
web-2 1/1 Running 0 30s
web-3 1/1 Running 0 29s
master $ kubectl get po -l app=nginx --no-headers
web-0 1/1 Running 0 2m
web-1 1/1 Running 0 2m
web-2 1/1 Running 0 1m
web-3 1/1 Running 0 1m
master $
master $
master $ kubectl get po -l app=nginx --no-headers | wc -l
4
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.
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