OpenShift in the public cloud graphic

Red Hat OpenShift simplifies application deployment, the management and monitoring of Kubernetes clusters, and other developer tasks. The OpenShift tools, both command-line interface (CLI) and graphical user interface (GUI), cover many crucial deployment tasks. This article focuses on the oc and odo CLI commands, but touches on the GUIs as well.

The oc client

The OpenShift oc command helps you develop, build, deploy, and run applications on an OpenShift or Kubernetes cluster. The command includes an adm subcommand for administering the cluster.

oc can be downloaded from the help menu of your OpenShift web interface, from OpenShift's GitHub releases page, or from the Red Hat web site after you create a Red Hat account. There are binaries for many operating systems, including Linux, Microsoft Windows, and macOS.

The --help option (flag) offers an overview of the oc command. This option can be also used with subcommands, which can help you build the right command in your early stages of use. For example, the following command displays options for the whoami subcommand:

$ 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.

Usage:
  oc whoami [flags]

Examples:
  # Display the currently authenticated user
  oc whoami

Options:
      --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
https://console-openshift-console.apps.varsha.lab.upshift.rdu2.redhat.com

Several more examples of oc commands appear in the following subsections.

View the console

You can pull up the OpenShift console from the command line by entering:

oc whoami --show-console

Log in to an OpenShift cluster

The login subcommand connects you to one of your clusters:

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

For example:

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

Alternatively, you can use an access token to log in. Obtain a token by visiting:

https://oauth-openshift.apps.varsha.lab.upshift.rdu2.redhat.com/oauth/token/request

A login using a token looks like this:

$ oc login --token=sha256~vNVfZ0VuFK7SkveM_nGRFTS7nrfawCQnQ9FEPNScv-0 --server=https://api.varsha.lab.upshift.rdu2.redhat.com:6443

Switch to a specific project

Specify the name of your project with the following command:

$ oc project <project_name>

List existing projects

You can view the projects in your account like this:

$ oc project list
$ oc projects

Display the current project

To see which project you are logged into, enter:

$ oc project

Run applications

Here are some useful commands to use with applications.

Execute a command on a particular pod:

$ oc exec <mypod> -- <command_to_execute_in_pod>

Start a remote shell so you can enter a series of commands on a pod:

$ oc rsh <pod_name>

Debug cluster activities on a pod:

$ oc debug <pod_name>

Frequently used commands

Some more common activities are:

# See an overview of the current project
oc status 
#  -f, --follow=false: Specify if the logs should be streamed.
oc logs -f <pod_name>
# list all pods resource in ps output format
oc get pods
oc get po
# list all resources in a namespace
oc get all
# Show details of a specific resource or group of resources.
Print a detailed description of the selected resources, including related
resources such as events or controllers.
oc describe pod <pod_name>
# List all services together in ps output format
oc get services
# To display output of yaml file of a resource 
oc get <resource> <resource_name> -o yaml
# Delete all resources of the application
oc delete all -l app=<app_name>
# Delete all pods
oc delete pod <pod_name>

Scaling applications

Autoscaling increases or decreases the number of pods in the cluster to meet demand for its service. OpenShift makes autoscaling easy. For instance, the following command creates an autoscaler for the specified deployment, replica set, stateful set, or replication controller:

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

You can also specify parameters for autoscaling. For instance, the following command allows OpenShift to scale the application between 2 and 4 pods with a target CPU utilization of 80%:

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

Deploy a ConfigMap

A ConfigMap is a key-value listing used in many aspects of configuration. Normally, the ConfigMap is stored as YAML in a file and pushed to the cluster, as shown in the following command. The --from-file option can be specified multiple times in a single command to create the ConfigMap from a combination of files:

$ oc create configmap <name> --from-file=<file_name>

Mount the ConfigMap 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'

Deploying a secret

A secret is a key-value listing protected by encryption, and is used for sensitive data such as passwords. The following command creates a secret and mounts it as a volume to a deployment configuration:

$ 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'

The odo client

odo (an abbreviation of OpenShift do) is a developer-focused command that runs OpenShift applications in an expedited and automated manner. odo reduces the developer's cognitive load. This command is for application developers who wish to deploy their applications to Kubernetes easily without spending a lot of time learning the DevOps and Kubernetes procedures for deploying enterprise infrastructure. In contrast, oc focuses on operations and requires a deeper understanding of Kubernetes and OpenShift concepts.

odo can be installed from its web page and has a GitHub repository with documentation.

Getting started with odo

Here are a few commands to try in order to get familiar with odo.

You can create an application as follows. The final push subcommand builds the application, pushes it to your cluster, and deploys it on OpenShift:

$ git clone https://github.com/openshift/nodejs-ex
$ cd nodejs-ex
$ odo create nodejs
$ odo push

The watch subcommand tells odo to automatically redeploy your application after edits:


$ odo watch

You can edit your code in real time and watch as odo automatically deploys your application.

You can create a URL where clients can send HTTP requests to your application as follows:

$ odo url create myurl
$ odo push

The following commands give you more information about the cluster, such as logs or what components you've deployed:

$ odo describe
$ odo list
$ odo log

Visual Studio Code OpenShift Connector for Red Hat OpenShift: An odo GUI

The OpenShift Connector extension, also known as the Visual Studio Code OpenShift Connector for Red Hat OpenShift, is a plug-in for the VSCode IDE that interacts with Red Hat OpenShift clusters. Basically, the OpenShift Connector extension provides a GUI for odo as well as oc, combining groups of related commands into compact units. Developers can use the interface to accomplish complex tasks with minimal OpenShift experience.

To start with the connector, choose a predefined template, such as a Project, Application, or Service. The graphical interface can then build the resource and deploy it to your cluster as an OpenShift component. The OpenShift Connector can:

  • Create applications
  • Build applications
  • Debug applications
  • Deploy the applications directly to an OpenShift cluster

The latest version of OpenShift Connector is available in the VS Code Marketplace. There are several options available for different environments, so you can use the interface directly on a laptop or install it into a local OpenShift cluster. (There are limitations on Apple M1 hardware.)

Conclusion

This article should get you started on OpenShift using the command line or a GUI. You will grow in sophistication over time, and use the tools for such advanced tasks as:

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

Comments