Join our Discord Server

OpenShift CheatSheet

OpenShift is Red Hat’s container platform based on Kubernetes. It extends Kubernetes with additional features for easier management, security, and developer experience. Below are some commonly used oc commands with examples:

Basic Commands

CommandDescriptionExample
oc loginLog in to an OpenShift clusteroc login –server=https://api.example.com:6443 –username=myuser –password=mypassword
oc projectSwitch to a specific projectoc project myproject
oc projectsList all projects you have access tooc projects
oc statusShow an overview of the current projectoc status
oc new-projectCreate a new projectoc new-project testproject –description=”Test project for development”

Application Management

CommandDescriptionExample
oc new-appCreate a new applicationoc new-app –name=myapp https://github.com/openshift/nodejs-ex.git
oc exposeExpose a service as a routeoc expose service myapp –hostname=myapp.example.com
oc getList resourcesoc get pods or oc get all
oc describeShow detailed information about a resourceoc describe pod mypod
oc scaleScale a deployment configurationoc scale –replicas=3 dc/myapp
oc rolloutManage the rollout of new revisionsoc rollout status dc/myapp
oc deleteDelete resourcesoc delete pod mypod

Build and Deploy

CommandDescriptionExample
oc new-buildCreate a new build configurationoc new-build –strategy=docker –name=mybuild –binary=true
oc start-buildStart a buildoc start-build mybuild –from-dir=. –follow
oc logsView logs for a resourceoc logs bc/mybuild

Debugging and Troubleshooting

CommandDescriptionExample
oc rshRemote shell into a containeroc rsh mypod
oc execExecute a command in a containeroc exec mypod — ls /app
oc port-forwardForward one or more local ports to a podoc port-forward mypod 8080:80

Networking

CommandDescriptionExample
oc get routesList all routes in the current projectoc get routes
oc get servicesList all services in the current projectoc get services

Advanced Usage

CommandDescriptionExample
oc patchUpdate fields of a resourceoc patch dc/myapp -p ‘{“spec”:{“template”:{“spec”:{“containers”:[{“name”:”myapp-container”,”env”:[{“name”:”MY_VAR”,”value”:”myvalue”}]}]}}}}’
oc setCommands to set specific features on objectsoc set env dc/myapp MY_VAR=myvalue
oc applyCreate or update resources from a file or stdinoc apply -f deployment.yaml

This cheat sheet provides a quick reference for developers and administrators working with OpenShift to manage applications, builds, and troubleshooting. Remember, the exact commands might vary slightly based on the OpenShift version you’re using, so always refer to the official documentation for the most accurate information.

Join our Discord Server