Openshift 4 CLI Common Command Guide: Part 1

Show OpenShift Version

[kni@bastion ~]$ oc get clusterversion
NAME      VERSION   AVAILABLE   PROGRESSING   SINCE   STATUS
version   4.6.4     True        False         6d2h    Cluster version is 4.6.4

Accessing the Web Console

Once your deployment is complete, you can use the following command to determine the web console url

[kni@bastion ~]$ oc whoami --show-console
https://console-openshift-console.apps.ocp.lab.localdomain

By default, your username for the console is kubeadmin, use the command below to obtain your password. Note, your cluster directory may not be clusterconfigs however this is the default

[kni@bastion ~]$ cat clusterconfigs/auth/kubeadmin-password 
KLNf6-JEjPC-A6cha-PJfft

Working with Nodes

Show nodes.

[kni@bastion ~]$ oc get nodes
NAME       STATUS   ROLES           AGE    VERSION
master-0   Ready    master,worker   6d3h   v1.19.0+9f84db3
master-1   Ready    master,worker   6d3h   v1.19.0+9f84db3
master-2   Ready    master,worker   6d3h   v1.19.0+9f84db3

Show baremetal hardware nodes.

Oops I have errors..

[kni@bastion ~]$ oc get bmh -n openshift-machine-api
NAME       STATUS   PROVISIONING STATUS      CONSUMER             BMC                     HARDWARE PROFILE   ONLINE   ERROR
master-0   OK       externally provisioned   ocp-d9z87-master-0   ipmi://10.1.99.2:6230                      true     
master-1   OK       externally provisioned   ocp-d9z87-master-1   ipmi://10.1.99.2:6231                      true     
master-2   OK       externally provisioned   ocp-d9z87-master-2   ipmi://10.1.99.2:6232                      true     
worker-0   error    registration error                            ipmi://10.1.99.2:623                       true     Failed to get power state for node c8105b63-3697-419c-9cab-fdfa9985411a. Error: IPMI call failed: power status.
worker-1   error    inspecting                                    ipmi://10.1.99.2:6234                      true     Introspection timeout

Delete a baremetal node.

[kni@bastion ~]$ oc delete bmh worker-0 -n openshift-machine-api
baremetalhost.metal3.io "worker-0" deleted

Working with Projects

Show Current Project

[kni@bastion ~]$ oc project
Using project "openshift-machine-api" on server "https://api.ocp.lab.localdomain:6443".

Create a New Project

[kni@bastion ~]$ oc new-project testproject
Now using project "testproject" on server "https://api.ocp.lab.localdomain:6443".

Change Current Project

If the project does not already exist, you will get an error

[kni@bastion ~]$ oc project testproject
Now using project "testproject" on server "https://api.ocp.lab.localdomain:6443".

Show Status of Current Project

[kni@bastion ~]$ oc status
In project testproject on server https://api.ocp.lab.localdomain:6443

You have no services, deployment configs, or build configs.
Run 'oc new-app' to create an application.

Delete a Project

[kni@bastion ~]$ oc delete project testproject
project.project.openshift.io "testproject" deleted

List all Projects

[kni@bastion ~]$ oc get projects
NAME                                               DISPLAY NAME   STATUS
default                                                           Active
kube-node-lease                                                   Active
kube-public                                                       Active
kube-system                                                       Active
openshift                                                         Active
openshift-apiserver                                               Active
openshift-apiserver-operator                                      Active
--trunc--

Working with Pods

Show all Pods in a Particular Namespace

oc get pods will shows you the pods in your current namespace. Use -n to specify a pod by name. Use -A to specify all pods.

[kni@bastion ~]$ oc get pod -n openshift-dns
NAME                READY   STATUS    RESTARTS   AGE
dns-default-64gcs   3/3     Running   0          4d1h
dns-default-dttgz   3/3     Running   0          4d1h
dns-default-jwmdx   3/3     Running   0          4d1h

Wide listing of Pods in a Particular Namespace

-o wide allows you to see the pod IP address and the node where it is running.

[kni@bastion ~]$ oc get pods -n openshift-console -o wide
NAME                         READY   STATUS    RESTARTS   AGE    IP            NODE       NOMINATED NODE   READINESS GATES
console-65c4b9c45b-gts2h     1/1     Running   0          4d     10.129.0.58   master-1   <none>           <none>
console-65c4b9c45b-jqrhp     1/1     Running   0          4d     10.128.0.44   master-0   <none>           <none>
downloads-65c97dd5b9-mwpfd   1/1     Running   0          4d1h   10.129.0.53   master-1   <none>           <none>
downloads-65c97dd5b9-np5hh   1/1     Running   0          4d1h   10.130.0.40   master-2   <none>           <none>

Show Pod Resource Usage

The command below will allow you to see number of Cores and Memory used per pod. Measured in millicores (# of host cores * 1000) and bytes.

[kni@bastion ~]$ oc adm top pods -A
NAMESPACE                                          NAME                                                      CPU(cores)   MEMORY(bytes)   
openshift-apiserver                                apiserver-c95cd4bfd-92h9s                                 20m          207Mi           
openshift-apiserver                                apiserver-c95cd4bfd-kfkd7                                 22m          243Mi           
openshift-apiserver                                apiserver-c95cd4bfd-nbm26                                 24m          227Mi           
openshift-apiserver-operator                       openshift-apiserver-operator-54ff4fb46f-jnxzn             16m          98Mi            
openshift-authentication                           oauth-openshift-8bcb6778d-lnqvj                           4m           49Mi            
openshift-authentication                           oauth-openshift-8bcb6778d-svffg                           3m           37Mi            
openshift-authentication-operator                  authentication-operator-5687669dcd-5vrmk                  29m          102Mi           
openshift-cloud-credential-operator                cloud-credential-operator-5bc7bbcdc5-w2hwt                2m           98Mi            
openshift-cluster-machine-approver                 machine-approver-6bbc78c46b-mmzdf                         0m           49Mi            
openshift-cluster-node-tuning-operator             cluster-node-tuning-operator-57d7f9b947-xkrd8             0m           25Mi            

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.