✳️OpenShift Essentials : CLI Commands

Sandeep KaleSandeep Kale
3 min read

Let's take a look at key OpenShift components and objects, along with relevant CLI commands for interacting with them.

Introduction

The OpenShift Command Line Interface (CLI), commonly known as oc, is a powerful tool that allows developers and administrators to interact with OpenShift clusters. Whether you are deploying applications, managing resources, or troubleshooting issues, the oc CLI provides a comprehensive set of commands to facilitate your tasks. In this guide, we will introduce the OpenShift CLI, cover basic and advanced commands, and provide practical examples of managing OpenShift resources via CLI.

✅Getting Started with OpenShift CLI Installation

Before you can use the oc CLI, you need to install it on your local machine. The installation process varies depending on your operating system.

  • Linux: Download the appropriate binary from the OpenShift CLI Downloads page, extract it, and move it to a directory in your PATH.

  • Windows: Download the oc CLI from the OpenShift CLI Downloads page and follow the instructions to install it.

  • MacOS: Use Homebrew to install the oc CLI.

✅Logging In

To interact with an OpenShift cluster, you need to log in using the oc CLI. You can obtain the login token and server URL from the OpenShift Web Console.

oc login --token=<your-token> --server=<your-server>

✅Basic Commands

Getting Cluster Information

  • View Cluster Status:
oc status
  • Get Cluster Info:
oc cluster-info

Managing Projects

Projects in OpenShift are Kubernetes namespaces with additional annotations. They provide a way to organize and manage resources.

  • List Projects:
oc projects
  • Create a New Project:
oc new-project my-project
  • Switch to a Project:
oc project my-project

Working with Pods

  • List Pods:
oc get pods
  • Describe a Pod:
oc describe pod <pod-name>
  • Get Logs from a Pod:
oc logs <pod-name>
  • Execute a Command in a Pod:
oc exec <pod-name> -- <command>

Deployments and Builds

  • Create a New App:
oc new-app <source-code-url>
  • Get Builds:
oc get builds
  • Start a New Build:
oc start-build <build-config>

✅Advanced Commands

Resource Management

  • Scale a Deployment:
oc scale --replicas=<number> deployment/<deployment-name>
  • Apply a Configuration File:
oc apply -f <file.yaml>

Networking

  • Expose a Service:
oc expose svc/<service-name>
  • Create a Route:
oc create route edge --service=<service-name>

Security and Policies

  • Create a Role Binding:
oc create rolebinding <rolebinding-name> --clusterrole=<role> --user=<user>
  • Get Security Context Constraints:
oc get scc

Troubleshooting

  • Get Events:
oc get events
  • Debug a Node:
oc debug node/<node-name>

💡Conclusion:

The OpenShift CLI is an essential tool for managing OpenShift clusters efficiently. By mastering both basic and advanced oc commands, you can efficiently manage your OpenShift cluster, troubleshoot issues, and automate tasks. Remember that while the CLI offers powerful control, OpenShift also provides a user-friendly web console for those who prefer a graphical interface.

0
Subscribe to my newsletter

Read articles from Sandeep Kale directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sandeep Kale
Sandeep Kale