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
Command Description Example oc login Log in to an OpenShift cluster oc login –server=https://api.example.com:6443 –username=myuser –password=mypassword oc project Switch to a specific project oc project myproject oc projects List all projects you have access to oc projects oc status Show an overview of the current project oc status oc new-project Create a new project oc new-project testproject –description=”Test project for development”
Application Management
Command Description Example oc new-app Create a new application oc new-app –name=myapp https://github.com/openshift/nodejs-ex.git oc expose Expose a service as a route oc expose service myapp –hostname=myapp.example.com oc get List resources oc get pods or oc get all oc describe Show detailed information about a resource oc describe pod mypod oc scale Scale a deployment configuration oc scale –replicas=3 dc/myapp oc rollout Manage the rollout of new revisions oc rollout status dc/myapp oc delete Delete resources oc delete pod mypod
Build and Deploy
Command Description Example oc new-build Create a new build configuration oc new-build –strategy=docker –name=mybuild –binary=true oc start-build Start a build oc start-build mybuild –from-dir=. –follow oc logs View logs for a resource oc logs bc/mybuild
Debugging and Troubleshooting
Command Description Example oc rsh Remote shell into a container oc rsh mypod oc exec Execute a command in a container oc exec mypod — ls /app oc port-forward Forward one or more local ports to a pod oc port-forward mypod 8080:80
Networking
Command Description Example oc get routes List all routes in the current project oc get routes oc get services List all services in the current project oc get services
Advanced Usage
Command Description Example oc patch Update fields of a resource oc patch dc/myapp -p ‘{“spec”:{“template”:{“spec”:{“containers”:[{“name”:”myapp-container”,”env”:[{“name”:”MY_VAR”,”value”:”myvalue”}]}]}}}}’ oc set Commands to set specific features on objects oc set env dc/myapp MY_VAR=myvalue oc apply Create or update resources from a file or stdin oc 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.