Get all pods except the pods inside kube-system - kubernetes

When I do
kubectl get pods -A
I get all pods, and I always have 17 pods that are not "apps", they belong to namespace kube-system. I would like to have an alias not to print them.
Is there a way to print all pods, excluding a namespace ?

You can accomplish this via field selectors:
kubectl get pods -A --field-selector=metadata.namespace!=kube-system
Additionally, the field selector list can have multiple parameters, separated by , (comma literals), and use == or != to specify additional criteria.

Use --field-selector
kubectl get pods --all-namespaces --field-selector metadata.namespace!=kube-system
more about field selectors here: https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/

Related

is it possible to get all pods from a list of namescapes?

I have a lot of namespaces and I want to get all pods from a sub-list of namespaces.
For getting all the pods from all namespace the command is:
kubectl get pods --all-namespaces
To get all pods from a spesific namespace the command is:
kubectl get pods -n namespace-name
However I can't find a way to get all pods from a list of namespaces, something like:
kubectl get pods -n namespace-name1, namespace-name2, namespace-name3
what is the right command for that?
kubectl does not support this. You can use egrep to filter the list of all pods by namespaces:
kubectl get pods -A | egrep '^(namespace-name1|namespace-name2|namespace-name3)'
Because kubectl prints the namespace at the beginning of the line, it greps for a line start ^ followed by one of the namespace names.
You can iterate over the subset of namespaces:
Either:
for NAMESPACE in "namespace-1" "namespace-2"
do
kubectl get pods \
--namespace=${NAMESPACE} \
--output=name
done
Or:
NAMESPACE=$(
"namespace-1"
"namespace-2"
)
for NAMESPACE in "${NAMESPACES[#]}"
do
kubectl get pods \
--namespace=${NAMESPACE} \
--output=name
done

Kubernetes List All Containers that are not running

How to list All Containers that are not running using Kubectl command. I want output like
CONTAINER_NAME STATUS POD_NAME NAMESPACE <br>
container_1 Running pod_1 ns1 <br>
container_2 Not Running pod_2 ns2 <br>
container_3 Running pod_2 ns2 <br>
kubectl get pods --field-selector status.phase!=Running
Above command will above list down all pods, not in Running status for default namespace.
if you want to run a command across all namespaces & list down all PODS
kubectl get pods --field-selector status.phase!=Running --all-namespaces
You can also print custom column as per require if you want to print Namespace
kubectl get pod --field-selector status.phase!=Running -o=custom-columns=NAME:.metadata.name,STATUS:.status.phase,NAMEDPACE:.metadata.namespace
Final command the way you are looking forward with columns
kubectl get pod --field-selector status.phase!=Running -o=custom-columns=POD_NAME:.metadata.name,STATUS:.status.phase,NAMEDPACE:.metadata.namespace,CONTAINER_NAME:.spec.containers[*].name
In Addition to above answer, I had a special usecase where I wanted to get all the non-running pods names to remove them.
So I used this to get the names as list
kubectl get pods --all-namespace --field-selector status.phase!="Running" -o=jsonpath='{.items[*].metadata.name}'

How to get the pods from the namespaces with a particular label?

It is possible to get all the pods on the cluster:
kubectl get pod --all-namespaces -o wide
It is also possible to get all pods on the cluster with a specific label:
kubectl get pod --all-namespaces -o wide --selector some.specific.pod.label
It is even possible to get all pods on the specific node of the cluster:
kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=<node>
The question is, how to get all pods from the namespaces with a particular label?
e.g. kubectl get pod --namespace-label some.specific.namespace.label -o wide (pseudocode)
One cannot do that operation in one shot, because labels on Namespace objects are not propagated down upon their child objects. Since kubectl is merely doing a GET on /api/v1/whatevers there is no obvious way to make a REST request to two endpoints at once and join them together.
You'll want either a for loop in shell, or to use one of the many API client bindings to create a program that does the Namespace fetch, then a Pod fetch for those matching Namespaces; for example:
for n in $(kubectl get ns --selector some.specific.namespace.label -o name); do
# it's possible kubectl -n will accept the "namespace/foo" output of -o name
# or one can even -o go-template='{{ range .items }}{{ .metadata.name }} {{ end }}'
n=${n##namespace/}
kubectl -n "$n" get pods -o wide
done

How can I use multiple parameter on kubernetes CLI?

I am seeking to how to be able to use multiple parameter on kubernetes. For example:
kubectl get pods -n default || kube-system
(but the results of this query come out only the result of default namespace).
How can I use multiple params?
You can't query for multiple namespaces resources in one command.
As there is explanation why it is not worth to do that on this github issue
But you can query for multiple resources across one or --all-namespaces. For example got get services and pods for namespace kube-dns and default(this will include workaround as #PEkambaram suggested)
kubectl get svc,pods --all-namespaces |egrep -e 'kube-dns|default'
try this
kubectl get po --all-namespaces | grep kube-system
or even better
kubectl get po --all-namespaces | grep -iE 'dns|api'

Kubectl pod vs pods

I have noticed that when using kubectl you can pretty much use pod and pods interchangeably. Is there any instance when using one instead of the other could get you different results or can you just use either without worrying about it?
For example:
kubectl get pods
kubectl get pod
kubectl describe pod/app
kubectl describe pods/app
and so on...
From the kubectl documentation:
Resource types are case-insensitive and you can specify the singular,
plural, or abbreviated forms. For example, the following commands
produce the same output:
kubectl get pod pod1
kubectl get pods pod1
kubectl get po pod1
It doesn't matter and both ways of writing will always result in the same result.