DEV Community

Philipp Gysel
Philipp Gysel

Posted on • Updated on

From a Docker Image to a Deployment on OpenShift

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. 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

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

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 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 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.

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

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. 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.

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)
  • That’s it! Your app is getting deployed on OpenShift. To check the status, go to “Administrator” view ‑> Workloads ‑> Pods

Running pod

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 ‑> 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

  • 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 “?” ‑> “Command Line Tools”
  • Login to OpenShift via oc CLI by following the instructions “Copy Login Command” on the previous page.
$ oc login --token=--server=
Enter fullscreen mode Exit fullscreen mode
  • Make sure you’re in the correct OpenShift project
$ oc projects
Enter fullscreen mode Exit fullscreen mode
  • Fetch the Kubernetes resources from Github
$ git clone https://github.com/pmgysel/cloud-stack_openshift-kubernetes-ressources.git
$ cd cloud-stack_openshift-kubernetes-ressources
Enter fullscreen mode Exit fullscreen mode
  • Upload Kubernetes resources
$ oc apply -f deployment.yaml
$ oc apply -f service.yaml
$ oc apply -f route.yaml
Enter fullscreen mode Exit fullscreen mode
  • 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

  • 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:

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

Top comments (2)

Collapse
 
jbebar profile image
jbebar

Nice post, very synthetic and clear :)

Collapse
 
pmgysel profile image
Philipp Gysel

Thanks man, better clear than confusing 😎