How to deploy full-stack JavaScript applications in OpenShift

Red Hat OpenShift is a Kubernetes distribution that makes it easy to deploy and scale applications in the cloud. In this hands-on learning path, created by Don Schenck, you will learn how to deploy a full-stack JavaScript application in an OpenShift cluster.

Access the Developer Sandbox

In this lesson, you will create a microservice from an existing image, then link it with other services by setting environment variables for the deployment.

Prerequisites:

In this lesson, you will:

  • Create the service urlshortener-redirector service using an existing image.
  • Link all of the URL Shortener application pieces by setting environment variables.

Create the redirection service

Existing images stored in registries can be used to create apps in OpenShift. Run the following command to create the service urlshortener-redirector in your OpenShift cluster:

oc new-app --name=urlshortener-redirector quay.io/rhdevelopers/urlshortener-redirector:latest

Run the following command to make the redirector available to the frontend website:

oc expose svc/urlshortener-redirector --port=8080

Link all of the URL Shortener application pieces

Run the following command to set the environment variable for the redirector so it can connect to the Postgres database service:

oc set env deploy/urlshortener-redirector POSTGRES_SERVER=shorties

Run the following command to get the route to the redirector service:

oc get routes

Using the URl for the route name urlshortener-redirector, prefix with the https:// scheme and run the following command so the front-end website can connect to the redirector service:

oc set env deploy/urlshortener-front REDIRECTOR_URL=http://$REDIRECTOR_SERVICE_ROUTE_URL

Here’s an example:

oc set env deploy/urlshortener-front REDIRECTOR_URL=http://urlshortener-redirector-rhn-engineering-dsch-dev.apps.sandbox-m3.1530.p1.openshiftapps.com

Now the application is completed with all layers up and running (Figure 1).

Server Status page showing all services up and running.
Figure 1: All services up and running.

Wrapping up

One database, one website, two services. Up and running in OpenShift. The concepts demonstrated here included deploying a database from template, deploying an application from source code, deploying an image from an image registry, and updating environment variables at the command line. These skills will allow you to deploy your own full-stack JavaScript (and other language) applications to your own Red Hat OpenShift cluster.

Note: The urlshortener-redirector app is written in Go. As a further self-learning opportunity, you can write the application in Node.js, create an image, and replace the app running in your OpenShift cluster.

Previous resource
Deploy the back end and connect it to the database and front end