Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Quick links: redhat.com, Customer Portal, Red Hat's developer site, Red Hat's partner site.

  • You are here

    Red Hat

    Learn about our open source products, services, and company.

  • You are here

    Red Hat Customer Portal

    Get product support and knowledge from the open source experts.

  • You are here

    Red Hat Developer

    Read developer tutorials and download Red Hat software for cloud application development.

  • You are here

    Red Hat Partner Connect

    Get training, subscriptions, certifications, and more for partners to build, sell, and support customer solutions.

Products & tools

  • Ansible.com

    Learn about and try our IT automation product.
  • Red Hat Ecosystem Catalog

    Find hardware, software, and cloud providers―and download container images―certified to perform with Red Hat technologies.

Try, buy, & sell

  • Red Hat Hybrid Cloud Console

    Access technical how-tos, tutorials, and learning paths focused on Red Hat’s hybrid cloud managed services.
  • Red Hat Store

    Buy select Red Hat products and services online.
  • Red Hat Marketplace

    Try, buy, sell, and manage certified enterprise software for container-based environments.

Events

  • Red Hat Summit and AnsibleFest

    Register for and learn about our annual open source IT industry event.

Deploy a 3-tier Node.js app on Red Hat OpenShift

October 12, 2019
Jan Kleinert
Related topics:
ContainersKubernetes
Related products:
Red Hat OpenShift

Share:

Share on twitter Share on facebook Share on linkedin Share with email
  • Part 1: Deploy and link the back end
  • Part 2: Deploy the front end
  • Part 3: Deploy and link the database

Through the exercises presented here, you’ll see how to deploy a three-tier Node.js application on Red Hat OpenShift. The example application is a concession kiosk, used to streamline the process of placing concession orders. The user will place an order via the front end web application. The order information will be saved in a MongoDB database, and then the user will be given an order number. Once their order number is called, users can go to the concession window to pay and receive their food.

Prerequisites

Before you start, you’ll need access to a Red Hat OpenShift cluster. If you don’t already have access to one, you can install Red Hat CodeReady Containers, which will allow you to run OpenShift in the CodeReady Containers virtual machine on your local computer. To install CodeReady Containers, go to https://www.redhat.com/en/technologies/cloud-computing/openshift/try-it and choose Laptop as your infrastructure provider:

illustration of a laptop, which says "Powered by Red Hat CodeReady Containers" below it, and below that says "Developer Preview"

Another option is to sign up for OpenShift Online’s free starter tier or a 30-day free trial of the pro tier.

Once you have access to an OpenShift cluster, be sure you have access to the oc command-line tool. If you’re using CodeReady Containers, it comes with oc. If you’re accessing OpenShift another way, you may need to download oc. You can download the appropriate version for your operating system here.

You have two different options for logging in with oc. One option is to use your username and password:

oc login <cluster addr>:<cluster port> -u <username> -p <password>
Copy snippet

Another option is to first log into the OpenShift Web Console and then click your username in the top right corner. A drop-down menu will appear.

Click on Copy Login Command and follow the instructions, and you’ll get an oc login command with your API token. It will be in a format similar to this:

dropdown screen with two options: Copy Login Command and Log out
oc login --token=<abc…> --server=<cluster addr>:<cluster port>
Copy snippet

Once you’re logged in, you’re ready to move on to the next section.

Part 1: Deploy and link the back end

Part 1: Deploy and link the back end

Overview of the back end app

The back end is written using Node.js and Express and will serve as the intermediary between the front end and the database (which we’ll create later in part 3). It will save the order information to the database (if the database is available) and generate an order number that will be provided to the customer. 

Log in to the OpenShift cluster

If you just completed the prerequisites, you are probably still logged into your OpenShift cluster. If not, you can do this multiple ways: you can authenticate with a token, which you can get from the web console, or you can use a username and password. Choose one of these options:

oc login <cluster addr>:<cluster port> --token=<abc…>
Copy snippet
oc login <cluster addr>:<cluster port> -u <username> -p <password>
Copy snippet

Deploy the back end on OpenShift

First, we need to create a project. We will deploy the back end, front end, and database in this project.

oc new-project concession-kiosk
Copy snippet

The code for our back end is in this GitHub repo: https://github.com/jankleinert/concession-kiosk-backend.

