Get started with Red Hat OpenShift using only 8 commands

Red Hat OpenShift is a container platform for the development, deployment and management of applications. It is based on Kubernetes, which means if you already know how to use the Kubernetes commands, you will not have any problem to use OpenShift. In fact, it’s basically the same. Of course it is beneficial to learn a minimum about OpenShift and its architecture but when you dive into a project you learn much more by doing hands-on actions. At the beginning you can be overwhelmed by the number of commands and parameters described in the documentation.

That is why I will present the 8 most useful OpenShift commands that will give you sufficient knowledge to get started in managing a project. I am not pretending that you will be able to understand everything on how to use OpenShift. Those are the commands I mostly used and learned when I had to first discover and dive into an OpenShift project.

As a prerequisite, you need to have the OpenShift CLI installed. It can be done by simply downloading a binary or by applying an RPM. More information on the official documentation here.

Then the only command that you will need are those 2 letters: oc (standing for OpenShift CLI) followed by the action you want to process.

1) oc project

The first one is a basic one, it will indicates the current project activated in your environment.

> oc project
Using project "default" on server "hostname:443"

You can also select the project you want by specifying the name

> oc project mediumproject
Now using project "mediumproject" on server "hostname:443".

2) oc get

With OpenShift you can manage multiple resources or objects like Pods, Nodes, Projects, Services, Routes, Deployment, Persistent Volumes and some others. This will be your number one command to check if your pods are running, what are your routes or to get the name of your deployment instance.

> oc get <object_type>

Here are a few examples :

> oc get pods
mediumpod 1/1 Running 0 19m
> oc get deployments
deployment1 1/1 1 1 19m
deployment2 1/1 1 1 19m

3) oc describe

The name is implicit here, it will describe your object. You need to specify what kind of resource you want to describe (“pod” for example) and also mention the name of this resource. The name is the one that you can obtain from using oc get.

There are plenty of reasons to use it. For example, when you want to observe the current progress of a pod initialization, or to check what is wrong if a pod is taking quite some time to run.

Describing a pod will give a lot of informations regarding the deployment, the different containers images, the mounted volumes etc… The last lines should give “interactive” informations about the containers events.

> oc describe <object_type> <object_name>

Example :

> oc describe pod mediumpod
Type Reason Age From Message
Normal Pulled 41m kubelet, worker0 Pulling image "im1"
Normal Created 41m kubelet, worker0 Created container container1
Normal Started 41m kubeletm worker0 Started Container container1

Normal Pulled 40m kubelet, worker0 Pulling image "im2"
Normal Created 40m kubelet, worker0 Created container container2
Normal Started 40m kubeletm worker0 Started Container container2

4) oc create

This one lets you create one or multiple objects based on a configuration file. You can provide information about deployments, pods, replica sets and so on. The main point here of course will be to create and define properly this configuration file.

> oc create -f <filepath>
> oc create -f mediumdeploy.yaml

To have some informations about how to write this configuration file, check the OpenShift documentation here : The version here is 3.7 but is still valid to get some templates.

5) oc edit

You can edit some objects which are already running using this command. This is useful if you want to try some parameters or configurations like the number of pods required. However some resources may be killed and it will start new ones based on the changes you made.

> oc edit <object_type> <object_name> 

Let’s say you already created an application and you want to modify one of the container tag inside a pod or change the amount of memory. One way to do it is by editing the deployment or the pod object. It will give you the yaml configuration file that you’ll be able to modify and overwrite.

> oc edit deployment deployment1
> oc edit pod mediumpod

6) oc delete

If you want to delete some objects or just want a pod to restart automatically use this command. If you want to delete permanently a Pod be careful to also delete the associated Replica Set objects. Otherwise the pod will keep restarting after being deleted (which is what we want eventually).

> oc delete <object_type> <object_name>

Examples :

> oc delete replicaset mediumrs
> oc delete pod mediumpod

7) oc logs

This one give you the ability to check the logs of a given objects. If one of your pod is taking too long to start or does not initialize, this is a good way to debug and check what is wrong.

> oc logs <object_name>
> oc logs mediumpod 
// If the pod has multiple containers.
> oc logs mediumpod -c container1

Tip : Sometimes the logs are too big to be displayed in a shell. An easy solution is to store the logs into a file.
oc logs mediumpod >> mediumpod.log Then you can work with this one using any editor like vim which is also more convenient.

Tip 2 : If you execute the command without specifying the container name like oc logs mediumpod it will give you all the possible containers name you can use in this pod. If only one container is running in a pod it will be the default one.

8) oc exec

This command is the most interesting one to interact with some pods you created. If you know the docker exec command this is basically the same, except that you’re dealing with a pod so you have to specify a container name. It comes helpful when you want to debug or check your container.

> oc exec <pod_name> -c <container_name> <command>

One essential parameter you can use is the-it allowing you to run commands interactively in your container.

> oc exec -it mediumpod -c container1 bash

Of course you can not do everything only with those 8 commands and from time to time you will have to search in the documentation for new commands. I could have mentionned oc login or oc rsync (to copy/paste files in pods) for example, but those are commands which are easily found when you need them.

Thanks for reading and I hope it helped you by reducing the number of commands you need to get started with OpenShift.

OpenShift CLI documentation :

You may also like...

Leave a Reply

Your email address will not be published.