Looking at http://kubernetes.io/docs/user-guide/labels/#selecting-sets-of-nodes it looks to be possible to select a certain range of pods based on labels. But in my case I w
kubectl describe node <node>
will show all of the non-terminated pods running on that node
You also can query for all pods an a node with the following command
kubectl get pods -o wide --all-namespaces | grep <YOUR-NODE>
As mentioned in the accepted answer the PR is now merged and you can get pods by node as follows:
kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=<node>
I've gone through the same process with the Go Client and it uncovers a few shortcuts the CLI is taking.
func doNodesHavePods(clientset *kubernetes.Clientset) error {
nodeLabelSelector := "nodelabel=interesting_nodes"
nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{LabelSelector: nodeLabelSelector})
if err != nil {
return err
}
nodeNames := []string{}
for _, node := range nodes.Items {
nodeNames = append(nodeNames, node.Name)
}
// --all-namespaces -> listing and looping on namespaces
namespaces, err := clientset.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
return err
}
for _, namespace := range namespaces.Items {
for _, name := range nodeNames {
// pods need a namespace to be listed.
pods, err := clientset.CoreV1().Pods(namespace.Name).List(metav1.ListOptions{FieldSelector: "spec.nodeName=" + name})
if err != nil {
println("%v", err)
}
for _, pod := range pods.Items {
fmt.Println(pod.Namespace, pod.Name)
}
}
}
return nil
}
I've started to find that a lot of the questions I need to ask are becoming too complex for the CLI which is a great workhorse, but learning to use the Go Client can help you get the first answer you're looking for, but also dig deeper into questions that those answers raise.
Example sorting pods by nodeName:
kubectl get pods -o wide --sort-by="{.spec.nodeName}"
Example of getting pods on nodes using label filter:
for n in $(kubectl get nodes -l your_label_key=your_label_value --no-headers | cut -d " " -f1); do
kubectl get pods --all-namespaces --no-headers --field-selector spec.nodeName=${n}
done
or by number of restarts
kubectl get pods --sort-by="{.status.containerStatuses[:1].restartCount}"
Example filtering by nodeName using --template flag:
$ kubectl get nodes
NAME STATUS AGE
ip-10-0-90-30.ec2.internal Ready 2d
ip-10-0-90-35.ec2.internal Ready 2d
ip-10-0-90-50.ec2.internal Ready,SchedulingDisabled 2d
ip-10-0-91-60.ec2.internal Ready 2d
ip-10-0-91-65.ec2.internal Ready 2d
$kubectl get pods --template '{{range .items}}{{if eq .spec.nodeName "ip-10-0-90-30.ec2.internal"}}{{.metadata.name}}{{"\n"}}{{end}}}{{end}}'
filebeat-pezch
app-5xole
node-exporter-6kfs8
prometheus-0
sso-359976856-wu8zt
What you want is supported in the Kubernetes API server-side like this:
curl --cacert ca.crt --cert apiserver.crt --key apiserver.key https://<server>:<port>/api/v1/namespaces/<namespace>/pods?fieldSelector=spec.nodeName%3Dsomenodename
However that field selector option is not built into kubectl
yet: https://github.com/kubernetes/kubernetes/pull/50140