We’ll use the oc new-app command to build a container image from this source code and then deploy it on OpenShift. The oc new-app command uses Source-to-Image, which can infer which builder image to use based on characteristics of your code. In this case, since we have a package.json file in the repo, new-app knows to use the Node.js builder image.

oc new-app https://github.com/jankleinert/concession-kiosk-backend --name backend

Your output should look something like this:

--> Found image a7092f1 (3 days old) in image stream 
"openshift/nodejs" under tag "10" for "nodejs"

    Node.js 10.16.3
    ---------------
    Node.js 10.16.3 available as a container is a base platform for 
building and running various Node.js 10.16.3 applications and 
frameworks. Node.js is a platform built on Chrome's JavaScript runtime 
for easily building fast, scalable network applications. Node.js uses 
an event-driven, non-blocking I/O model that makes it lightweight and 
efficient, perfect for data-intensive real-time applications that run 
across distributed devices.

    Tags: builder, nodejs, nodejs-10.16.3

    * The source repository appears to match: nodejs
    * A source build using source code from 
https://github.com/jankleinert/concession-kiosk-backend will be 
created
      * The resulting image will be pushed to image stream tag 
"backend:latest"
      * Use 'oc start-build' to trigger a new build
    * This image will be deployed in deployment config "backend"
    * Port 8080/tcp will be load balanced by service "backend"
      * Other containers can access this service through the hostname 
"backend"

--> Creating resources ...
    imagestream.image.openshift.io "backend" created
    buildconfig.build.openshift.io "backend" created
    deploymentconfig.apps.openshift.io "backend" created
    service "backend" created
--> Success
    Build scheduled, use 'oc logs -f bc/backend' 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/backend'
    Run 'oc status' to view your app.
Copy snippet

Run oc logs -f bc/backend to watch the status of the build. Once it displays Push successful, the build is complete.

One of the resources created by running this command was a service. A service gives us a way of accessing the pod(s) for our back end application within the cluster. Run the following to see some basic information about the back end service:

oc get svc backend
Copy snippet

The output should look something like this:

NAME      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
backend   ClusterIP   172.30.236.87           8080/TCP   7m53s
Copy snippet

The back end service is accessible within your OpenShift cluster via the service name backend and using port 8080. In Part 2, you’ll see that when we link the front end and the back end, the front end will be able to communicate with the back end through its service name and port.

Part 2: Deploy the front end

Part 2: Deploy the front end

The front end for the application is a simple web app built with Node.js and EJS (embedded JavaScript) for templating. It displays a menu for the concession kiosk. Once a user places their order, it displays a confirmation page with an order number, so that they can wait for their order to be called.

Deploy the front end

Similar to what we did for the back end, we’ll use oc new-app to deploy the front end as well. This time though, we are passing in some environment variables. These are the environment variables that need to be set in order for the front end to talk to the back end through its service name as mentioned in the previous section. 

oc new-app https://github.com/jankleinert/concession-kiosk-frontend --name frontend -e COMPONENT_BACKEND_HOST=backend -e COMPONENT_BACKEND_PORT=8080
Copy snippet

As before, you can watch the logs as the build progresses:

oc logs -f bc/frontend
Copy snippet

Create a URL for the front end

With our front end deployed, we want to be able to access it in the browser, so we need a publicly accessible URL for it. To do that, we will create a Route to expose the service.

oc expose svc/frontend

Then, we can run the following command to get the route that was created.

oc get routes

Copy that URL and paste it into your browser. You should see a Concession Kiosk Menu like the one below.

menu with hotdog, hamburger, salad, pizza, and drink

Try it out to see how it works. Users can select the items and quantities they want to order and once their order is placed, they’ll receive an order number. The order number is just a placeholder for now, until we have a database in place.

In Part 3, we’ll deploy and link the database so that the orders are stored and real order numbers are generated.

Part 3: Deploy and link the database

Part 3: Deploy and link the database

Up to this point, we’ve been using some hard-coded values for the order number, and we haven’t actually stored the order information anywhere. In this part, we will update that by deploying and connecting to a MongoDB database.

Create a MongoDB Database Service

Make sure you are logged in to your Red Hat OpenShift cluster and confirm that you are still in the concession-kiosk project:

oc project concession-kiosk
Copy snippet

