Basic Commands¶
Essential OpenShift CLI (oc) commands for managing your applications and troubleshooting issues.
Introduction¶
The OpenShift CLI (oc) is your primary tool for interacting with your cluster. Since OpenShift is built on Kubernetes, you can also use kubectl for standard Kubernetes operations.
CLI Tool Options¶
OpenShift CLI (oc):
- Superset of
kubectlwith all Kubernetes functionality - Includes OpenShift-specific features (routes, projects, builds)
- Recommended for OpenShift environments
Kubernetes CLI (kubectl):
- Standard Kubernetes CLI
- Works for all basic Kubernetes operations
- Use if you prefer consistency across different Kubernetes distributions
Command Compatibility
All commands in this guide work with both oc and kubectl unless specifically noted. Replace oc with kubectl in any standard Kubernetes command. OpenShift-specific features like routes and projects require oc.
Getting Started¶
# Login to cluster
oc login -w https://api-<cluster-name>.apps.intilitycloud.com
# Check current user and project
oc whoami
oc project
# Create and switch to a new project
oc new-project my-app
# List all projects
oc projects
Viewing Resources¶
# View specific resource types
oc get pods
oc get deployments
oc get services
oc get routes
# Get detailed information about a resource
oc describe pod <pod-name>
# Watch resources for changes
oc get pods -w
Managing Applications¶
# Create resources from YAML files
oc apply -f deployment.yaml
# Quick deployment creation
oc create deployment nginx --image=nginxinc/nginx-unprivileged:alpine
# Expose deployment as a service
oc expose deployment nginx --port=80 --target-port=8080
# Create external route (OpenShift-specific)
oc expose service nginx
# Scale applications
oc scale deployment nginx --replicas=3
Troubleshooting¶
# View application logs
oc logs <pod-name>
oc logs -f <pod-name> # Follow logs
oc logs deployment/<deployment-name> # All pods in deployment
# Get shell access to pod
oc exec -it <pod-name> -- /bin/bash
# Copy files to/from pods
oc cp <pod-name>:/app/logs/app.log ./app.log
# Port forward for local testing
oc port-forward service/<service-name> 8080:80
# Check events for troubleshooting
oc get events --sort-by='.lastTimestamp'
Updates and Rollbacks¶
# Update deployment image
oc set image deployment/nginx nginx=nginxinc/nginx-unprivileged:1.21
# Monitor rollout
oc rollout status deployment/nginx
# Rollback if needed
oc rollout undo deployment/nginx
Configuration¶
# Create ConfigMap and Secret
oc create configmap app-config --from-file=config.yaml
oc create secret generic app-secret --from-literal=api_key=secret123
# Set environment variables from ConfigMap/Secret
oc set env deployment/nginx --from=configmap/app-config
oc set env deployment/nginx --from=secret/app-secret
Useful Shortcuts¶
# Resource shortcuts
oc get po # pods
oc get svc # services
oc get deploy # deployments
# Use labels for filtering
oc get pods -l app=nginx
# Delete resources
oc delete deployment,service,route -l app=nginx
Next Steps¶
Now that you know the essential commands, explore these topics:
- Deploy your first application - Put these commands into practice
- Navigate OpenShift console - GUI alternative to CLI commands
- Deploy with GitOps - Automated deployments