Simple ways to deploy in OpenShift
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?
- Pre-requisite
- Ways to deploy an application
- From Source code
- From Image repository
- From Image Stream
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,
- Exposure to Docker environment
- Exposure to Application deployment in any eco system
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'
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,
Here, we are doing an exec into the docker container using the console,
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
oc create secret generic mysql-secret \
--from-literal=MYSQL_PASSWORD=user_a_pass
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
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
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
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!