We will use a template to create a MongoDB database. A template describes a set of objects that can be parameterized and processed to produce a list of objects for creation by OpenShift. There are a set of templates that are included with OpenShift. The one we will use is the mongodb-ephemeral template.

Note: The mongodb-ephemeral template does not use persistent storage and should therefore only be used for testing. The mongodb-persistent template is better used if you desire persistent storage.

Create the database using the template:

oc new-app --template=mongodb-ephemeral
Copy snippet

You should see output that looks something like this:

--> Deploying template "openshift/mongodb-ephemeral" to project 
concession-kiosk

     MongoDB (Ephemeral)
     ---------
     MongoDB database service, without persistent storage. For more 
information about using this template, including OpenShift 
considerations, see 
https://github.com/sclorg/mongodb-container/blob/master/3.2/README.md.

     WARNING: Any data stored will be lost upon pod destruction. Only 
use this template for testing

     The following service(s) have been created in your project: 
mongodb.

            Username: 
            Password: 
       Database Name: sampledb
      Connection URL: mongodb://:@mongodb/sampledb

     For more information about using this template, including 
OpenShift considerations, see 
https://github.com/sclorg/mongodb-container/blob/master/3.2/README.md.

     * With parameters:
        * Memory Limit=512Mi
        * Namespace=openshift
        * Database Service Name=mongodb
        * MongoDB Connection Username= # generated
        * MongoDB Connection Password= # generated
        * MongoDB Database Name=sampledb
        * MongoDB Admin Password= # generated
        * Version of MongoDB Image=3.6

--> Creating resources ...
    secret "mongodb" created
    service "mongodb" created
    deploymentconfig.apps.openshift.io "mongodb" created
--> Success
    Application is not exposed. You can expose services to the outside 
world by executing one or more of the commands below:
     'oc expose svc/mongodb'
    Run 'oc status' to view your app.
Copy snippet

Update the back end to communicate with the database

With the database up and running, we need to set an environment variable for our back end application that tells it the MongoDB Connection URL. Copy the Connection URL from the output. It will be in this format:

mongodb://<generated username>:<generated password>@mongodb/sampledb
Copy snippet

If you look into the index.js file for the back end, you’ll see the following line. We’ll use the oc set env command to set the MONGODB_URL environment variable.

const dbConnectionUrl = process.env.MONGODB_URL || 
'mongodb://localhost:27017/sampledb’;
Copy snippet

Run this command, replacing the MongoDB connection string with your own:

oc set env dc/backend MONGODB_URL=mongodb://<generated username>:<generated password>@mongodb/sampledb
Copy snippet

This change will trigger a new deployment, which should not take long to complete. Reload the front end in your browser and place a new order. Now, for each order you place, you should see a new order number and the contents of your order should be displayed on the thank you page as well.

Code repos for concession kiosk exercise

github.com/jankleinert/concession-kiosk-backend

github.com/jankleinert/concession-kiosk-frontend

Last updated: July 2, 2024

Recent Posts

  • How to integrate vLLM inference into your macOS and iOS apps

  • How Insights events enhance system life cycle management

  • Meet the Red Hat Node.js team at PowerUP 2025

  • How to use pipelines for AI/ML automation at the edge

  • What's new in network observability 1.8

Red Hat Developers logo LinkedIn YouTube Twitter Facebook

Products

  • Red Hat Enterprise Linux
  • Red Hat OpenShift
  • Red Hat Ansible Automation Platform

Build

  • Developer Sandbox
  • Developer Tools
  • Interactive Tutorials
  • API Catalog

Quicklinks

  • Learning Resources
  • E-books
  • Cheat Sheets
  • Blog
  • Events
  • Newsletter

Communicate

  • About us
  • Contact sales
  • Find a partner
  • Report a website issue
  • Site Status Dashboard
  • Report a security problem

RED HAT DEVELOPER

Build here. Go anywhere.

We serve the builders. The problem solvers who create careers with code.

Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

Sign me up

Red Hat legal and privacy links

  • About Red Hat
  • Jobs
  • Events
  • Locations
  • Contact Red Hat
  • Red Hat Blog
  • Inclusion at Red Hat
  • Cool Stuff Store
  • Red Hat Summit

Red Hat legal and privacy links

  • Privacy statement
  • Terms of use
  • All policies and guidelines
  • Digital accessibility

Report a website issue