How to get an insight about containers status, restarts, exit code, reason, CPU and memory requests and limits in OpenShift 4?

Solution Verified - Updated

Environment

  • Red Hat OpenShift Container Platform (RHOCP)
    • 4
  • Containers

Issue

  • How to get the most important information about containers running in the OCP environment, all together, in a table format?

Resolution

Disclaimer: Links contained herein to external website(s) are provided for convenience only. Red Hat has not reviewed the links and is not responsible for the content or its availability. The inclusion of any link to an external website does not imply endorsement by Red Hat of the website or their entities, products or services. You agree that Red Hat is not responsible or liable for any loss or expenses that may result due to your use of (or reliance on) the external site or content.

The below query would be of help to get an insight into the most important fields about the pods and the containers running into them:

$ oc get po -A -o json | jq -r '["NODE_NAME", "NAMESPACE", "POD", "SCHEDULER", "POD_PHASE", "CONTAINER", "READY", "STARTED_AT", "P_EXIT_C", "P_FINISHED_AT", "REASON", "EXIT_C", "RESTARTS", "CPU_REQ", "MEM_REQ", "CPU_LIM", "MEM_LIM"], (.items[] | . as $ROOT | $ROOT.status.containerStatuses // [] | .[] | . as $CONTAINER_STATUS | [$ROOT.spec.nodeName // "-", $ROOT.metadata.namespace, $ROOT.metadata.name, $ROOT.spec.schedulerName, $ROOT.status.phase, $CONTAINER_STATUS.name, $CONTAINER_STATUS.ready, $CONTAINER_STATUS.state.running.startedAt // "-", $CONTAINER_STATUS.lastState.terminated.exitCode // "-" , $CONTAINER_STATUS.lastState.terminated.finishedAt // "-", $CONTAINER_STATUS.lastState.terminated.reason // "-", $CONTAINER_STATUS.state.terminated.exitCode // "-", $CONTAINER_STATUS.restartCount // "-", ($ROOT.spec.containers[] | select(.name==$CONTAINER_STATUS.name) | (.resources.requests.cpu|tostring), (.resources.requests.memory|tostring), (.resources.limits.cpu|tostring), (.resources.limits.memory|tostring))]) | @tsv' | column -t

The following fields will be printed:

  • NODE_NAME: Name of the node where the pod is executed.
  • NAMESPACE: Name of the namespace where the pod is running.
  • POD: Name of the pod.
  • SCHEDULER: Scheduler configured for the pod.
  • POD_PHASE: Pod's current Content from kubernetes.io is not included.phase.
  • CONTAINER: Container name.
  • READY: Container's readiness status.
  • STARTED_AT: Timestamp of the container's start.
  • P_EXIT_C: Last state exit code (i.e. if the pod has restarted once it will show the exit code for the last time it ended).
  • P_FINISHED_AT: Timestamp of the container's last execution exit.
  • REASON: Reason for the container's last execution exit.
  • EXIT_C: Container's current execution exit code.
  • RESTARTS: Container's restarts count.
  • CPU_REQ: Container's CPU requests.
  • MEM_REQ: Container's memory requests.
  • CPU_LIM: Container's CPU limits.
  • MEM_LIM: Container's memory limits.

Note: it is possible to filter the output for only a specific namespace changing the -A with the `-n [namespace_name].

Root Cause

Having specific information from all the pods in a cluster could help troubleshooting scheduling and performance issues, among others.

SBR
Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.