Going further on Openshift
Tags: openshift, devops, docker, kubernetes
Published: 2018-08-19 00:25:17

---
title: Going further on Openshift
published: true
description: Openshift has various resources to help deploy your applications without even requiring a previously built Docker image.
tags: openshift,devops,docker,kubernetes
---

In my previous post I gave a brief introduction about what is Openshift ([now OKD](https://okd.io)) and how we could easily deploy [Gitea](https://gitea.io). Now we will focus on building and deploying java applications using the "source2image" (S2I) strategy.

But what is this S2I?

According to the [S2I project site](https://github.com/openshift/source-to-image):

> Source-to-Image (S2I) is a toolkit and workflow for building reproducible Docker images from source code. S2I produces ready-to-run images by injecting source code into a Docker container and letting the container prepare that source code for execution. By creating self-assembling builder images, you can version and control your build environments exactly like you use Docker images to version your runtime environments.

What will happen in practice is that Openshift will take your project's source, combine it with a *builder* docker image and generate the final runtime image used to run the application.

## Preparing the development environment

To run our development environment, we will use [minishift](https://github.com/minishift/minishift) to create an openshift cluster. Use the commands below to download and install minishift (if you are using linux):

```sh
MINISHIFT_VERSION=1.22.0
MINISHIFT_URL=https://github.com/minishift/minishift/releases/download/v$MINISHIFT_VERSION/minishift-$MINISHIFT_VERSION-linux-amd64.tgz

curl -L -s -o /tmp/minishift.tgz $MINISHIFT_URL

tar -C /tmp --wildcards -x */minishift --strip 1 -zf /tmp/minishift.tgz
sudo install /tmp/minishift /usr/local/bin/minishift

```

With minishift installed, we can configure and start it (which may take several minutes):

```sh
# change values according to your workstation
MINISHIFT_VM_DRIVER=virtualbox
MINISHIFT_MEMORY=8G
MINISHIFT_CPUS=4
MINISHIFT_DISK_SIZE=100G

# configure minishift
minishift config set vm-driver $MINISHIFT_VM_DRIVER
minishift config set memory $MINISHIFT_MEMORY
minishift config set cpus $MINISHIFT_CPUS
minishift config set disk-size $MINISHIFT_DISK_SIZE

minishift start
```

To interact with Openshift, we need to download the `oc` command line tool, but with minishift we can use the bundled one, just execute the following:

```sh
eval $(minishift oc-env)
```

Then we login to openshift:

```sh
oc login 192.168.99.100:8443 -u admin
# use 'admin' as password
```

## Deploying the application

Now that we have minishift up and running, we can start deploying some application. Let's start with a [django example application](https://github.com/sclorg/django-ex).

But first, let's create the *project*:

```sh
oc new-project django-example --display-name 'Django Example'
```

It will print something like this

```
Now using project "django-example" on server "https://192.168.99.100:8443".

You can add applications to this project with the 'new-app' command. For example, try:

    oc new-app centos/ruby-22-centos7~https://github.com/openshift/ruby-ex.git

to build a new example application in Ruby.
```

(Do not worry with the message about a ruby application, we are actually deploying a **python** application ;)

A *project* is the openshift terminology to a namespace, a place to group related resources and define access rules to these resources. For example, we could allow user "snake" to view our "django-example" project, user "rattlesnake" to view and edit (trigger builds and deploys, etc.) and not give any access to user "badger".

With the project created, we can create the application:

```sh
oc new-app python:3.6~https://github.com/sclorg/django-ex.git
```

You might be asking "what is going on?". Well, we are telling openshift to take the `python:3.6` image and combine it with the source code available at https://github.com/sclorg/django-ex.git. The syntax is "image"~"source".

**But be careful!** The `python:3.6` is **not the official python image from docker hub**! It is instead from [centos/python-36-centos7](https://hub.docker.com/r/centos/python-36-centos7/) and was imported to the openshift integrated registry! It is an S2I image to build and run python projects! You can view other images using the command `oc get imagestream -n openshift`

The output of the `oc new-app` will have some information about the application, with the following at the end:

```
--> Success
    Build scheduled, use 'oc logs -f bc/django-ex' to track its progress.
    Application is not exposed. You can expose services to the outside world by executing one or more of the commands below:
     'oc expose svc/django-ex' 
    Run 'oc status' to view your app.
```

The suggested command `oc expose svc/django-ex` will do something very important: it will expose a *Service* (a Kubernetes Service) to external traffic through a *Route*. The *Route* is very similar to a Service, but will handle external requests that matches a specific hostname and can also handle encryption (https).

Let's expose our application:

```sh
oc expose svc/django-ex
```

Unlike previous commands, exposing a service does not produce much output, so let's list the available routes to get the one we just created:

```sh
oc get routes
```

Which will output:

```
NAME        HOST/PORT                                        PATH      SERVICES    PORT       TERMINATION   WILDCARD
django-ex   django-ex-django-example.192.168.99.100.nip.io             django-ex   8080-tcp                 None
```

As we can see, the route listens for the hostname `django-ex-django-example.192.168.99.100.nip.io`. This `nip.io` is a special service service that will redirect requests to the ip address contained in the hostname itself, is this case, 192.168.99.100, the ip address of the minishift vm.

Accessing the route address will show the following screen:

![openshift django example welcome screen](https://thepracticaldev.s3.amazonaws.com/i/ym1gv7dgtbkxnh5sguyh.png)

## Openshift web console

Openshift also has a very nice web console accessible at [https://192.168.99.100:8443/console]. Just login with the same credentials we used before ("admin" for both username and password).

![openshift web console](https://thepracticaldev.s3.amazonaws.com/i/80jf3vky8vxez681m62z.png)

Opening the link to our project on the right will take us to the following page:

![openshift project overview](https://thepracticaldev.s3.amazonaws.com/i/s6cov2vkjuvn09hboghm.png)

It is the project dashboard where we have an overview of the project, the applications deployed, routes, container status, etc. We can even scale the container!

The web console has lots of featues, even though some actions still require using the cli, like creating a service by exposing the *Deployment*.

This is it for now. Next time we will get into the web console.