Using the oc Command (with examples)
The oc
command is a powerful tool that allows developers and administrators to manage applications and containers in the OpenShift Container Platform. In this article, we will explore various use cases of the oc
command with code examples.
1: Logging in to the OpenShift Container Platform server
oc login
Motivation: Logging in to the OpenShift Container Platform server is the first step to interact with the platform using the oc command.
Explanation: The oc login
command prompts the user to enter their username and password to authenticate with the OpenShift Container Platform server. It also asks for the server URL.
Example Output:
Authentication required for https://api.openshift.com (openshift)
Username: john_doe
Password:
Login successful.
2: Creating a new project
oc new-project project_name
Motivation: Creating a new project allows users to organize their applications and resources within a logical boundary.
Explanation: The oc new-project
command creates a new project with the specified project_name
. Projects provide isolation between applications, resources, and permissions.
Example Output:
Now using project "project_name" on server "https://api.openshift.com".
3: Switching to an existing project
oc project project_name
Motivation: Switching to an existing project is necessary to perform operations, such as deploying applications or managing resources, within that project.
Explanation: The oc project
command switches the current project context to the specified project_name
.
Example Output:
Now using project "project_name" on server "https://api.openshift.com".
4: Adding a new application to a project
oc new-app repo_url --name application
Motivation: Adding a new application to a project is essential for deploying and managing applications within the OpenShift Container Platform.
Explanation: The oc new-app
command deploys a new application from the specified repo_url
and assigns it the specified application
name. This command automatically creates the necessary resources and sets up the build and deployment configurations.
Example Output:
--> Creating resources ...
imagestream.image.openshift.io "application" created
buildconfig.build.openshift.io "application" created
deploymentconfig.apps.openshift.io "application" created
...
5: Opening a remote shell session to a container
oc rsh pod_name
Motivation: Opening a remote shell session to a container allows users to troubleshoot and debug issues within the running containers.
Explanation: The oc rsh
command opens a remote shell session to the specified pod_name
. Users can then execute commands within the container.
Example Output:
sh-4.2$
6: Listing pods in a project
oc get pods
Motivation: Listing pods provides an overview of the running pods within the current project.
Explanation: The oc get pods
command retrieves a list of all pods within the current project.
Example Output:
NAME READY STATUS RESTARTS AGE
application-554c4f7db9-54692 1/1 Running 0 5m
database-75c59fb9f9-24pcq 1/1 Running 0 5m
...
7: Logging out from the current session
oc logout
Motivation: Logging out from the current session ensures the security of the user’s account and prevents unauthorized access to the OpenShift Container Platform.
Explanation: The oc logout
command logs out the user from the current session, terminating the access to the OpenShift Container Platform.
Example Output:
Logged "john_doe" out on server "https://api.openshift.com".
These examples demonstrate the versatility and usefulness of the oc
command for managing applications and containers within the OpenShift Container Platform. Whether it’s deploying applications, troubleshooting issues, or managing projects, the oc
command is an invaluable tool for developers and administrators.