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

Connect MongoDB to a Node.js application with kube-service-bindings

August 16, 2022
Costas Papastathis Michael Dawson
Related topics:
KubernetesNode.jsOperators
Related products:
Red Hat OpenShift Container Platform

Share:

    This is the third and final article in a three-part series introducing kube-service-bindings for Node.js developers on Kubernetes. Together with the Service Binding Operator (SBO), kube-service-bindings makes it easier to share credentials for services with your applications.

    The first article of this series offered background on the tools we're using, and the second set up some basic elements such as hosting on the Developer Sandbox for Red Hat OpenShift and a MongoDB database. Now we're going to use all these tools to create a binding between our Node.js application and the database.

    Deploy MongoDB as a cloud-hosted database

    The previous article set up access between MongoDB Atlas and your OpenShift cluster. If you went through those steps successfully, you are ready to deploy a cloud-hosted MongoDB database in the cluster as follows:

    1. In the OpenShift console, visit the Topology view by selecting Developer→Topology from the left sidebar.
    2. Select the project where you would like to deploy the cloud database by selecting, from the top of the left sidebar, Developer→Topology→Project. From the dropdown menu, select your project.
    3. In the left sidebar menu, click +Add→Cloud-Hosted Database→MongoDB Atlas Cloud Database Service→Add to Topology. Select your database instance and click Add to topology→Continue.
    4. Upon successful connection, you are taken to the Topology view, where the cloud-hosted database is deployed and visible (Figure 1).
    The Topology view shows that MongoDB Atlas is now accessible in your cluster.
    Figure 1: The Topology view shows that MongoDB Atlas is now accessible in your cluster.

    Deploy the Node.js application in OpenShift

    There are several ways to deploy a Node.js application in OpenShift: Through the oc OpenShift command-line interface (CLI), the odo CLI, the OpenShift console, etc. This article covers two options: The OpenShift console and Nodeshift, an NPM package.

    Deploy through the OpenShift console

    From the Developer perspective, select +Add→Import from Git.

    In the Git Repo url field, set the repository URL to https://github.com/nodeshift-blog-examples/kube-service-bindings-examples. This is a kube-service-bindings examples repository maintained by our team; it contains the Node.js application you are deploying in this article.

    Expand Show advanced Git options. On the Context dir field, set the value to src/mongodb, which is the path of the subdirectory where your Node.js application is located.

    On Builder Image, select Node.js and click Create.

    Deploy through Nodeshift

    Open a terminal and clone the git repository:

    $ git clone https://github.com/nodeshift-blog-examples/kube-service-bindings-examples.git
    $ cd ./kube-service-bindings-examples/src/mongodb

    Install Nodeshift globally:

    $ npm install -g nodeshift

    To find the login credentials required by the next command you'll use, visit your OpenShift console. In the upper right corner, click your username. A dropdown will appear. Click Copy login command (Figure 2), which transfers you to another page. Then click Display Token to display the username, password, and server credentials to log in with Nodeshift.

    Under your name in the console, you can obtain login credentials.
    Figure 2: Under your name in the console, you can obtain login credentials.

    Using these credentials, you can now log in to your OpenShift cluster with Nodeshift:

    $ nodeshift login --username=developer --password=password --server=https://api.server

    Deploy the Node.js application with Nodeshift through the following command, replacing the namespace name with your specific project name:

    $ nodeshift --namespace.name=<selected-project>

    Your application should be deployed and visible in the Topology view, as shown in Figure 3.

    The Node.js application appears in the Topology view.
    Figure 3: The Node.js application appears in the Topology view.

    Establish a connection between the Node.js application and the MongoDB database

    The final step in this series is to establish a connection between your Node.js application and the MongoDB database, which we'll accomplish in this section.

    Service Binding Operator

    At this point, two instances should show up in your Topology view: the Node.js application and the connection to your MongoDB instance in Atlas (Figure 4).

    The Topology view shows both the Node.js application and the external MongoDB database.
    Figure 4: The Topology view shows both the Node.js application and the external MongoDB database.

    To establish a connection between these instances, you will use the Service Binding Operator to share the credentials and kube-service-bindings to parse those credentials (binding data).

    You can create a Service Binding in two different ways:

    • Drag a line in the Topology view between the two backing services (the Node.js application and MongoDB).
    • Apply a YAML file specifying the service binding.

    We will go with the first option, which in our case is faster and easier.

    Hover the mouse over the Node.js application in the Topology view. An arrow should appear. Drag the arrow from the Node.js application to the circle around the MongoDB instance. A tooltip should be visible that says Create service binding. Release the mouse button and a pop-up box will let you specify the name of the service binding. Click Create binding. The container of the Node.js application will restart immediately (Figure 5).

    Check the environment of Node.js application by clicking the Node.js application container in the Topology view. In the right sidebar, click Resources→View Logs (Pods Section) and visit the Environment tab. The SERVICE_BINDING_ROOT environment variable should be set, as shown in Figure 6.

    kube-service-bindings

    The final step is to read the binding data under the directory indicated by the SERVICE_BINDING_ROOT variable and pass the data to the MongoDB client to establish a connection to the MongoDB database. Your Node.js application already has kube-service-bindings as a dependency. So calling the getBinding() function, as shown in the following JavaScript code snippet, does all the hard work of parsing, cleaning, and transforming the binding data into a consumable format for the MongoDB client:

    const { MongoClient } = require("mongodb");
    
    const serviceBindings = require("kube-service-bindings");
    
    const { url, connectionOptions } = serviceBindings.getBinding("MONGODB", "mongodb");
    
    const mongoClient = new MongoClient(url, connectionOptions);

    That's it. By visiting the URL of the Node.js application (click the arrow-box icon on the node), you can perform simple CRUD operations through the UI on the database.

    Easier integration with services on Kubernetes

    Over the past year, our team has been active in developing kube-service-bindings, making it easier for developers with little or no experience in managing containerized applications to securely share credentials among backing services.

    Complementing the work on kube-service-bindings development, our team provides examples for most of the clients supported by kube-service-bindings, instructions on utilizing kube-service-bindings, and a description of how to deploy a variety of backing services through Nodeshift in Kubernetes and OpenShift environments.

    This series of articles has shown which clients are supported and how both a service binding and kube-service-bindings work. We guided you through the whole cycle of deploying a Node.js application backing service using the SBO and kube-service-bindings, sharing and parsing credentials for a connection between a Node.js application and a MongoDB database. kube-service-bindings read, parsed, and transformed binding data projected by the Service Binding Operator, returning data in a form directly consumable by the MongoDB client.

    To help you use kube-service-bindings in other types of deployments, we have provided additional Node.js examples. We hope you found this article interesting and now have a better understanding of kube-service-bindings and service bindings in general.

    If you want to learn more about what Red Hat is up to on the Node.js front, check out our Node.js page.

    Last updated: October 31, 2023

    Recent Posts

    • AI meets containers: My first step into Podman AI Lab

    • Live migrating VMs with OpenShift Virtualization

    • Storage considerations for OpenShift Virtualization

    • Upgrade from OpenShift Service Mesh 2.6 to 3.0 with Kiali

    • EE Builder with Ansible Automation Platform on OpenShift

    What’s up next?

    The Node.js cheat sheet will help you master the most useful command-line flags to customize Node.js’s behavior. You’ll save time and energy looking up how to do everyday development tasks like executing scripts, debugging, and monitoring your Node.js applications.

    Get the cheat sheet
    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
    © 2025 Red Hat

    Red Hat legal and privacy links

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

    Report a website issue