kubernetes

Bitnami How-To Guides for Kubernetes

Deploy a MEAN Application on Kubernetes with Helm

Introduction

MEAN is an opinionated full-stack Javascript framework which simplifies and accelerates web application development. It is a popular choice for developers who want to create web applications with ease. It is based on the Node.js language and it allows you to write less code with its predefined layout and additional libraries for simplifying the programming of web applications.

Running your application in a production environment like Kubernetes is the best way to automate the deployment, scaling and management of the operations of the application containers. To deploy and manage the application containers in a Kubernetes cluster, you can use Helm charts.

This guide walks you through the process of running an example MEAN application on a Kubernetes cluster. It uses a simple MEAN application which exploits HTML5 geo-location features to show your location on a map. This location can be saved in a MongoDB database, and you can see other people’s locations too. After showing you how to deploy your application in a Kubernetes cluster using the Bitnami Node.js Helm chart (which includes MongoDB by default), this guide also explores how to modify the source code and publish a new application release in Kubernetes using the Helm CLI.

Assumptions and prerequisites

This guide focuses on deploying a custom MEAN application in a Kubernetes cluster. This guide makes the following assumptions:

This guide assumes that you already have your MEAN application code in GitHub but, if you don’t, you can fork this example application. This example application uses MEAN and is referenced in the Create a MEAN Stack Google Map App tutorial.

Step 1: Adapt the application source code

As a first step, you must adapt your application’s source code to expose the MongoDB parameters as environment variables so that you can later connect the application with the Bitnami Node.js Helm chart. The steps below explain how to do this for the example application which you should have forked into your own repository.

  • Edit the app/config.js file so that it looks like this and then save your changes:

    module.exports = {
    
        bitnami:
        {
            name: "MongoDB Service",
            url: "mongodb://" + process.env.DATABASE_USERNAME + ":" + process.env.DATABASE_PASSWORD + "@" + process.env.DATABASE_HOST + "/" + process.env.DATABASE_NAME,
            port: process.env.DATABASE_PORT
        }
    
    };
    

    Change the MongoDB connection string in your custom application in a similar manner.

  • Next, edit the server.js file and update the connection URL as follows:

    ...
    // Express Configuration
    // -----------------------------------------------------
    // Sets the connection to MongoDB
    mongoose.connect(database.bitnami.url);
    ...
    

    Change the connection URL in your custom application in a similar manner if needed.

  • Check and update application dependencies to ensure it is using the latest and most stable version of each package by editing the package.json file. For the example application, update the dependencies as below:

    "dependencies": {
        "body-parser": "^1.19.0",
        "express": "^4.17.1",
        "jsonwebtoken": "^5.0.2",
        "method-override": "^3.0.0",
        "mongoose": "^5.6.1",
        "morgan": "^1.9.1"
    }
    

Step 2: Deploy the example application in Kubernetes

NOTE: Before performing the following steps, make sure you have a Kubernetes cluster running, and that Helm and Tiller are installed correctly. For detailed instructions, refer to our starter tutorial.

Once your application code has been adapted, the next step is to deploy it on your Kubernetes cluster. Bitnami’s Node.js Helm chart makes it very easy to do this, because it has built-in support for Git repositories and also installs MongoDB by default. So, to deploy the example application using the current Helm chart, follow these steps:

  • Make sure that you can to connect to your Kubernetes cluster by executing the command below:

    $ kubectl cluster-info
    
  • Deploy the application by executing the following (Replace the my-mean-app example name with the name you want to give your application and the GitHub repository URL with the correct URL for your application repository):

    $ helm install --name my-mean-app --set repository=https://github.com/USERNAME/MeanMapAppV2.0.git bitnami/node
    

    This will create two pods within the cluster, one for the MongoDB service and one for the application itself.

    Deploy Helm chart on Kubernetes

    TIP: You can also specify a Git tag if you wish to check out a particular branch of the repository. See the complete list of supported parameters for the chart.

Once the chart has been installed, you will see a lot of useful information about the deployment. The application won’t be available until database configuration is complete. Run the kubectl get pods command to see pod status and get a list of running pods:

$ kubectl get pods

Check pod status

To obtain the application URL, run the commands shown in the “Notes” section. For example, the command below will allow you to access the application by browsing to the URL http://127.0.0.1:8080 on your host:

$ kubectl port-forward --namespace default svc/my-mean-app-node 8080:80

Here is an example of what you should see:

Access application URL

Congratulations! You’ve successfully deployed your MEAN application on Kubernetes.

IMPORTANT: HTML5 Geolocation is a technology that offers location services for web applications. It’s included by default on all major browsers, but for security reasons, it may require user action to work. Select the option “Always allow on this site” in Google Chrome or click “Allow Location Access” in Mozilla Firefox.

Step 3: Update the source code and re-deploy the application

To re-deploy a modified version of the application, you need to carry out a few basic steps: change your application source code, commit your changes and update the chart.

  • Edit the file public/index.html and in the Header-Title section, change the page title

    <title>The Scotch MEAN MapApp</title>
    

    to

    <title>My new MEAN MapApp</title>
    
  • Execute the helm upgrade command to update your Helm chart:

    $ helm upgrade my-mean-app bitnami/node --recreate-pods
    
  • Refresh your browser and you will see the modified title on the application welcome page:

    Access application release

Follow these steps every time you want to update and re-release your application.

To learn more about the topics discussed in this guide, use the links below:

Last modification June 27, 2019