Kubernetes with OpenShift World Tour: Get hands-on experience and build applications fast! Find a workshop!

Build a cloud-ready Express.js application on IBM Cloud

Web applications are powerful applications that have great potential for business and personal use. But when creating, using, and deploying web applications you have to make sure they are reliable, scalable, available, and more. You can handle these important tasks when containerizing and deploying the web application to the cloud.

In this tutorial, we focus in on building a cloud-ready Express.js application on IBM Cloud. This tutorial is an extension of the Building a Cloud Ready Express.js Application workshop. In this tutorial, you will learn how to take the Node.js application made from the original tutorial and deploy it to a Kubernetes cluster on the IBM Cloud.

Prerequisites

To complete the steps in this tutorial, you need to:

  • Have a basic understanding of container technology, including Kubernetes, Docker and Helm
  • Create an IBM Cloud account
  • Create the application from the original tutorial, but you can start with Step 1 (below) first

1. Create a Kubernetes cluster

  1. Create a Kubernetes cluster from the IBM Cloud catalog

    Note: This tutorial can be accomplished with a free cluster. Two optional sections relating to Kubernetes Ingress and custom domain require a paid cluster of type Standard.

  2. Select the Cluster type, choose a Cluster name, and click Create Cluster to provision a Kubernetes cluster.

    alt

  3. Provisioning a cluster takes about 20 minutes. While the cluster is provisioning, you can move on to Step 2 of the tutorial and come back to this step. The cluster will be ready when the state shows Normal. To check the state of the cluster, do the following:

    alt

    1. Select the navigation menu icon in the top left corner to bring up the navigation menu. Then go to Kubernetes.

      alt

    2. Select Clusters from the side menu to bring up the list of clusters you have provisioned. Locate the cluster you provisioned for this tutorial. If the state is Normal as in the photo above, then the cluster is ready to use. Once the state is Normal, click the cluster name to bring up the dashboard for the cluster.

    3. Follow the instructions under Access of your newly created cluster for setting up access to the cluster.

      alt

2. Build a cloud-ready Express.js application

Follow the instructions from Setting up to Step 5 of the original tutorial. In the original tutorial, you:

  • Create an Express.js application
  • Add health checks and metrics to your application
  • Build your application with Docker
  • Package your application with Helm

Once you’ve completed this step, your Kubernetes cluster from step 1 should be provisioned, and you’re ready to move to step 3.

3. Deploy your application to a Kubernetes cluster on IBM Cloud

Now that you have built a Helm chart for your application, the process for deploying your application is greatly simplified.

Deploy your Express.js application into Kubernetes using the following steps:

  1. Find your namespace by listing all the namespace in the registry:

     ibmcloud cr namespaces
    
    • If you do not have a namespace or want to use a new one, then create one by using the following command:

      ibmcloud cr namespace-add <NAMESPACE>
      
  2. Set MYNAMESPACE and MYPROJECT environment variables to your namespace and project name, respectively

     export MYNAMESPACE=<NAMESPACE>
     export MYPROJECT=nodeserver
    
  3. Identify your container registry by running ibmcloud cr region. Set MYREGISTRY env var to your registry.

     export MYREGISTRY=<REGISTRY>
    
  4. Tag the Docker image that is used to create a container to run your app locally by running docker tag nodeserver-run:1.0.0 ${MYREGISTRY}/${MYNAMESPACE}/${MYPROJECT}:v1.0.0

  5. Push the Docker image to your container registry on IBM Cloud:

     ibmcloud cr login
     docker push ${MYREGISTRY}/${MYNAMESPACE}/${MYPROJECT}:v1.0.0
    
  6. Initialize Helm

    • Create a file in chart/nodeserver named rbac.yaml. This file will serve as the configuration for creating a service account with cluster-admin role for the cluster. Copy the following into the file:

      apiVersion: v1
      kind: ServiceAccount
      metadata:
        name: tiller
        namespace: kube-system
      ---
      apiVersion: rbac.authorization.k8s.io/v1
      kind: ClusterRoleBinding
      metadata:
        name: tiller
      roleRef:
        apiGroup: rbac.authorization.k8s.io
        kind: ClusterRole
        name: cluster-admin
      subjects:
        - kind: ServiceAccount
          name: tiller
          namespace: kube-system
      
    • Run the following from the chart/nodeserver directory to set up the configuration:

      kubectl create -f rbac.yaml
      helm init --service-account tiller
      
    • To verify that Helm is running and configured correctly, run helm version and verify that the Client and Server are both running the same version.

  7. Install the Helm chart on the IBM Cloud

    • Modify the chart/nodeserver/values.yaml file to use your image on the IBM Cloud container registry. Run echo ${MYREGISTRY}/${MYNAMESPACE}/${MYPROJECT} and change the repository field to the result of the command. In addition, change the tag field to v1.0.0.

      NOTE: If the container registry ends in bluemix.net then go here to find the updated registry region domain that ends in icr.io and update it for repository in chart/nodeserver/values.yaml

    • Run the following from the chart/nodeserver directory to install the helm chart on the cloud:

      helm install . --name ${MYPROJECT}
      

      If the application successfully deployed, you will see a confirmation message. To access the application that is now running on the cloud, run kubectl get services and locate the PORT for nodeserver-service. Next, go to the cluster dashboard on IBM Cloud and locate the Public IP of the worker node for the cluster.

      alt

      Open a new tab/window in your browser and go to :

      You should now be able to see the application running on the cloud in your browser.

4. Monitor your application

IBM Cloud provides a Kubernetes dashboard for monitoring Kubernetes on the cloud. This can be accessed from your cluster on the IBM Cloud dashboard.

alt

Congratulations! 🎉

You now have an Express.js application deployed using Docker, Helm, and Kubernetes on IBM Cloud!

Max Shapiro