openshift command line view

Openshift Commands

Openshift Login and Configuration

#login with a user
oc login -u developer -p developer
oc login --token=<token> --server=
#login as system admin
oc login -u system:admin
#User Information
oc whoami
oc whoami -t -> get token
#View your configuration
oc config view
# list all project
oc projects
# using project
oc project <myproject>
#Use specific template
oc new-app --template=<template>

#New app from a different branch
oc new-app --name=node-dev

#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
# from docker images
oc new-app mongo:4.2 --name=mongo-db
# with git url
oc new-app --name=node-dev
# 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 -o yaml > myapp.yaml
# with local code
oc new-app . --name=node-dev
oc delete pod php-test-84-pcn4q — force — grace-period=0
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
oc new-build --name cookbook --binary --strategy docker --to=devops:v2 && oc start-build cookbook --from-dir=.
oc exec podname command
oc exec node-1-yhuek date&&Echo "helloworld"
oc exec nodejs-2-nh25m -it -- ls -al
$ oc patch bc/cookbook --patch '{"spec":{"resources":{"limits":{"memory":"1Gi"}}}}'
oc policy add-role-to-user \
system:image-puller system:serviceaccount:project-a:default \
oc create route edge --service=frontend frontend
oc create route edge --service=frontend
oc create route passthrough --service=frontend frontend
$ oc set route-backends ab-example ab-example-a=198 ab-example-b=2 5500ms roundrobin source leastconn
#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
#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":{"": "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

A DevOps who is passionate about Autom@tion.