Helpful oc / kubectl commands

- Toni Schmidbauer Toni Schmidbauer ( Lastmod: 2024-05-08 ) - 1 min read

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
ta.name, 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

DESCRIPTION:
     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