OpenShift CLI (oc) Setup & Essential Commands
Learn how to install, configure, and use the OpenShift CLI (oc) with practical examples, developer workflows, and best practices.
Introduction
The OpenShift CLI (oc) is the primary command-line tool used to interact with an OpenShift cluster. While the OpenShift Web Console provides a graphical interface, professional developers, DevOps engineers, SREs, and platform engineers rely on the oc CLI for automation, deployments, troubleshooting, and day-to-day cluster management.
Think of oc as the remote control for your OpenShift cluster.
Using oc, you can:
- Connect to a cluster
- Create projects
- Deploy applications
- Scale workloads
- View logs
- Debug pods
- Manage secrets
- Configure routes
- Automate deployments
- Perform administrative tasks
Learning Objectives
By the end of this article, you will understand:
- What the
ocCLI is - How
ocdiffers fromkubectl - How to install
oc - How to connect to an OpenShift cluster
- Common authentication methods
- Essential commands every developer should know
- Managing projects, pods, deployments, and services
- Debugging applications
- Best practices for production environments
Why Use the oc CLI?
Although the OpenShift Web Console is user-friendly, many tasks are faster and more efficient using the CLI.
Benefits include:
- Faster deployments
- Easy scripting and automation
- CI/CD integration
- Easier troubleshooting
- Repeatable operations
- Infrastructure as Code workflows
OpenShift CLI Architecture
flowchart LR
DEV["Developer"]
OC["oc CLI"]
API["OpenShift API Server"]
CLUSTER["OpenShift Cluster"]
DEV --> OC
OC --> API
API --> CLUSTER
The oc command communicates directly with the OpenShift API Server, which performs operations on the cluster.
oc vs kubectl
| Feature | oc | kubectl |
|---|---|---|
| Kubernetes Commands | ✅ | ✅ |
| OpenShift Resources | ✅ | ❌ |
| Routes | ✅ | ❌ |
| BuildConfig | ✅ | ❌ |
| DeploymentConfig | ✅ | ❌ |
| Image Streams | ✅ | ❌ |
| Templates | ✅ | ❌ |
A useful rule of thumb:
- Use
kubectlfor standard Kubernetes resources. - Use
ocwhen working with OpenShift-specific features.
Installing the OpenShift CLI
The CLI is available for:
- macOS
- Windows
- Linux
After installation, verify it:
oc version
Example output:
Client Version: 4.x.x
Kubernetes Version: v1.xx.x
Logging In
Log in using the cluster URL:
oc login https://api.cluster.example.com:6443
Or use an access token:
oc login --token=<TOKEN> --server=https://api.cluster.example.com:6443
Verify Connection
oc whoami
oc whoami --show-server
These commands confirm your identity and the cluster you're connected to.
Working with Projects
Projects are OpenShift's logical isolation mechanism.
List projects:
oc projects
Switch projects:
oc project payments-dev
Create a new project:
oc new-project payments-dev
Basic Resource Commands
List pods:
oc get pods
List services:
oc get svc
List routes:
oc get routes
List deployments:
oc get deployments
List everything:
oc get all
Describe Resources
View detailed information:
oc describe pod payment-api
Useful for diagnosing scheduling issues, events, and container status.
Viewing Logs
Display pod logs:
oc logs payment-api
Follow logs in real time:
oc logs -f payment-api
Execute Commands Inside a Pod
oc exec -it payment-api -- /bin/bash
This opens an interactive shell for debugging.
Port Forwarding
Access an application running inside the cluster locally:
oc port-forward pod/payment-api 8080:8080
Then browse:
http://localhost:8080
Scaling Applications
Increase replicas:
oc scale deployment payment-api --replicas=3
Verify:
oc get pods
Rolling Restart
Restart a deployment:
oc rollout restart deployment payment-api
Check rollout status:
oc rollout status deployment payment-api
Applying Configuration
Create or update resources from YAML:
oc apply -f deployment.yaml
Delete resources:
oc delete -f deployment.yaml
Managing Secrets
Create a secret:
oc create secret generic db-secret \
--from-literal=username=admin \
--from-literal=password=password123
List secrets:
oc get secrets
Managing ConfigMaps
Create:
oc create configmap app-config \
--from-literal=env=dev
View:
oc get configmaps
Common Developer Workflow
flowchart LR
CODE["Write Code"]
BUILD["Build Image"]
PUSH["Push Image"]
DEPLOY["oc apply"]
VERIFY["Verify Pods"]
CODE --> BUILD
BUILD --> PUSH
PUSH --> DEPLOY
DEPLOY --> VERIFY
Typical Spring Boot Deployment Workflow
- Develop locally
- Build Docker image
- Push image to registry
- Deploy with
oc apply - Verify pods
- View logs
- Test application
- Scale if needed
Troubleshooting Checklist
When an application fails:
- Check pod status
oc get pods
- Describe the pod
oc describe pod <pod-name>
- View logs
oc logs <pod-name>
- Check events
oc get events
- Verify deployment
oc get deployment
Best Practices
- Always work in the correct project.
- Use declarative YAML (
oc apply) instead of imperative commands. - Store configuration in ConfigMaps.
- Store credentials in Secrets.
- Use labels consistently.
- Monitor rollout status after deployments.
- Avoid making production changes manually.
Real-World Example
Imagine a banking application:
- Developer pushes code to Git.
- CI/CD pipeline builds a container image.
- Image is stored in the registry.
- Deployment is updated using
oc apply. - OpenShift creates new pods.
- Service routes traffic.
- Users access the updated application through a Route.
This entire lifecycle can be managed from the oc CLI.
Interview Questions
- What is the purpose of the
ocCLI? - How is
ocdifferent fromkubectl? - How do you log in to an OpenShift cluster?
- How do you switch projects?
- How do you view pod logs?
- How do you execute commands inside a pod?
- How do you scale a deployment?
- What is the difference between
oc applyandoc create? - How do you restart a deployment?
- What commands do you use to troubleshoot a failing pod?
Summary
The oc CLI is one of the most important tools in the OpenShift ecosystem. Mastering it enables developers to deploy applications, manage resources, troubleshoot issues, automate workflows, and interact efficiently with OpenShift clusters. For enterprise teams building cloud-native applications, proficiency with oc is an essential skill that complements Kubernetes knowledge and forms the foundation for advanced OpenShift administration.
Comments
Share a question, correction, or practical insight about this article.
Checking login status...
Loading approved comments...