kubectl get pods --all-namespaces provides the list of all pods. The column RESTARTS shows the number of restarts that a pod has had. How to get the list of all the pods that have had at least one restart? Thanks
kubectl get pods --all-namespaces | awk '$5>0'
or simply just
kubectl get po -A | awk '$5>0'
Use awk to print if column 5 (RESTARTS) > 0
or with the use of an alias
alias k='kubectl'
k get po -A | awk '$5>0'
I'm trying to delete Kubernetes evicted pods which are older than 30 days from all namespaces, but the command throws an error :
Error from server(NotFound): pods (xxxxxxxxxxxxx) not found
Command:
kubectl delete pod $(kubectl get pods --all-namespaces | grep Evicted | sed 's#d$##' | awk '$6 > 30 {print $2}')"
Any ideas?
i think it's due to when you are listing down the pod you are listing all pod and deleting all those
but command
kubectl delete pod not getting the namespace from which to delete the specific pod
kubectl delete pod $(kubectl get pods --all-namespaces | grep Evicted | awk '$6 > 30 {print $2}') -n $(kubectl get pods --all-namespaces | grep Evicted | awk '$6 > 30 {print $1}')
maybe you try something like or simplify it better way.
This assumes the GNU coreutils version of date and a bourne-like shell:
One-liner:
onemonthago=$(date -u -d '-1 month' +"%FT%TZ"); kubectl get pod --field-selector='status.phase=Failed' -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name,STATUS:.status.reason,STARTTIME:.status.startTime -A | while read ns pod status starttime; do if [ "$status" = "Evicted" ]; then if [ "$starttime" \> "$onemonthago" ]; then kubectl -n $ns delete pod $pod --wait=false; fi; fi; done
More readable formatting:
onemonthago=$(date -u -d '-1 month' +"%FT%TZ");
kubectl get pod --field-selector='status.phase=Failed' -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name,STATUS:.status.reason,STARTTIME:.status.startTime -A | while read ns pod status starttime; do
if [ "$status" = "Evicted" ]; then
if [ "$starttime" \> "$onemonthago" ]; then
kubectl -n $ns delete pod $pod --wait=false;
fi;
fi;
done
The details:
onemonthago=$(date -u -d '-1 month' +"%FT%TZ"); gets the cutoff time in the same format as what kubectl returns and stores it in a shell variable.
The kubectl then gets all the failed pods and outputs the columns that we need for further steps.
It then gets piped to a while loop that reads the fields into shell variables.
The if statements then check whether the pod was evicted or is failed for another reason (The status.reason field is not supported for --field-selector) and that it is older than a month. If it matches that criteria, it deletes it.
(It is likely possible to optimise it to gather a list of all pods in a namespace and pass that to the minimum number of kubectls using xargs in one go) (Both checks can be combined, either in the shell or in AWK as well) (Replacing the entire while with an elaborate AWK script would likely yield the best perfomance (collecting a list of pos to delete per namespace and running / outputting the delete command from the END pattern)
I'm looking for a command like "gcloud config get-value project" that retrieves a project's name, but for a pod (it can retrieve any pod name that is running). I know you can get multiple pods with "kubectl get pods", but I would just like one pod name as the result.
I'm having to do this all the time:
kubectl get pods # add one of the pod names in next line
kubectl logs -f some-pod-frontend-3931629792-g589c some-app
I'm thinking along the lines of "gcloud config get-value pod". Is there a command to do that correctly?
There are many ways, here are some examples of solutions:
kubectl get pods -o name --no-headers=true
kubectl get pods -o=name --all-namespaces | grep kube-proxy
kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'
For additional reading, please take a look to these links:
kubernetes list all running pods name
Kubernetes list all container id
https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/
You can use the grep command to filter any output on stdout. So to get pods matching a specified pattern you can use a command like this:
> kubectl get pods --all-namespaces|grep grafana
Output:
monitoring kube-prometheus-grafana-57d5b4d79f-smkz6 2/2 Running 0 1h
To only output the pod name, you can use the awk command with a parameter of '{print $2}', which displays the second column of the previous output:
kubectl get pods --all-namespaces|grep grafana|awk '{print $2}'
To only display one line you can use the head command like so:
kubectl get pods --all-namespaces|grep grafana|awk '{print $2}'|head -n 1
This will output the last pod name :
kubectl get pods -o go-template --template ' {{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' | awk '{print $1}' | tail -n 1
if you want to fetch logs for a pod:
kubectl logs -f kubectl get pods -o go-template --template ' {{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' | awk '{print $1}' | tail -n 1
just run the command with a single quotes.
Links:
kubernetes list all running pods name
https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/
Hope this helps!!
I have a 6 minion cluster and would like to know how many of these minions are actually hosting pods at any given time. Is there a specific command for that ? Right now Im using a very generic command.
kubectl get po | grep Running > RUNNING.txt
for i in `cat RUNNING.txt `; do kubectl describe po $i; done | grep "Started container with docker
"
Any direct command to fetch the info I want ?
Just add -o wide:
kubectl get pod -o wide
This command will print all the nodes that have pods running on them:
kubectl get pods -o jsonpath='{range .items[*]}{.spec.nodeName} {end}' | tr " " "\n" | sort | uniq
I looking for the option to list all pods name
How to do without awk (or cut). Now i'm using this command
kubectl get --no-headers=true pods -o name | awk -F "/" '{print $2}'
Personally I prefer this method because it relies only on kubectl, is not very verbose and we don't get the pod/ prefix in the output:
kubectl get pods --no-headers -o custom-columns=":metadata.name"
You can use the go templating option built into kubectl to format the output to just show the names for each pod:
kubectl get pods --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'
Get Names of pods using -o=name Refer this cheatsheet for more.
kubectl get pods -o=name
Example output:
pod/kube-xyz-53kg5
pod/kube-xyz-jh7d2
pod/kube-xyz-subt9
To remove trailing pod/ you can use standard bash sed command
kubectl get pods -o=name | sed "s/^.\{4\}//"
Example output:
kube-xyz-53kg5
kube-pqr-jh7d2
kube-abc-s2bt9
To get podname with particular string, standard linux grep command
kubectl get pods -o=name | grep kube-pqr | sed "s/^.\{4\}//"
Example output:
kube-pqr-jh7d2
With this name, you can do things, like adding alias to get shell to running container:
alias bashkubepqr='kubectl exec -it $(kubectl get pods -o=name | grep kube-pqr | sed "s/^.\{4\}//") bash'
You can use custom-columns in output option to get the name and --no-headers option
kubectl get --no-headers=true pods -l app=external-dns -o custom-columns=:metadata.name
You can use -o=name to display only pod names. For example to list proxy pods you can use:
kubectl get pods -o=name --all-namespaces | grep kube-proxy
The result is:
pod/kube-proxy-95rlj
pod/kube-proxy-bm77b
pod/kube-proxy-clc25
There is also this solution:
kubectl get pods -o jsonpath={..metadata.name}
Here is another way to do it:
kubectl get pods -o=name --field-selector=status.phase=Running
The --field-selector=status.phase=Running is needed as the question mention all the running pod names. If the all in the question is for all the namespaces, just add the --all-namespaces option.
Note that this command is very convenient when one want a quick way to access something from the running pod(s), such as logs :
kubectl logs -f $(kubectl get pods -o=name --field-selector=status.phase=Running)
Get all running pods in the namespace
kubectl get pods --field-selector=status.phase=Running --no-headers -o custom-columns=":metadata.name"
From viewing, finding resources.
You could also specify namespace with -n <namespace name>.
jsonpath alternative
kubectl get po -o jsonpath="{range .items[*]}{#.metadata.name}{end}" -l app=nginx-ingress,component=controller
see also:
more examples of kubectl output options
If you want to extract specific container's pod name then
A simple command can do all the hard work
kubectl get pods --template '{{range .items}}{{.metadata.name}}{{end}}' --selector=app=<CONTAINER-NAME>
Just replace <CONTAINER-NAME> with your service container-name
Well, In our case we have kept pods inside different namespace, here to identify the specific pod or list of pods we ran following command-
Approach 1:
To get the list of namespaces
kubectl get ns -A
To get all the pods inside one namespaces kubectl get pods -n <namespace>
Approach 2:
Use this command-
kubectl get pods --all-namespaces
kubectl exec -it $(kubectl get pods | grep mess | awk '{print $1}') /bin/bash
kubectl get po --all-namespaces | awk '{if ($4 != "Running") system ("kubectl -n " $1 " delete pods " $2 " --grace-period=0 " " --force ")}'