Helpful oc / kubectl commands

- By: Toni Schmidbauer ( Lastmod: 2021-08-14 )

This is a list of useful oc and/or kubectl commands so they won’t be forgotton. No this is not a joke…​

List all pods in state Running

oc get pods --field-selector=status.phase=Running

List all pods in state Running and show there resource usage

oc get pods --field-selector=status.phase=Running -o json|jq ".items[] | {name: .metada, res: .spec.containers[].resources}"

List events sort by time created

 oc get events --sort-by='.lastTimestamp'

Explain objects while specifing the api version

Sometimes when you run oc explain you get a message in DESCRIPTION that this particular version is deprecated, e.g. you are running oc explain deployment and get

     DEPRECATED - This group version of Deployment is deprecated by
     apps/v1/Deployment. See the release notes for more information. Deployment
     enables declarative updates for Pods and ReplicaSets.
Note the DEPRECTATED message above

if you want to see the documentation for the object that has not been deprecated you can use

oc explain deployment --api-version=apps/v1

Magic with oc set

oc set is actually a very versatile command. Studying oc set -h is a good idea, here are some examples

Set route weights when alternateBackends in a route are defined

oc set route-backends bluegreen blue=1 green=9

Set resources on the command line

oc set resources dc cakephp-mysql-example --limits=memory=1Gi,cpu=200m