Skip to content

OpenShift

OpenShift Command Line Interface (oc) for Red Hat OpenShift Container Platform. Manage projects, applications, builds, and resources.

Installation

Linux/Ubuntu

# Download oc CLI
curl -L https://mirror.openshift.com/pub/openshift-v4/clients/ocp/latest/openshift-client-linux.tar.gz -o oc.tar.gz
tar xzf oc.tar.gz
sudo mv oc /usr/local/bin/

# Verify installation
oc version

macOS

# Using Homebrew
brew install openshift-cli

# Download from official source
curl -L https://mirror.openshift.com/pub/openshift-v4/clients/ocp/latest/openshift-client-mac.tar.gz -o oc.tar.gz
tar xzf oc.tar.gz
sudo mv oc /usr/local/bin/

Windows

# Using Chocolatey
choco install openshift-cli

# Download and extract
# https://mirror.openshift.com/pub/openshift-v4/clients/ocp/latest/openshift-client-windows.zip

# Verify installation
oc version

Basic Commands

CommandDescription
oc versionShow OpenShift and Kubernetes versions
oc helpDisplay help information
oc login [URL]Authenticate to OpenShift cluster
oc logoutRemove local authentication token
oc whoamiDisplay current user
oc statusShow project status
oc cluster-infoDisplay cluster endpoint info
oc api-versionsList all API versions
oc api-resourcesList all available resources

Project Management

CommandDescription
oc new-project [name]Create new project/namespace
oc delete project [name]Delete project
oc project [name]Switch to project
oc projectsList all projects
oc project -qGet current project
oc get projectsList projects with details
oc describe project [name]Show project details
oc get namespaceList namespaces

Applications & Deployments

Creating Applications

# Create app from source code
oc new-app https://github.com/user/repo

# Create from Docker image
oc new-app docker.io/httpd:latest

# Create from local Dockerfile
oc new-app --docker-image=myapp:latest --name=myapp

# Create from template
oc new-app --template=mongodb-template

# Create with name and labels
oc new-app docker.io/nginx --name=web --labels=app=web,tier=frontend

Managing Deployments

# Get all deployments
oc get deployments

# View deployment details
oc describe deployment [name]

# Rollout status
oc rollout status deployment/[name]

# Rollout history
oc rollout history deployment/[name]

# Rollback to previous version
oc rollout undo deployment/[name]

# Rollback to specific revision
oc rollout undo deployment/[name] --to-revision=2

# Scale deployment
oc scale deployment/[name] --replicas=3

Pods & Containers

CommandDescription
oc get podsList all pods in project
oc get pods -o wideList pods with node details
oc describe pod [name]Show pod details
oc logs [pod-name]View pod logs
oc logs -f [pod-name]Follow pod logs (streaming)
oc logs [pod-name] -c [container]View specific container logs
oc exec [pod-name] -- [command]Execute command in pod
oc exec -it [pod-name] /bin/bashInteractive shell in pod
oc port-forward [pod-name] [local-port]:[pod-port]Port forward to pod
oc delete pod [name]Delete pod

Services & Routes

Services

# Get all services
oc get services

# Describe service
oc describe service [name]

# Create service
oc expose deployment [name] --port=8080

# Get service endpoints
oc get endpoints [name]

# Delete service
oc delete service [name]

Routes (Ingress)

# Create route to service
oc expose service [service-name]

# Create route with hostname
oc expose service [service-name] --hostname=example.com

# Get all routes
oc get routes

# Describe route
oc describe route [name]

# Delete route
oc delete route [name]

# List routes with details
oc get routes -o wide

Builds & BuildConfigs

CommandDescription
oc new-build [source]Create new BuildConfig
oc start-build [bc-name]Trigger build
oc start-build [bc-name] --from-dir=.Start build from local directory
oc get buildsList all builds
oc describe build [name]Show build details
oc logs build/[name]View build logs
oc cancel-build [name]Cancel running build
oc get bcList all BuildConfigs

Configuration & Secrets

ConfigMaps

# Create ConfigMap from file
oc create configmap [name] --from-file=[file]

# Create ConfigMap from key-value pair
oc create configmap [name] --from-literal=key=value

# Get ConfigMap
oc get configmap [name]

# Edit ConfigMap
oc edit configmap [name]

# Delete ConfigMap
oc delete configmap [name]

Secrets

# Create secret from file
oc create secret generic [name] --from-file=[file]

# Create Docker registry secret
oc create secret docker-registry [name] --docker-server=registry.example.com

# Get secrets
oc get secrets

# Describe secret
oc describe secret [name]

# Delete secret
oc delete secret [name]

# Base64 decode secret
oc get secret [name] -o jsonpath='{.data.key}' | base64 -d

Resource Management

CommandDescription
oc get allList all resources in project
oc get [resource-type]List resources by type
oc get [resource] -o yamlExport resource as YAML
oc get [resource] -o jsonExport resource as JSON
oc describe [resource] [name]Show resource details
oc edit [resource] [name]Edit resource
oc apply -f [file.yaml]Apply resource from file
oc delete [resource] [name]Delete resource
oc delete -f [file.yaml]Delete from file

Troubleshooting

Debugging

# Get detailed pod information
oc describe pod [name]

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

# View pod logs with timestamps
oc logs [pod-name] --timestamps=true

# Get previous pod logs (if crashed)
oc logs [pod-name] --previous

# Describe node
oc describe node [node-name]

# Get node resource usage
oc top nodes

# Get pod resource usage
oc top pods

Common Issues

# Check pod readiness
oc get pod [name] -o jsonpath='{.status.conditions[*].{type:.type,status:.status}}'

# View pod resource limits
oc describe pod [name] | grep -A 5 "Limits\|Requests"

# Check ImagePullBackOff
oc describe pod [name] | grep -i image

# View pod startup errors
oc logs [pod-name] --tail=50

# Exec into pod for debugging
oc debug pod/[name]

Best Practices

Development

  • Use namespaces to organize projects
  • Set resource requests and limits for all pods
  • Use liveness and readiness probes
  • Store sensitive data in secrets, not ConfigMaps
  • Use deployment strategies (rolling, blue-green)
  • Tag images with versions, not latest
  • Use RBAC for access control

Production

  • Monitor resource usage with oc top
  • Use autoscaling for high-traffic applications
  • Implement pod disruption budgets
  • Configure network policies
  • Use PersistentVolumes for stateful data
  • Set up proper logging and monitoring
  • Regularly backup etcd and persistent data
  • Review security policies regularly

Useful Aliases

# Add to .bashrc or .zshrc
alias occ='oc config current-context'
alias ocgp='oc get pods'
alias ocgs='oc get svc'
alias ocgd='oc get deployments'
alias ocgr='oc get routes'
alias ocga='oc get all'
alias ocl='oc logs -f'
alias ocex='oc exec -it'

Resources


Last updated: 2026-03-30