From a Docker Image to a Deployment on OpenShift
Tags: beginners, cloud, openshift, kubernetes
Published: 2020-08-03 13:35:15

In this tutorial, I’ll show you a working example for deploying a docker container on OpenShift V4. You can find all Kubernetes resources from this tutorial on my [Github repo](https://github.com/pmgysel/cloud-stack_openshift-kubernetes-ressources). If you only have some basic knowledge of Docker and Kubernetes and don’t know OpenShift yet – don’t worry, this tutorial will be perfect for you!

![Niderhorn](https://dev-to-uploads.s3.amazonaws.com/i/ecgr14ylpakotcqgfnmu.JPG)

# Quick History

## From Native Applications to Docker Containers to Kubernetes

In the olden days, software companies deployed their applications on “bare metal”, meaning one physical computer hosted one application. Over time, things improved thanks to virtual machines, which allowed multiple isolated applications to run on the same physical server. In the last couple of years, a more light-weight virtualization solution became popular – docker containers. Once companies started to heavily use docker containers, their deployment landscape became pretty messy, and so orchestration tools like Kubernetes came to the rescue. Kubernetes allows to plan, perform, and manage the deployment of applications in the form of Docker containers and to make deployments reproduceable and easy to customize.

![Container mess](https://dev-to-uploads.s3.amazonaws.com/i/xfophynpn4v3bwmdbwm1.jpg)

## From “Bare-metal” to “PaaS”

Parallel to the development of new tools, the hosting landscape also changed significantly. While most companies were hosting their applications on machines in their basement a decade or two ago, more and more software development companies started to investigate letting third party companies host their applications. This greatly helped to separate the concerns of application development vs. application deployment and monitoring. Today there are many great cloud providers which take care of several important challenges like hardware scaling, geo redundancy, monitoring of running clusters and configuring security related components. Companies which offer a [Platform as a Service](https://en.wikipedia.org/wiki/Platform_as_a_service) include AWS, Google Cloud, IBM Cloud, Microsoft Azure, and many more.

# Why OpenShift?

OpenShift is the PaaS solution we’ll be discussing in this blog. It is developed by [Red Hat](https://www.redhat.com) and builds on top of Docker and Kubernetes. OpenShift can be run on any cloud infrastructure, and many popular providers have an offering, including the aforementioned providers AWS, Google Cloud, IBM Cloud, and Microsoft Azure.

So, what’s the advantage of using OpenShift over a Kubernetes cluster? Well OpenShift offers several additional features while also supporting all Kubernetes resources:

* **Simple to use**: Thanks to many preconfigured parameters and a nice GUI, OpenShift is fairly simple to learn.
* **Secure by default**: OpenShift apps are secure by default. Security experts from Red Hat have put a lot of time and effort into making the platform resilient against attacks.
* **Additional tools**: OpenShift offers additional tools compared to a simple Kubernetes cluster: Source-to-Image build, Kibana monitoring, OpenShift registry, etc.

Note though that some people might prefer pure Kubernetes cluster infrastructures, for the following reasons:
* **Avoid vendor look-in**: OpenShift supports all Kubernetes objects, so by using only standard Kubernetes resources for the deployments, a future switch to another cloud provider is possible without extra effort. Unfortunately, there are some minor differences between OpenShift and Kubernetes, for example OpenShift’s Route resource (which doesn’t exist in Kubernetes).
* **Pricing**: There are many providers out there for both OpenShift and Kubernetes, so comparing the costs is a tricky endeavor. Generally speaking, you get more from an OpenShift platform, which is why this solution tends to cost slightly more.

Overall, OpenShift is a great solution that is getting adapted by many companies all over the world, so my guess is that it is a solution that will stay for quite some time. Without any further ado, let’s jump into the working example!

# DIY OpenShift Example

Alright, now it’s time to run your first app on OpenShift V4. All you need is an OpenShift account and the Kubernetes resources from the [Github repo](https://github.com/pmgysel/cloud-stack_openshift-kubernetes-ressources).

## Create Free OpenShift Account

For experimental purposes, OpenShift offers a solution termed “OpenShift online” which can be used for free for 2 months. 

![OpenShift online](https://dev-to-uploads.s3.amazonaws.com/i/88rnbrnmyfw6wy0jadim.png)

* Create Red Hat account: https://www.redhat.com/ &#x2011;> Login &#x2011;> Register now
* Confirm email
* Start OpenShift online trial: https://www.openshift.com/products/online/ &#x2011;> Choose Starter plan and follow the instructions
* Login to OpenShift online web console: https://manage.openshift.com/sign_in 
* Create your first project: in “Administrator” view, go to Home &#x2011;> Projects &#x2011;> click Create Project

## Deploy App on OpenShift (via web console)

To deploy your first app, you’ll need two items:
* A Docker image
* The Kubernetes resources

As for the Docker image: I have packaged a simple Java app into a Docker image and published it on [Docker Hub]( https://hub.docker.com/repository/docker/pmgysel/spring-boot-app). The docker image is directly reference in the Kubernetes resources, so all you need to do is download the Kubernetes resources from my [Gihub repo](https://github.com/pmgysel/cloud-stack_openshift-kubernetes-ressources).

Let’s start with the deployment. Head over to the web console where you’ve already created an OpenShift project, then:
* In “Developer” view, go to “+Add” and select “YAML”
* Copy and paste all Kubernetes resources: `deployment.yaml`, `service.yaml` and `route.yaml` (you’ll find the files on [Github](https://github.com/pmgysel/cloud-stack_openshift-kubernetes-ressources))
* That’s it! Your app is getting deployed on OpenShift. To check the status, go to “Administrator” view &#x2011;> Workloads &#x2011;> Pods

![Running pod](https://dev-to-uploads.s3.amazonaws.com/i/o7212jbk5f04ibd0n2ji.png)

You should see a Pod called “spring-boot-app-*” starting up. As soon as it is up and running, let’s test it like this:
* In “Administrator” view, go to Networking &#x2011;> Routes
* For “spring-boot-app-route”, select the “Location”: this is a public URL to access your running app.
* Append “/api/ping” to the URL – and you should see an answer from your app.

![Show pong](https://dev-to-uploads.s3.amazonaws.com/i/0mcjyv25h2t1fvgeii7c.png)

* That’s it folks 😊 You’ve got an application running on OpenShift!

## Deploy App on OpenShift (via CLI)

In a real development project, you’ll want to automate as much work as possible. People typically use a CI/CD pipeline like Jenkins to compile new app versions, test them, build the docker image and deploy the app in the cloud. For this purpose, OpenShift offers the command line tool `oc` – which allows to show the status of your running apps, alter deployment configurations and start new deployments.
* First, you probably want to remove all existing resources on OpenShift. The simplest way to do so is just to remove the active OpenShift project and create a new one.
* Install the `oc` CLI by following the instructions under “?” &#x2011;> “Command Line Tools”
* Login to OpenShift via oc CLI by following the instructions “Copy Login Command” on the previous page.

```shell
$ oc login --token=… --server=…
```

* Make sure you’re in the correct OpenShift project

```shell
$ oc projects
```

* Fetch the Kubernetes resources from Github

```shell
$ git clone https://github.com/pmgysel/cloud-stack_openshift-kubernetes-ressources.git
$ cd cloud-stack_openshift-kubernetes-ressources
```

* Upload Kubernetes resources

```shell
$ oc apply -f deployment.yaml
$ oc apply -f service.yaml
$ oc apply -f route.yaml
```

* That’s it folks 😊 You just performed your first OpenShift deployment via command line!

# Our Kubernetes Resources

You have created 3 Kubernetes resources on the OpenShift platform. Let’s quickly get an overview of how these 3 resources work together. This is a fairly simple primer on Kubernetes, so feel free to skip this part if you already have advanced knowledge in this technology.

![Kubernetes resources](https://dev-to-uploads.s3.amazonaws.com/i/xc4jvj8p23cs9wqc1bv5.png)

* *deployment.yaml*: The Kubernetes Deployment specifies how your containers should be deployed, including the number of instances, the port to be exposed and the amount of RAM per Pod.
* *service.yaml*: A Kubernetes Service acts as entry point to running pods. You don’t need to worry about the exact number of running pods, how to distribute the load, how to detect ready pods and so on. The Service will do all the work for you.
* *route.yaml*: All Services and Pods are only accessible from within the OpenShift cluster. To publish your service to the outside world, you can use an OpenShift Route. Just a small hiccup here: Routes are an OpenShift only feature and don’t exist in Kubernetes. In pure Kubernetes, there’s an analog resource termed “Ingress”.

# Summary

Bonus points if you’re still reading this 😊 So in this post, we learned about the advantages of OpenShift and ran a simple app on the OpenShift platform. I hope this was informative for you!

Some more resources:

* OpenShift has an official [OpenShift tutorial](https://www.openshift.com/learn/courses/getting-started/?hsLang=en-us)
* Kubernetes resources: [Deployments]( https://docs.openshift.com/online/pro/dev_guide/deployments/how_deployments_work.html), [Services](https://docs.openshift.com/online/pro/architecture/core_concepts/pods_and_services.html#services), and [Routes](https://docs.openshift.com/online/pro/dev_guide/routes.html)
* OpenShift’s `oc` CLI is an extension to Kubernete’s kubectl. The OpenShift website has a [docu on oc]( https://docs.openshift.com/online/pro/cli_reference/basic_cli_operations.html)

Remember to like ❤️ this blog if it was helpful to you and see you next time!