About
As you progress in your journey of mastering OpenShift, you will find yourself using the command line more and more. Given that scripting a solution is the key component of creating automated and reproducible results, command-line expertise becomes paramount to elegant solutions.
In short: The command line makes life easier.
This cheat sheet covers 12 essential commands for the OpenShift command line. Some activities you’ll return to again and again, such as copying files to a pod or a Persistent Volume Claim (PVC). Others are more obscure operations, such as editing a deployment from your local machine. Some commands are more developer-centric, like an endless curl loop that lets you watch a rolling update in real time.
While this cheat sheet is by no means exhaustive, it has several must-have commands and—more importantly—can serve to inspire you to create your own library of commands and scripts. You’ll learn how to:
- Install the OpenShift command-line interface (CLI).
- Log in to the Developer Sandbox for Red Hat OpenShift.
- Create a new app from an image.
- Create a new app from source code.
- Get the route for a deployment.
- Get pods for a deployment.
- Edit a deployment.
- Enter debug mode.
- Run a command-line infinite curl loop.
- Copy a file to a pod.
- See all objects with a specific label.
- Delete all objects with a specific label.
Excerpt
1. Install the OpenShift command-line interface
Red Hat OpenShift features a web-based dashboard for configuration and operation. It also supports a command-line interface, oc
, that allows you to control clusters via a terminal session.
The power of the oc
command means you have complete control over your cluster, objects, and more. Combining that power with other scripting elements gives developers and operations the ability to create reproducible results via commands and scripts.
All of this begins when you install the oc
command on your PC, whether it be on Linux, macOS, or Windows.
You will need to log into your OpenShift sandbox in your browser. This lands you at the dashboard.
Before diving into the OpenShift command-line interface (CLI), the CLI must be installed on your PC. This, ironically, is best done by starting at the OpenShift web-based dashboard. After opening the dashboard, follow these steps:
- Find the help icon (the question mark) in the upper-right corner and click it.
- From the drop-down menu, select the Command Line Tools option.
- Select the appropriate download for your operating system and hardware and save the file to a convenient location.
With the installation file downloaded, expand the compressed ZIP file and move the executable file into a directory that is in your system path.
For Windows PowerShell, run as Administrator and use the following commands:
Expand-Archive oc.zip
cd .\oc\
mkdir $ENV:ProgramFiles\OpenShift-Client
Move-Item oc.exe $ENV:ProgramFiles\OpenShift-Client\
$current_path = (Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name "PATH").path
$new_path = “$current_path;$ENV:ProgramFiles\OpenShift-Client”
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name path -Value $new_path
Close the terminal and open a new session to fetch the new system Path.
For macOS, use the following commands:
unzip oc.zip
sudo mv oc /usr/local/bin
For Linux, use the following commands:
tar xvf oc.tar
sudo mv oc /usr/bin
You can now run the oc version
command to prove that the installation was successful, such as the following example:
PS C:\Users\foo> oc version
Expected output:
Client Version: 4.14.0-202310201027.p0.g0c63f9d.assembly.stream-0c63f9d
Kustomize Version: v5.0.1
error: You must be logged in to the server (Unauthorized)