Mastering OpenShift 4 with ‘oc’ Commands for Developers

Jimin
3 min readSep 2, 2023

--

Introduction

Openshift, developed by Red Hat, is an open-source container orchestration platform that leverages Kubernetes and helps automate the deployment, scaling, and management of application containers. It’s an essential tool for developers working in a containerized environment. One of the key components of the OpenShift is the ‘oc’ command-line interface (CLI), which is used for interacting with the OpenShift cluster, managing resources, and controlling the OpenShift environment.

In this blog post, we will go over some of the most commonly used ‘oc’ commands that every developer working with OpenShift 4 should have in their toolkit.

Getting Started

Before diving into the ‘oc’ commands, make sure you have the OpenShift CLI installed on your machine. You can download it from the OpenShift official website.

Once you have the ‘oc’ CLI installed, you can log in to your OpenShift cluster by running:

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

Essential ‘oc’ Commands

  1. Login and Logout
    oc login: Log in to the OpenShift cluster by providing a token or username and password. Example: oc login -u developer -p developer
    oc logout: Log out from the current OpenShift session.
  2. Project Management
    oc new-project <project-name>: Creates a new project. Example: oc new-project myproject
    oc projects: List all available projects.
    oc project <project-name>: Switches to another project. Example: oc project myproject
  3. Application Creation and Deployment
    oc new-app: Creates a new application. Example: oc new-app ruby+mysql
    oc status: Shows the status of the current project.
    oc get: List resources. Example: oc get pods to list all pods.
  4. Scaling Applications
    oc scale: Scales a resource. Example: oc scale dc/myapp --replicas=3 to scale the ‘myapp’ deployment config to 3 replicas.
  5. Logs and Debugging
    oc logs: Retrieves logs from a pod. Example: oc logs mypod-1-abcdef
    oc describe: Shows detailed information about a resource. Example: oc describe pod mypod-1-abcdef
  6. Resource Management
    oc create: Creates a resource. Example: oc create -f myresource.yaml
    oc delete
    : Deletes a resource. Example: oc delete pod mypod-1-abcdef
    oc edit
    : Edits a resource. Example: oc edit dc/myapp
  7. Route Management
    oc expose: Exposes a service as a route. Example: oc expose svc/myapp
    oc get routes
    : Gets all routes.
  8. Pod Management
    oc exec: Executes a command in a pod. Example: oc exec mypod-1-abcdef -- ls /
    oc rsh
    : Opens a shell session in a pod. Example: oc rsh mypod-1-abcdef
  9. Template Management
    oc process: Processes a template. Example: oc process -f mytempate.yaml | oc create -f -
  10. Resource Monitoring
    oc top: Shows metrics for resources. Example: oc top pod

Conclusion

The oc CLI is a powerful tool that can greatly optimize your workflow when working with OpenShift 4. While this post covers some of the most commonly used commands, there are many more features and options available. Make sure to explore the ocCLI further and consult the OpenShift documentation or run oc help or more oc <command> --help for more detailed information and examples.

Mastering the occommands is essential for every developer working with OpenShift 4. It will not only help in managing the applications and resources effectively but also in automating and streamlining the development process.

Happy coding!

--

--

Jimin
Jimin

Written by Jimin

DevOps engineer and tech enthusiast. Sharing tech insights to simplify the complex. Let's connect on LinkedIn! https://www.linkedin.com/in/byun-jimin/

No responses yet