Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

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

    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

    • Tekton joins the CNCF as an incubating project

    • Federated identity across the hybrid cloud using zero trust workload identity manager

    • Confidential virtual machine storage attack scenarios

    • Introducing virtualization platform autopilot

    • Integrate zero trust workload identity manager with Red Hat OpenShift GitOps

    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

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.