Kubernetes: list all pods and its nodes - kubernetes

I have 3 nodes, running all kinds of pods. I would like to have a list of nodes and pods, for an example:
NODE1 POD1
NODE1 POD2
NODE2 POD3
NODE3 POD4
How can this please be achieved?
Thanks.

You can do that with custom columns:
kubectl get pod -o=custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName --all-namespaces
or just:
kubectl get pod -o=custom-columns=NODE:.spec.nodeName,NAME:.metadata.name --all-namespaces

kubectl has a simple yet useful extended output format that you can use like
kubectl get pod -o wide
so while custom formats provided in other answers are good, this might be a handy shortcut.

You can use kubectl get pods --all-namespaces to list all the pods from all namespaces and kubectl get nodes for listing all nodes.

The following command does more or less what you wanted. However, it's more of a jq trick than kubectl trick:
kubectl get pod --all-namespaces -o json | jq '.items[] | .spec.nodeName + " " + .status.podIP'

Not exactly as you wanted cause it describe much more, but you can use
kubectl describe nodes
it will expose each pod per node in the cluster with the following info
Namespace | Name | CPU Requests | CPU Limits | Memory Requests |
Memory Limits

This gets you: "nodeName namespace pod" across the cluster:
kubectl get pods --all-namespaces --output 'jsonpath={range .items[*]}{.spec.nodeName}{" "}{.metadata.namespace}{" "}{.metadata.name}{"\n"}{end}'

Maybe the answers are a little bit old, now you can simply launch this:
kubectl get pods -o wide

Related

Return list of all containerIDs in kubernetes pod

I would like to get a list of all container IDs from long running containers in a kubernetes pod. I'm working on creating a simple script that will docker commit any / all changes made to the images for further testing.
I've been experimenting with something like this kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].containerid}" to no avail. Gratitude in advance for any help / perspective.
Did you try this way?
kubectl get pods --all-namespaces -o jsonpath='{range .items[*].status.initContainerStatuses[*]}{.containerID}{"\n"}{end}' | cut -d/ -f3

List network policy rules kube-proxy on minikube

could you please help me? Where I can find list network policy rules kube-proxy on minikube?
Thanks.
Could you specify what exactly you want to list ? If it comes to listing network policies in all your namespaces, you can use the following command:
kubectl get networkpolicies --all-namespaces
If you want to list all your kube-proxy related resources, you can do it by running:
kubectl get all --all-namespaces | grep kube-proxy
As you can see there are only Pods and they are located in kube-system namespace, so kubectl get pods --namespace kube-system will also list them.
If you want to see their details, run:
kubectl describe pod <kube-proxy-pod-name> -n kube-system
Please let me know if this is what you're looking for.

List all the running container in whole cluster?

I want to know how I can check how many containers are currently running in my cluster? is there any command which shows me all the running containers in the cluster, not in a specific namespace. and how I can get the info about how many container per day get's run in my whole cluster?
You need to sum up all running containers in all pods. Try the following command.
kubectl get pod --all-namespaces | awk '{print $3}' | awk -F/ '{s+=$1} END {print s}'
Get all pods from all namespace :
kubectl get po --all-namespaces
Then you can have the number of containers in the READY column.
You can find some more info in the official doc
You can get pods by nodes and phase:
kubectl get po --all-namespaces=true --no-headers -o=custom-columns=NODE:.spec.nodeName,NAME:.metadata.name,STATUS:.status.phase --sort-by='.metadata.name'
hope this helps

List all containers in all pods with status

I am trying to get a list of all non-READY containers in all pods to debug a networking issue in my cluster.
Is it possible to use kubectl to get a clean list of all containers in all pods with their status (READY/..)?
I am currently using
$ kubectl get pods
However, the output can be huge and it can be difficult to know which containers are READY and which ones have issues.
Thanks.
kubectl get pods -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .status.containerStatuses[*]}{.ready}{", "}{end}{end}'
Adapted from this doc: https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/#list-containers-by-pod
Edit to describe what the jsonpath is doing:
From what I understand with the jsonpath, range iterates all of the .items[*] returned by getting the pods. \n is added to split the result to one per line, otherwise the result would come to one line. To see how the rest work, you should choose one of your pods and run:
kubectl get pod podname -o yaml
.metadata.name corresponds to
apiVersion: v1
kind: Pod
metadata:
name: podname
Similarly, .status.containerStatuses[*] corresponds to the list of container statuses that should be towards the bottom.
I've adapted #joshua-oliphant's answer to include container-names:
kubectl get pods -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .status.containerStatuses[*]}{.name}{": "}{.ready}{", "}{end}{end}'
This will display all pods including all containers in the current namespace with their ready-state such as:
pod-name: container-name-a: true, container-name-b: false
Using jq, we can list all pods and their containers' statuses:
kubectl get pods --output=json | jq --raw-output '.items | map([.metadata.name, (.status.containerStatuses | map(.name + ": " + (.ready | tostring)))[]]) | .[] | join("\n ")'
This displays something like this:
pod-1234-auiend
container1: true
container2: false
another-pod-2454-123123:
container1: true
(...)

