Nodejs App From Code To Kubernetes Cluster
Tags: kubernetes, javascript, react, opensource
Published: 2021-11-02 00:50:27

## TL;DR

* [Ketch](https://github.com/shipa-corp/ketch) automatically builds Docker images from your application source code
* [Ketch](https://github.com/shipa-corp/ketch) saves you time by creating all the required components to run your application on Kubernetes and exposing it through an endpoint without you dealing with infrastructure

## The Pressure to Deliver

Many of us now have to deploy our applications on Kubernetes.

While this is great and introduces you to an opportunity to learn new technologies, the reality is that you are often swamped with work, under pressure to deliver your applications, and you want to go from ‘code to application’ as fast as possible.

What conflicts with that goal is that Kubernetes introduces different infrastructure-level concepts and requirements that take our time away from delivering our application code.

Most of us had an opportunity to experience Heroku. From a developer’s perspective, the experience was great, and we could go from ‘code to application’ fast and without dealing with infrastructure complexity.

So how can we get the same level of experience but on top of Kubernetes, giving our Ops team the freedom to adopt Kubernetes but still maintain an application layer on top of it to guarantee developer experience?

This article will show you how you can get a starter NodeJS server deployed on Kubernetes. The goal is to not deal with infrastructure complexities and not even have to build a Docker file, going from ‘code to application.’

## Prerequisites

* You need NodeJS installed to start development
* Docker in your local machine
* You need to be logged into your container registry
* Access to a Kubernetes cluster and kubectl configured
* [Ketch installed](https://learn.theketch.io/docs/getting-started) and available in that cluster
* [Ketch CLI available](https://learn.theketch.io/docs/getting-started#ketch-cli) in your local machine
* You have a [framework created in Ketch](https://learn.theketch.io/docs/getting-started#creating-a-framework) where you will deploy your application.

For the Kubernetes cluster, I will be using a GKE (Google Kubernetes Engine) I have set up, but you can use local clusters directly from your laptops, such as [K3s](https://k3s.io/) or [Minikube](https://minikube.sigs.k8s.io/docs/start/).

For [Ketch](https://github.com/shipa-corp/ketch), once you have your cluster available and kubectl access configured, you can quickly install Ketch by [following the instructions here](https://learn.theketch.io/docs/getting-started)

## Step 1: Initializing the Node Application

First, create a separate directory. 

From within the directory, you can initialize the project with npm (Node Package Manager).

Running `npm init` will prompt you with basic configuration questions, such as your project name, version, and others. Keeping the default values is good enough for our sample application.

```
❯ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (nodejs-sample) 
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /Users/brunoandrade/ketch/apps/nodejs-sample/package.json:

{
  "name": "nodejs-sample",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Is this OK? (yes) 

~/ketch/apps/nodejs-sample ❯  
```

## Step 2: Installing Express

Next, you will install Express. You will leverage the Express framework to build web applications and APIs. Use npm to install it.

```
❯ npm install express -save

added 50 packages, and audited 51 packages in 2s

found 0 vulnerabilities
```

The code above imports the Express module using a require function to return an object to configure your application.

## Step 4: Deploying the Code

This is the fun part, having your application deployed directly from code into Kubernetes. Ketch will automatically build the Docker image for you, push it to your container registry of choice, create the required Kubernetes objects to run your application, and create an endpoint where you can access your application.

If you are using Kubernetes directly, you would need to learn and deal with services, deployments, and more, but because you are using Ketch, you will do it all without dealing with any of that!

```
ketch app deploy nodejs-sample . -i shiparepo/nodejs-sample:0.1 -k dev-framework
```

Breaking down the command above:

* You use App deploy to deploy your application
* Nodejs-sample is the name of the application
* The ‘.’ is the path to your application source code
* ‘-i shiparepo/nodejs-sample:0.1’ is the name and version of the image I want Ketch to use when creating and pushing the Docker image to my registry.
* Use the framework you created before with the ‘-k dev-framework’ flag

## Step 5: Checking your Application

Now, you can check your application deployment status and endpoint by running the ketch app list command.

```
❯ ketch app list
NAME             FRAMEWORK         STATE          ADDRESSES        BUILDER                                                         
nodejs-sample    dev-framework     1 deploying    http://nodejs-sample.35.230.16.206.shipa.cloud    heroku/buildpacks:20   
```

In the result, you can see that Ketch automatically configured the ingress controller and created the endpoint for your application.

## Step 6: There is No Step 6 🙂

You have successfully deployed a sample NodeJS application on Kubernetes!

The team is excited about enabling developers to focus on their application code instead of infrastructure. We would love it if you could show your support by [starring the project on GitHub](https://github.com/shipa-corp/ketch) and sharing this article with your teammates.