A Primer on OpenShift CLI Tools

The command-line interface (CLI) is an effective text-based user interface (UI). Today, many users rely on graphical user interfaces and menu-driven interactions, but some programming and maintenance tasks may not have a GUI, or at times, may experience slowness. In such scenarios, command-line interfaces can be used.

When working on the OpenShift Container Platform, a variety of tasks can be performed using CLI tools aside from managing clusters, applications, and so on.

These tasks include:

  • Gathering information from cluster for debugging issues
  • Working directly with project source code
  • Managing project content when web console is not accessible

oc Client

OpenShift client can be easily downloaded on MacOS via binary or web console under Help icon (?) -> Command line tools.

This client helps to develop, build, deploy, and run applications on any OpenShift or Kubernetes cluster. It also includes the administrative commands for managing a cluster under the adm subcommand.

The help flag with oc Command lists all the basic and advanced commands along with sub-commands for application management and troubleshooting/debugging.

➜  ~ oc --help

--help can be used with sub-commands as well, which assists well in creation of a command for first-time cli users.

For example, to get the web console URL via CLI, the command can be framed as below:

➜  ~ oc whoami --help
Show information about the current session

 The default options for this command will return the currently authenticated user name or an empty string.  Other flags
support returning the currently used token or the user context.

  oc whoami [flags]

  # Display the currently authenticated user
  oc whoami

      --show-console=false: If true, print the current server's web console URL
  -c, --show-context=false: Print the current user context name
      --show-server=false: If true, print the current server's REST API URL
  -t, --show-token=false: Print the token the current session is using. This will return an error if you are using a
different form of authentication.

Use "oc options" for a list of global command-line options (applies to all commands).
➜  ~ oc whoami --show-console

The command oc whoami --show-console displays the web console URL via CLI.

similarly, oc commands can be framed as needed for multiple operations.

A few examples of oc commands are as follows:

1. Log in to the OpenShift cluster:

oc login -u <username> -p <password> <servername>

Example : oc login -u kubeadmin -p asdfghjkliuytr https://upi-o.varsha.lab.upshift.rdu2.redhat.com:6443

Alternatively, a token can also be used to log in to the OpenShift cluster.

Example :

➜  ~ oc login       
You must obtain an API token by visiting https://oauth-openshift.apps.varsha.lab.upshift.rdu2.redhat.com/oauth/token/request

Display the token by visiting the URL mentioned previously.

# Log in with this token
oc login --token=sha256~vNVfZ0VuFK7SkveM_nGRFTS7nrfawCQnQ9FEPNScv-0 --server=https://api.varsha.lab.upshift.rdu2.redhat.com:6443


2. Switch to a specific project:

oc project <project_name>

3. List existing projects:

oc project list

oc projects

4. Display the currently logged-in project:

oc project

5. Access running applications:

oc rsh <pod_name>
oc exec <mypod> -- <command_to_execute_in_pod>
oc debug <pod_name>

6. Frequently used commands:

oc status
oc logs -f <pod_name>
oc get pods 
oc get po
oc get all
oc describe pod <pod_name>
oc get services 
oc delete all -l app=<app_name>
oc delete pod <pod_name>
oc get <resource> <resource_name> -o yaml 

7. Scaling applications:

oc scale dc <resource_name> --replicas=<count>

#autoscale looks up a deployment, replica set, stateful set, or replication controller by
#name and creates an autoscaler that uses the given resource as a reference. An
#autoscaler can automatically increase or decrease number of pods deployed within
#the system as needed.

#Auto scale a deployment, with the number of pods between 2 and 4, target CPU utilization at 80%

oc autoscale deployment <resource_name> --min=2 --max=4 --cpu-percent=80%

8. Create a config map file and mount it as a volume to a deployment/deployment config:

oc create configmap <name> --from-file=<file name> --from-file=<file name{If multiple files}>

#Mount the config map into the pods via the deployment controller or deployment:
oc set volume deployment/<resource_name> --add --name=<config_map_volume> -m <mount_path> -t configmap --configmap-name=<name> --default-mode="0777"

9. Create a secret from the CLI and mount it as a volume to a deployment config:

oc create secret generic <secret_name> --from-literal=username=myuser --from-file=<file_name> 

oc set volume dc/<resource_name> --add --name=<secret_volume> -m <mount_path> -t secret --secret-name=<secret_name> --default-mode="0755" 

odo Client

Red Hat OpenShift Developer CLI (odo) is a CLI tool for running OpenShift applications in a fast and automated manner. It is a developer-focused CLI tool.

odo reduces the cognitive load as compared to the oc client, which is more operations-focused and requires a deep-understanding of Kubernetes and OpenShift concepts.

odo focuses on application developers, whereas oc is used to manage production OpenShift clusters.

Odo is for application developers wishing to deploy to Kubernetes easily without spending time on learning DevOps and Kubernetes in order to deploy enterprise infrastructure.

Odo can be easily installed following https://odo.dev/docs/getting-started/installation. Find more information at https://github.com/redhat-developer/odo

Getting Started

1. Create a new application:

git clone https://github.com/openshift/nodejs-ex 
cd nodejs-ex

odo create nodejs

odo push

# push deploys application. Running 'odo push' pushed the source code, built the application and deployed it on OpenShift.

2. To edit code in real time and watch as odo automatically deploys your application, use the following command:

odo watch

3. To access application, create a URL:

odo url create myurl

odo push

3. More information such as logs or what components you’ve can be accessed with these commands:

odo describe

odo list

odo log

GUI for odo

OpenShift Connector extension provides an end-to-end developer experience for Red Hat OpenShift. This extension aka Visual Studio Code OpenShift Connector for Red Hat OpenShift, is a plug-in for IDE that can be used to interact with Red Hat OpenShift clusters.

It is a GUI for the OpenShift Do (odo) utility which aggregates the OpenShift CLI (oc) commands into compact units.

With OpenShift Connector, new developers can create applications and run them on the cloud regardless of whether they have OpenShift experience.

Instead of using the OC command line, the user creates a new component or service using a predefined template, such as a project, an application, or a service, and then deploys it to their cluster as an OpenShift Component similar to what odo CLI does.

You can use OpenShift Connector in an IDE for the following tasks :

  • Create
  • Build
  • Debug
  • Deploy the applications directly to a running OpenShift cluster

The latest version of OpenShift Connector is available in VS Code Marketplace. There are several infrastructure options available, including Laptop to install an OpenShift cluster locally using Red Hat CodeReady Containers with an exception of CRC unsupported on Apple M1 hardware.


Leave a Comment