Goglides Dev 🌱

Balkrishna Pandey
Balkrishna Pandey

Posted on • Updated on

oc get all pods handy cheat sheet

  • Pod which are not running Across all namespaces
oc get po -A \
| grep -v Completed \
| awk -F"[ /]+" 'BEGIN{found=0} !/NAME/ {if ($3!=$4) { found=1; print $0}} END { if (!found) print "Not Found"}'
Enter fullscreen mode Exit fullscreen mode
  • Delete all pods which are not running across all namespaces
oc get po -A \
| grep -v Completed \
| awk -F"[ /]+" 'BEGIN{found=0} !/NAME/ {if ($3!=$4) { found=1; print $0}} END { if (!found) print "Not Found"}' \
| awk  '{ printf "oc delete po %s -n %s \n", $2 , $1}'
Enter fullscreen mode Exit fullscreen mode

Next Copy and paste output from above command or run above command with |bash at the end.

  • Delete all failed pods in a single namespace where context is set
oc get po -o wide \
| grep -v Completed \
| awk -F"[ /]+" 'BEGIN{found=0} !/NAME/ {if ($2!=$3) { found=1; print $0}} END { if (!found) print "Not Found"}' \
| awk  '{ printf "oc delete po %s  \n", $1 }'
Enter fullscreen mode Exit fullscreen mode

Next Copy and paste output from above command or run above command with |bash at the end.

  • Pods which are not running on openshift-ingress namespace.
oc get po -n openshift-ingress \
| grep -v Completed \
| awk -F"[ /]+" 'BEGIN{found=0} !/NAME/ {if ($2!=$3) { found=1; print $0}} END { if (!found) print "Not Found"}'
Enter fullscreen mode Exit fullscreen mode

Here I am comparing column 2 with 3, notice header changes in oc get pods -A vs oc get pods

[root@bastion ~]# oc get pods | head -n 1
NAME          READY   STATUS    RESTARTS   AGE
[root@bastion ~]# oc get pods -A | head -n 1
NAMESPACE                                          NAME                                                              READY   STATUS                  RESTARTS   AGE
Enter fullscreen mode Exit fullscreen mode
  • Count all Pods per Nodes
oc get nodes -o custom-columns=NODE:.metadata.name --no-headers \
| while read node; do 
echo "$node $(oc get pods --all-namespaces=true --field-selector spec.nodeName=$node --no-headers | wc -l)"; 
done
Enter fullscreen mode Exit fullscreen mode
  • Count Pods per Namespace
oc get --all-namespaces=true pods \
| awk '{a[$1]++} END {for (i in a) {print i, a[i]}}'
Enter fullscreen mode Exit fullscreen mode
  • Pods which are running since 24h
oc get pods --field-selector=status.phase=Running -o json \
| jq '.items[] | select(.metadata.creationTimestamp | fromdateiso8601 > (now - 86400)) | .metadata.name'
Enter fullscreen mode Exit fullscreen mode
  • Pods which are running since 24h in a specific node
oc get pods  --field-selector=status.phase=Running,spec.nodeName=worker-11 -o json \
| jq '.items[] | select(.metadata.creationTimestamp | fromdateiso8601 > (now - 86400)) | .metadata.name'
Enter fullscreen mode Exit fullscreen mode
  • List all operators installed in OCP
oc get csv --all-namespaces -o jsonpath='{range .items[*]}{"\n"}{.metadata.name}{end}' | sort | uniq
Enter fullscreen mode Exit fullscreen mode
  • Cleanup quay operator. Command is applicable for other operators as well, make sure to adjust the command.
JOB_NAME=$(oc get job -n openshift-marketplace -o json | jq -r '.items[] | select(.spec.template.spec.containers[].env[].value|contains ("quay")) | .metadata.name')
echo $JOB_NAME
oc delete job $JOB_NAME -n openshift-marketplace
oc delete configmap $JOB_NAME -n openshift-marketplace
for i in `oc get ip -n openshift-operators | grep quay | awk '{ print $1}'`; do echo $i; oc delete ip $i -n openshift-operators; done;
CURRENT_CSV=$(oc get subscription quay-operator -n openshift-operators -o=jsonpath='{.status.currentCSV}')
echo $CURRENT_CSV
oc delete subscription quay-operator -n openshift-operators
oc delete csv  $CURRENT_CSV -n openshift-operators
Enter fullscreen mode Exit fullscreen mode
  • ODF device list
for pod in `oc get pods -l app=csi-rbdplugin --no-headers|awk '{print $1}'`
do
echo $pod
oc exec -n openshift-storage $pod -c  csi-rbdplugin -- rbd device list
done
Enter fullscreen mode Exit fullscreen mode

ODF device list, grep for particular csi-vol

for pod in `oc get pods -l app=csi-rbdplugin --no-headers|awk '{print $1}'`
do
echo $pod
oc exec -n openshift-storage $pod -c  csi-rbdplugin -- rbd device list | grep csi-vol-8b0eade3-1995-11ed-973d-0a580a800311 
done
Enter fullscreen mode Exit fullscreen mode

Output of this command looks like this,

csi-rbdplugin-58mqv
...
5  ocs-storagecluster-cephblockpool           csi-vol-6e9e743b-1995-11ed-973d-0a580a800311 -    /dev/rbd5  
...
Enter fullscreen mode Exit fullscreen mode

Now we can unmap this device as follows,

oc exec -it csi-rbdplugin-66wtg -c csi-rbdplugin bash
rbd  device unmap /dev/rbd5 -o force
Enter fullscreen mode Exit fullscreen mode

Top comments (0)