openshift command line view

Dheeraj kumar
3 min readNov 2, 2020

Openshift Commands

Openshift Login and Configuration

#login with a user
oc login https://192.168.99.100:6443 -u developer -p developer
oc login --token=<token> --server=https://192.168.33.10:6443
#login as system admin
oc login -u system:admin
#User Information
oc whoami
oc whoami -t -> get token
#View your configuration
oc config view

Basic Commands

# list all project
oc projects
# using project
oc project <myproject>
#Use specific template
oc new-app https://github.com/name/project --template=<template>

#New app from a different branch
oc new-app --name=node-dev https://github.com/name/project.git#branchname

#Create objects from a file:
oc create -f myobject.yml -n <myproject>
oc process -f myobject.yml | oc create -f -
#Create or merge objects from file
oc apply -f myobject.yaml -n <myproject>

#Update existing object
oc patch svc mysvc --type merge --patch '{"spec":{"ports":[{"port": 8080, "targetPort": 5000 }]}}'

#Monitor Pod status
watch oc get pods

#show labels
oc get pods --show-labels

#Gather information on a project's pod deployment with node information
$ oc get pods -o wide

#Hide inactive Pods
oc get pods --show-all=false

#Display all resources
oc get all,secret,configmap
oc get dc -> to get all deployment config
oc get svc -> to get all services
oc get bc -> to get all build config
oc get routes -> to get all routes
oc get pods -> to get all pods
#Get the specific instance info
oc get route node-dev
oc get dc node-dev
pc get svc node-dev

#Get the Pod name from the Selector and rsh in it
POD=$(oc get pods -l app=myapp -o name)
oc rsh -n $POD

# uploading and downloading files in pods
oc rsync pod-hthj-1:/tmp/data.json .
oc rysnc /tmp/data.json:pod-hthj-1:/tmp
#Read resource schema
oc explain dc

creating instance

# from docker images
oc new-app mongo:4.2 --name=mongo-db
# with git url
oc new-app --name=node-dev https://github.com/name/project.git
# image streams
oc new-app --image-stream=node-dev:latest --name=node-dev
# with yaml
oc apply -f myapp.yaml
oc create -f myapp.yml
oc process -f myapp.yml | oc create -f -
oc process -f myapp.yml -p APP_NAME=helloworld -p SERVICE_NAME=node-dev | oc create -f - -> passing parameter in yaml
# output all resources in yaml file
oc new-app --name=node-dev https://github.com/name/project.git -o yaml > myapp.yaml
# with local code
oc new-app . --name=node-dev

#deleting pod forcefully in case of a deadlock with multi-container storage attached error

oc delete pod php-test-84-pcn4q — force — grace-period=0

#Build config patch update resources

oc new-build --name cookbook --binary --strategy docker --to=$IMAGE && oc patch bc/cookbook --patch ‘{“spec”:{“output”:{“to”:{ “kind”:”ImageStreamTag”,”name”:”’”$IMAGE”’”}},”resources”:{“limits”:{“memory”:”4Gi”}}}}’ && oc start-build cookbook --from-dir=. && oc logs -f bc/cookbook

#Using Build config docker strategy in the current directory

oc new-build --name cookbook --binary --strategy docker --to=devops:v2 && oc start-build cookbook --from-dir=.

#exec remote commands in pods

oc exec podname command
oc exec node-1-yhuek date&&Echo "helloworld"
oc exec nodejs-2-nh25m -it -- ls -al

#patching Build Config

$ oc patch bc/cookbook --patch '{"spec":{"resources":{"limits":{"memory":"1Gi"}}}}'

#Accessing image from namespace b to namespace A

oc policy add-role-to-user \
system:image-puller system:serviceaccount:project-a:default \
--namespace=project-b

#creating routes { edge/ passthrough }

oc create route edge --service=frontend frontend
oc create route edge --service=frontend --hostname=www.frontend.com
oc create route passthrough --service=frontend frontend

#Setting weights to routes to distribute load/traffic

$ oc set route-backends ab-example ab-example-a=198 ab-example-b=2

#Routes IP Whitelisting and timeouts annotations

haproxy.router.openshift.io/ip_whitelist: 180.5.61.153 192.168.1.0/24 10.0.0.0/8haproxy.router.openshift.io/timeout: 5500ms

#Route Loadbalancing annotations

haproxy.router.openshift.io/balance: roundrobin
haproxy.router.openshift.io/balance: source
haproxy.router.openshift.io/balance: leastconn

#Pod network sharing across different Projects/namespaces.

mongo.projecta.svc.cluster.local

storage class

#create a PersistentVolumeClaim and attaching with the deployment configoc set volume dc/node-dev --add --name=my-shared-storage \-t pvc --claim-mode=ReadWriteMany --claim-size=1Gi \--claim-name=my-shared-storage --claim-class=ocs-storagecluster-cephfs \--mount-path=/opt/app-root/src/uploaded #List storage classes
oc get sc

Nodes

#Get Nodes list
oc get nodes
#Check on which Node your Pods are running
oc get pods -o wide
#Schedule an application to run on another Node
oc patch dc myapp -p '{"spec":{"template":{"spec":{"nodeSelector":{"kubernetes.io/hostname": "ip-10-0-0-74.acme.compute.internal"}}}}}'
#List all pods which are running on a Node
oc adm manage-node node1.local --list-pods
#Add a label to a Nodeoc label node node1.local mylabel=myvalue#Remove a label from a Nodeoc label node node1.local mylabel-#delete pod forcefully
oc delete pod node-test --force --grace-period=0

--

--