List all the running container in whole cluster? - kubernetes

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

Related

Find out all the pods that are using default service account

We have a k8s cluster with 10 workers. we run hundreds of pods in the cluster. we want to avoid running pods with default service account.
Need to find out the pods that are running with default service account. am able to find the number of pods using default service account with grep command but also need the pod name and the image it is using. Let us know your thoughts
In Case if you want to use just kubectl without jq :
needed to print both namespace and the pod name
kubectl get pods --all-namespaces -o jsonpath='{range .items[?(#.spec.serviceAccountName == "default")]}{.metadata.namespace} {.metadata.name}{"\n"}{end}' 2>/dev/null
i have added 2>/dev/null to avoid printing whole json template in case if no field was found
I used the below command to identify the pods from each namespace that is using default service account
kubectl get pods --all-namespaces -o json | jq '.items[] | select(.spec.serviceAccountName?=="default") | "\(.metadata.namespace) \(.metadata.name)"' | cut -d'"' -f2 | sort
if you are using k9s you can also :pod then e the pod to see which service account it is associated with

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

kubectl - How to get the list of all pods that have been restarted at least once

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'

Kubernetes API - gets list of Nodes hosting specific deployment/pods

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

Kubernetes: list all pods and its nodes

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