oc Cheat Sheet

published book

oc is the primary command line for OpenShift. It includes tools to build, deploy, and administer containers.

Logging in

oc login -u=<username> -p=<password> --server=<your-openshift-server> --insecure-skip-tls-verify

Switch to a specific project or log in directly to a project:

oc project <myproject>
oc login -n <myproject>

Building and deploying applications

Build code or a Dockerfile in a Git repo:

oc new-app centos/ruby-22-centos7~https://github.com/openshift/ruby-ex.git

Build from a template:

oc new-app --template=myproject/mytemplate -p=<param_name>=<param_value>

Create other objects from a file:

oc create -f myobject.yaml -n <myproject>

Update an existing object:

oc patch svc mysvc --type merge --patch '{"spec":{"ports":[{"port": 8080, "targetPort": 5000 }]}}'

Accessing running applications

oc exec <mypod> cat /opt/app-root/myapp.config
oc rsh <mypod
oc debug dc <mydc>

Common commands

oc status
oc logs pod <mypod>
oc get pods --all-namespaces
oc describe pod <mypod>
oc get services --sort-by=.metadata.name
oc delete all -l app=tomcat
oc delete pod <mypod> --grace-period=0
oc export bc,dc,is,svc --as-template=myapp.yaml

Scaling applications

oc scale dc <mydc> --replicas=5
oc autoscale dc/app-cli --min 2 --max 5 --cpu-percent=75

Administrative operations using the adm subcommand

Manage user roles:

oc adm policy add-role-to-user admin oia -n python
oc adm policy add-cluster-role-to-user cluster-reader system:serviceaccount:monitoring:default
oc adm policy add-scc-to-user anyuid -z default

Manage node state:

oc adm manage node <node> --schedulable=false

Run cluster diagnostics:

oc adm diagnostics

Advanced operations

Create a config map file and mount it as a volume to a deployment config:

oc create configmap propsfilecm --from-file=application.properties
oc set volumes dc/myapp --add --overwrite=true --name=configmap-volume
 --mount-path=/data -t configmap --configmap-name=propsfilecm

Create a secret from the CLI and mount it as a volume to a deployment config:

oc create secret generic oia-secret --from-literal=username=myuser
 --from-literal=password=mypassword
oc set volumes dc/myapp --add --name=secret-volume --mount-path=/opt/app-root/
 --secret-name=oia-secret
Get OpenShift in Action
add to cart
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage