This is an extension to the question here - how can I get the list pods running on nodes with a certain label?
I am trying to find the pods in a specific zone (failure-domain.beta.kubernetes.io/zone)
You can get all nodes' name with the label you want using for command and list the pods within theses nodes:
Example:
for node in $(kubectl get nodes -l failure-domain.beta.kubernetes.io/zone=us-central1-c -ojsonpath='{.items[*].metadata.name}'); do kubectl get pods -A -owide --field-selector spec.nodeName=$node; done
The command will list all pods with label failure-domain.beta.kubernetes.io/zone=us-central1-c and then list the pods.
Related
Say I have a 5 node cluster of kafka and a kubernetes cluster of 100 nodes.
Now, I want to find all 5 nodes (of 100) which is hosting kafka pod. So something like:
kubectl get nodes --selector="deployment.kafka"
I dont think thats possible, what you can do is select your pods based on labels and get the node name, As #Krishna said in his comment, so the command will be
kubectl get pods -n NAMESPACE_NAME -l app=kafka -o wide | awk '{print $7}'
app=kafka is the label on the pods, it might be different in your case
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}'
I'm trying to get the ip address of pods with particular label using jsonpath with the following command:
kubectl get pods -l app=validate -n {namespace_name} -o jsonpath={.status.podIP}
But this doesn't result into anything, even though the namespace and label names are correct. On the other hand, if I try to do:
kubectl get pod/pod_name -n {namespace_name} -o jsonpath={.status.podIP}
I'm able to get the pod IP address after that. But the problem is, since I'm trying to query all the pods created for a particular deployment, I want to fetch Ip addresses for all the pods under that particular label. I'm not sure what is wrong with the command.
If you have multiple Pods with the same label, you get a list of Pods. You have to adjust your jsonpath to -o jsonpath="{.items[*].status.podIP}" to get all the podIPs.
According to the official doc, you can add custom columns when querying a list of resources.
So you can do kubectl get pods -l app=validate -n {namespace_name} -o custom-columns=ip:.status.podIP
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/
I know we can do the following commands:
kubectl get pods -l app==<kafka> gets pods with kafka label
kubectl get pods -l app!=<kafka> gets pods without kafka label
kubectl get pods -l app=kafka,env=staging gets pods with both kafka and staging labels
But what about if I want to list all the pods which have either kafka or zookeeper label. Something like -l app==kafka||zookeeper.
Is this even possible with -l kubectl option...?
Have you tried this?
kubectl get pods -l 'app in (kafka, zookeeper)'
See: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#api