Simple ways to deploy in OpenShift

Suriya
Javarevisited
Published in
4 min readDec 4, 2022

localhost-ing

OpenShift is a cloud based container orchestration platform based on Kubernetes. It provides a self-service platform to create, modify, and deploy applications on demand, thus enabling faster development and release life cycles.

Contents

Why deploy in OpenShift?

There is whole bunch of features that the OpenShift provides out of the box rather than bringing them one by one on a bare metal. Here is the overview why one should choose OpenShift over Kubernetes.

Keeping the theories aside, let’s jump into the post to see how easy or hard, a basic application deployment is, from a developer’s perspective.

Pre-requisite

This post assumes the reader has,

Ways to deploy an application

If you are looking to setup OpenShift locally, check the following post

There are multiple ways to deploy an application into OpenShift.

The easiest way is to use the console UI to add/remove resources/project/application.

However, in the real world we would use some sort of pipeline to deploy the application. Wherein, deploying an application thru automated script might come handy.

So, here are the three simple ways to deploy an application into OpenShift cluster using mainly the command line tool which can be then part of the pipeline script.

  • From Source code
  • From Image repository
  • From Image Stream

From Source code

The code can either be in the local or in a remote source code repository such as Gitlab or GitHub.

Code repository

oc new-app <github-url> --name='appname'

— Local

oc new-app . --name='java-local'
navigate to the code path in the local file system and run the above
Deployments created

From Image repository

This would be the most commonly preferred basic method. Rather than pulling it from the Source code and using the OC’s Build Config, this provides more control.

Let’s try to run the mysql image from docker hub with a mandatory environment variable,

oc new-app --name=mysql-img \
--image=mysql \
--env=MYSQL_ROOT_PASSWORD=suriya

Here is the pod environment info for the above deployment,

Environment variable with the MYSQL_ROOT_PASSWORD=suriya variable

Here, we are doing an exec into the docker container using the console,

Logged in as a root user with password suriya

Now, lets create a new users Config Map resource,

oc create configmap mysql-config \
--from-literal=MYSQL_USER=user_a \
--from-literal=MYSQL_DATABASE=mydb
Config Map resource showing the created literal key-values
oc create secret generic mysql-secret \
--from-literal=MYSQL_PASSWORD=user_a_pass
Secret resource showing the secret created

Now, let’s set it to the container’s environment variable so that it will be visible to the mysql,

oc set env deployment/mysql-img \
--from=configmap/mysql-config \
--from=secret/mysql-secret
The terminal showing mydb and the logged in user user_a

From Image Stream

For this section, we will be using our private image repository similar to the docker hub running in the local machine.

Here, are the steps for running a local image repository,

After building the docker image and pushing it to the local image repository using docker build and docker push. Run the following to import the image from the local image repository to the OpenShift’s internal image stream.

oc import-image my-app-img-import --from=192.168.128.1:5443/my-app \
--reference-policy='Local' --confirm
Check the image stream via console or via CLI

Now, run new-app to create a new app from the image stream my-app-img-import,

oc new-app --image-stream=my-app-img-import
A new project created with deployment — pod and service

There are still other means of managing a deployment to facilitate continuous integration and deployment be seamless rather than passing all the variables in the command line.

Let’s take a look at it some other day!

Happy localhost-ing!

--

--

Suriya
Javarevisited

I am a full-time Software Engineer and a passionate Landscape Photographer. For more info visit https://suriyaprakhash.com