How do you cleanly list all the containers in a kubernetes pod?

I am looking to list all the containers in a pod in a script that gather's logs after running a test. kubectl describe pods -l k8s-app=kube-dns returns a lot of info, but I am just looking for a return like:
etcd
kube2sky
skydns
I don't see a simple way to format the describe output. Is there another command? (and I guess worst case there is always parsing the output of describe).
Answer
kubectl get pods POD_NAME_HERE -o jsonpath='{.spec.containers[*].name}'
Explanation
This gets the JSON object representing the pod. It then uses kubectl's JSONpath to extract the name of each container from the pod.
You can use get and choose one of the supported output template with the --output (-o) flag.
Take jsonpath for example,
kubectl get pods -l k8s-app=kube-dns -o jsonpath={.items[*].spec.containers[*].name} gives you etcd kube2sky skydns.
Other supported output output templates are go-template, go-template-file, jsonpath-file. See http://kubernetes.io/docs/user-guide/jsonpath/ for how to use jsonpath template. See https://golang.org/pkg/text/template/#pkg-overview for how to use go template.
Update: Check this doc for other example commands to list container images: https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/
Quick hack to avoid constructing the JSONpath query for a single pod:
$ kubectl logs mypod-123
a container name must be specified for pod mypod-123, choose one of: [etcd kubesky skydns]
I put some ideas together into the following:
Simple line:
kubectl get po -o jsonpath='{range .items[*]}{"pod: "}{.metadata.name}{"\n"}{range .spec.containers[*]}{"\tname: "}{.name}{"\n\timage: "}{.image}{"\n"}{end}'
Split (for readability):
kubectl get po -o jsonpath='
{range .items[*]}
{"pod: "}
{.metadata.name}
{"\n"}{range .spec.containers[*]}
{"\tname: "}
{.name}
{"\n\timage: "}
{.image}
{"\n"}
{end}'
How to list BOTH init and non-init containers for all pods
kubectl get pod -o="custom-columns=NAME:.metadata.name,INIT-CONTAINERS:.spec.initContainers[*].name,CONTAINERS:.spec.containers[*].name"
Output looks like this:
NAME INIT-CONTAINERS CONTAINERS
helm-install-traefik-sjts9 <none> helm
metrics-server-86cbb8457f-dkpqm <none> metrics-server
local-path-provisioner-5ff76fc89d-vjs6l <none> local-path-provisioner
coredns-6488c6fcc6-zp9gv <none> coredns
svclb-traefik-f5wwh <none> lb-port-80,lb-port-443
traefik-6f9cbd9bd4-pcbmz <none> traefik
dc-postgresql-0 init-chmod-data dc-postgresql
backend-5c4bf48d6f-7c8c6 wait-for-db backend
if you want a clear output of which containers are from each Pod
kubectl get po -l k8s-app=kube-dns \
-o=custom-columns=NAME:.metadata.name,CONTAINERS:.spec.containers[*].name
To get the output in the separate lines:
kubectl get pods POD_NAME_HERE -o jsonpath='{range .spec.containers[*]}{.name}{"\n"}{end}'
Output:
base-container
sidecar-0
sidecar-1
sidecar-2
If you use json as output format of kubectl get you get plenty details of a pod. With json processors like jq it is easy to select or filter for certain parts you are interested in.
To list the containers of a pod the jq query looks like this:
kubectl get --all-namespaces --selector k8s-app=kube-dns --output json pods \
| jq --raw-output '.items[].spec.containers[].name'
If you want to see all details regarding one specific container try something like this:
kubectl get --all-namespaces --selector k8s-app=kube-dns --output json pods \
| jq '.items[].spec.containers[] | select(.name=="etcd")'
Use below command:
kubectl get pods -o=custom-columns=PodName:.metadata.name,Containers:.spec.containers[*].name,Image:.spec.containers[*].image
To see verbose information along with configmaps of all containers in a particular pod, use this command:
kubectl describe pod/<pod name> -n <namespace name>
Use below command to see all the information of a particular pod
kubectl get pod <pod name> -n <namespace name> -o yaml
For overall details about the pod try following command to get the container details as well
kubectl describe pod <podname>
I use this to display image versions on the pods.
kubectl get pods -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{end}{end}' && printf '\n'
It's just a small modification of script from here, with adding new line to start next console command on the new line, removed commas at the end of each line and listing only my pods, without service pods (e.g. --all-namespaces option is removed).
There are enough answers here but sometimes you want to see a deployment object pods' containers and initContainers. To do that;
1- Retrieve the deployment name
kubectl get deployment
2- Retrieve containers' names
kubectl get deployment <deployment-name> -o jsonpath='{.spec.template.spec.containers[*].name}'
3- Retrieve initContainers' names
kubectl get deployment <deployment-name> -o jsonpath='{.spec.template.spec.initContainers[*].name}'
Easiest way to know the containers in a pod:
kubectl logs -c -n