Full Stack • Java • System Design • Cloud • AI Engineering

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 oc CLI is
  • How oc differs from kubectl
  • 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 kubectl for standard Kubernetes resources.
  • Use oc when 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

  1. Develop locally
  2. Build Docker image
  3. Push image to registry
  4. Deploy with oc apply
  5. Verify pods
  6. View logs
  7. Test application
  8. Scale if needed

Troubleshooting Checklist

When an application fails:

  1. Check pod status
oc get pods
  1. Describe the pod
oc describe pod <pod-name>
  1. View logs
oc logs <pod-name>
  1. Check events
oc get events
  1. 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

  1. What is the purpose of the oc CLI?
  2. How is oc different from kubectl?
  3. How do you log in to an OpenShift cluster?
  4. How do you switch projects?
  5. How do you view pod logs?
  6. How do you execute commands inside a pod?
  7. How do you scale a deployment?
  8. What is the difference between oc apply and oc create?
  9. How do you restart a deployment?
  10. 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.

Loading likes...

Comments

Share a question, correction, or practical insight about this article.

Loading approved comments...