Recently I’d completed the training from Redhat on Introduction to OpenShift Applications(DO101). So, I thought to write an article on how to get started with the Redhat OpenShift Container Platform(RHOCP).
Redhat OpenShift is an enterprise-ready Kubernetes container platform with full-stack automated operations to manage hybrid cloud and multi-cloud deployments. This platform integrates the tools required to build and run applications and manages the complete application lifecycle from initial development to production.
The workflow for the OpenShift platform starts when a developer provides a Git repository URL for an application to OpenShift. This platform is so much powerful that, it automatically retrieves the source code from Git, builds it, and then deploys the application. The developer can also set the OpenShift to automatically detect the new Git commits, and then automatically rebuild and redeploy the application.
Let’s start with the installation of OpenShift.
For using OpenShift on local machines, there is one program we have to used know as MiniShift. Minishift and OpenShift 4 are very different, but Minishift is nevertheless a powerful introduction to the concept of running a cluster, creating projects, monitoring tasks, and maintaining cloud infrastructure.
- The machine should support virtualization.
- The hypervisor should be installed like VirtualBox, KVM, etc.
- Download the MiniShift
Download the MiniShift installation file compatible with your OS using the following link. In my case, I’m using MiniShift version v1.32.0.
Create your free GitHub account today to subscribe to this repository for new releases and build software alongside 50…
2. Unzip the downloaded file and save it. Now, add the path of the unzipped file to the environment variables so that the minishift command can be accessed through the command line.
Now, open the command line and run the command to check if the minishift command is available.
$ minishift version
3. Starting the MiniShift cluster:
If you are installing minishift in Virtualbox, then configure minishift to use vm-driver virtualbox.
$ minishift config set vm-driver virtualbox
Check if if the vm-driver is set to virtualbox.
$ minishift config get vm-driver
For the first time, we are running minishift start command, we have to specify the hypervisor name. So that it will download and set up the MiniShift cluster in the hypervisor. In my case, I’m using VirtualBox as a hypervisor.
$ minishift start --vm-driver virtualbox --network-nameserver 184.108.40.206
Now, our OpenShift cluster started and accessible via web console at:
If we open the VirtualBox, we can see that the MiniShift cluster/VM is installed there.
Using OpenShift through WebUI(Console):
To use the OpenShift through the console, open up the browser and go the URL provided by the minishift start command.
Enter the username shown by the minishift start command, in my case, the username is a developer and in the password field enter any value you liked.
By hitting Log In button, you will be logged into the OpenShift console.
Deleting the default project named “My Project”. Now, we don’t have any projects.
We can check the same thing using CLI:
The command to see the available projects is
$ oc get projects
Create a new project:
For creating a new project, click on the “Create Project” button, enter the project name, and hit the create button.
But, let’s create a new project using the command line. Our project name will be myapp.
The command for creating a new project named myapp is
$ oc new-project myapp
Create a new app:
In this module, we are going to see how to create a new app using a web console as well as CLI(Command Line).
Create a new app using Console:
Go inside a project. Let’s say the myapp project.
The catalog method allows us to choose available ready to use applications. Also, we can select the language of our application and provide the source code from the Git Repository.
As we are deploying PHP application, let’s select our app language as PHP from the catalog.
In the configuration, provide the name to the app, URL of the Git Repository. In the Git Reference, the default branch to fetch the source code is selected as the master branch.
Now, our PHP application code is stored in the Web folder in the Git Repository. So, we have to provide a directory of the application code as a Context Dir. This directory is used as a context directory for the build.
As of now, we are keeping the Source Secret blank.
In the Routing, make sure the checkbox of “Create a route to the application” is checked. When our application is deployed, we will use this Route URL to access the application.
Select the number of replicas.
Make sure the proper labels are given and hit the create button.
Now, go to the overview tab. We will see our app deployment is started. In the NETWORKING section, the Route URL is created. When the build is completed, we will see 1 POD is running.
Finally, our app is accessible by using the route URL. Open the route URL in the browser and hopefully, you will see our app is up and running.
Create a new app using CLI:
Another way to create an app is by using the command line. OpenShift provides an efficient way to create a new app using oc new-app command.
I have to create a new app for an application in PHP language and the source code is present in a Git Repository. The following command is used to launch this new app in the OpenShift project.
$ oc new-app php~https://github.com/cankush625/Web.git
As the command is fired, we can see that our new app build is started.
Once the build is completed, we can see that one POD is up and running.
But, we are not able to access the new app because we haven’t created the route for this application yet. So, let’s create a route for this application first.
Route for the app can be created bu using oc expose command. This command will require the name of the service we want to expose. In our case, the name of the service is the web.
$ oc expose service web --name=web
Let’s verify is the route is created for the new app or not from the OpenShift web console.
The new app can be accessed from this route.
Finally, we had successfully launched a new app from the CLI.
Scaling Applications in OpenShift:
In the real world, we can’t run the applications on a single pod. It may be possible that over a night our app gets famous and the traffic comes to our app is got increased. The single pod can’t handle that much traffic. So, we need to increase the number of pods to meet the requirement. This is known as scaling up the application.
When the load/traffic from our app goes down, we don’t need lots of pods. To save the cost, we need to decrease the number of pods. This is known as scaling down the application.
Let’s take a look at how to do scale up and scale down the application from the OpenShift console.
Scale-up the application:
Open the application from the overview tab. We can see that currently, one pod is running. Click on the up arrow near the pod.
After we clicked on the up arrow, we will see that the application starts scaling up by launching another pod.
And finally, another pod is launched identical to the first pod. Now, the number of running pods are two. Here, we had scaled up our application.
By going into the Monitoring tab, we can see more details about the running services.
Scale-down the application:
Open the application from the overview tab. We can see that currently, two pods are running. Click on the down arrow near the pod.
After we clicked on the down arrow, we will see that the application starts scaling down by terminating the extra pod.
And finally, the application is scaled down as the extra pod is terminated. Now, the number of running pods are one.
Get the logs of the pod:
Logs are very important in monitoring the application. Let’s get the logs from the pods in myapp project.
First of all, we need to know the names of the pods. This can be done by using oc get pods command. This command will give us some more information like no of ready pods, the status of the pod, no. of restarts, and the age of the pod.
$ oc get pods
After we get the names of the pod, we can run the oc logs command to see the logs of the pod.
$ oc logs podName