If you're a developer working on a Node.js application, you may have wondered about the best way to run it in a production environment. For example:
- You'd probably like to deploy it in the cloud, to ensure maximum flexibility and optimum resource utilization.
- You'd want it to be easy to scale and highly available, to make sure that your users have a good experience.
- To keep things as simple as possible, you'd probably also want to deploy it directly from your Git repository.
Bitnami makes it easy to meet all these requirements. Bitnami's Node.js High-Availability Cluster provides a multi-node Node.js cluster with a shared filesystem and load balancing capabilities. It includes the ability to deploy your Node.js application directly from a Git repository to your cluster, and can be easily integrated with an external MongoDB database, such as Cosmos DB, to enable scalability and geographic redundancy.
Here's the topology of Bitnami's Node.js High-Availability Cluster:
This guide walks you through the process of deploying a custom MEAN (MongoDB, Express, Angular and Node.js) application on Microsoft Azure using Bitnami's Node.js High-Availability Cluster and Cosmos DB service. This guide uses a simple to-do list application as an example, but you can use similar steps to deploy your own Node.js application on Microsoft Azure.
Assumptions and prerequisites
This guide makes the following assumptions:
- You have a Microsoft Azure account. If you don't, create a Microsoft Azure account.
- You have a custom Node.js application in a Git repository.
- Your Node.js application is compatible with Bitnami's Node.js High-Availability Cluster. In case it isn't, or if you don't have a custom Node.js application, use our sample application.
Step 1: Deploy a Cosmos DB instance
The example Node.js application used in this guide needs a MongoDB database. Since MongoDB is not included with Bitnami's Node.js High-Availability Cluster, you must deploy this separately. The easiest way to do this is with the Azure Cosmos DB service, which provides a scalable, high-availability database in the cloud.
As a first step, go ahead and launch a new Azure Cosmos DB instance, as follows:
- Browse to the Microsoft Azure management console and log in if needed.
- From the menu bar on the left, select the "Azure Cosmos DB" item.
- Click "Add" to add a new Cosmos DB instance.
- Enter a unique name for your new database instance in the "ID" field and select "MongoDB" as the API.
In the same tab, create a new resource group by entering a name for it, and choose the location for the server to be deployed. Click "Create" once done.
You will see a notification regarding the new deployment. Click "Refresh" to check the status, which should cycle through various messages until it eventually displays "Online" (this typically takes 2-3 minutes). At this point, your new Azure Cosmos DB instance is deployed, and should appear in the list of available instances.
Once the instance is online, select the newly-created instance and then select the "Settings -> Connection String" menu item. In the "Read-write Keys" tab, you will find the database host name, port, username and primary password. You will need these details when deploying your Node.js application to the cluster in the next step. Therefore, keep this tab open for the moment so that it's easier for you to copy these details over in the next step.
While there, click the "Settings -> Replicate data globally" menu item to understand and configure how your data is replicated. The "Write" region refers to the primary region you specified, while specifying multiple "Read" regions ensures high availability. You can add/remove read regions by clicking the plus and minus symbols, and you can also implement a manual or automatic failover policy using the buttons in the top right corner of the page.
Step 2: Deploy a Node.js application
The next step is to launch the Bitnami Node.js High-Availability Cluster solution with a Node.js application. The Azure Marketplace lets you do this in just a couple of clicks. Follow these steps:
- Select the Bitnami Multi-Tier Solution for Node.js High-Availability Cluster in the Azure Marketplace.
- On the description page, review the information and fees. Click the "Get it now" button to proceed and confirm.
- You will now be redirected to the Microsoft Azure management console. Log in if required.
On the solution description page, review the available information and click the "Create" button to proceed.
In the "Basics" tab, enter an easy-to-remember name for the deployment together. By default, the solution is configured with the URL to Bitnami's sample MEAN application, but you can edit this to reflect the Git repository URL for your own application.
NOTE: Ensure that your application complies with these requirements before proceeding. TIP: You can use a private repository by including the username and password in the repository URL, in the format https://USERNAME:PASSWORD@URL.
In the same tab, enter the host name, port, username and password for your Cosmos DB instance (you can copy this over from the screen you left open in Step 1), a name for the application database and the number of nodes for the cluster. Enter the string ssl=true&replicaSet=globaldb in the "Database connection options" field and create a new resource group by entering a name for it and choosing its location. Click "OK" when done.
In the "Environment Configuration" tab, enter a password or an SSH public key and choose the size of the virtual machines in the deployment. For more information on available sizes and configurations, refer to the Microsoft Azure pricing sheet.
NOTE: To generate a new SSH key pair, you can use PuTTYgen (Windows) or the ssh-keygen command (Linux and Mac OS X). Find instructions on using PuTTYgen and instructions on using ssh-keygen.
In the "Summary" tab, verify the chosen settings. Click "OK" to proceed.
Microsoft Azure will now begin spinning up the new cluster. A notification will appear indicating the current status.
The process can take up to 40 minutes as the various nodes in the cluster are created and the load balancer is initialized. Once all the necessary elements of the solution been provisioned, you will see a notification and the new instances and related resources will appear in the "Resource groups" section of the Microsoft Azure management console.
Look closely at the image above and note these important points:
- The resource group includes multiple virtual machines (the nodes of the cluster) as well as a load balancer.
- Each virtual machine is identified with a numeric suffix, corresponding to the number of nodes requested at deployment time.
- Each virtual machine has its own NIC.
Select the resource group and then the "Settings -> Deployments" menu item. Select the deployment and obtain the auto-generated domain name for the load balancer from the "FQDN" field in the "Outputs" tab.
Step 3: Test the deployed application
Enter the load balancer's domain name directly into your browser's address bar. You should now see your application. The image below shows the sample application's welcome page:
|NOTE: If you see a 502 server error instead of the sample application, this is usually a transient condition that occurs when the deployment is still in progress and the Node.js instances are not yet up and running. Try refreshing your browser after a few minutes.|
Verify that the application is able to read and write data using the MongoDB database configured earlier. With the example application, enter a new to-do item and it should appear in the list, as shown below:
|NOTE: Find out more about the database configuration variables used by the sample MEAN application.|
Your Node.js application is now deployed, configured for use with Cosmos DB, and available for use in high-availability scenarios. For more information, including details about how to configure SSL and connect to the individual nodes of the cluster using SSH, refer to the documentation for Bitnami's Node.js High-Availability Cluster.