How to Develop and Debug Node.js Applications in Kubernetes

Image of How to Develop and Debug Node.js Applications in Kubernetes

Kubernetes is an open-source project for automating deployment, scaling, and management of containers. It has rapidly become the standard to run production workloads and the community around it is just great!

But developing in Kubernetes presents some challenges. The typical development workflow looks like this: write code, build a Docker image, push it to the registry, redeploy, validate your changes and repeat. This flow is not only slow, but it also prevents us from benefiting from standard features of the Node ecosystem such as hot reload your app as fast as you type your code or debuggers.

Okteto was created to solve this problem. On this blog post, we will show you how Okteto improves the developer experience in Kubernetes for Node developers. You will be able to take full advantage of tools like nodemon, dependency caching or IDE debuggers while developing your application directly in Kubernetes.

Step 1: Deploy the Node Sample App

Get a local version of the Node Sample App by executing the following commands:

1
2
$ git clone https://github.com/okteto/node-getting-started
$ cd node-getting-started

The k8s.yml file contains the Kubernetes manifests to deploy the Node Sample App. Run the application by executing:

You can deploy to your own Kubernetes cluster or give Okteto Cloud a try. Okteto Cloud is a development platform for Kubernetes applications. Free developer accounts come with three Kubernetes namespaces with 8GB of RAM, 4 CPUs, and 50GB Disk space each.

1
$ kubectl apply -f k8s.yml
1
2
deployment.apps "hello-world" created
service "hello-world" created

This is cool! You typed one command and a dev version of your application just runs 😎.

Step 2: Install the Okteto CLI

The Okteto CLI is an open-source project that lets you develop your applications directly in Kubernetes while taking advantage of well-known local tooling. We will use it to speed up our development cycle instead of using the typical development workflow based on building docker images and redeploying containers.

Install the Okteto CLI:

MacOS / Linux
1
$ curl https://get.okteto.com -sSfL | sh
Windows Download https://downloads.okteto.com/cli/okteto.exe and add it to your `$PATH`.

Step 3: Start your development environment in Kubernetes

With the Node Sample Application deployed, run the following command:

1
$ okteto up
1
2
3
4
5
6
7
8
9
 ✓  Development environment activated
✓ Files synchronized
Namespace: rlamana
Name: hello-world
Forward: 3000 -> 3000
9229 -> 9229

Welcome to your development environment. Happy coding!
okteto>

The okteto up command starts a Kubernetes development environment, which means:

  • The Node Sample App container is updated with the docker image okteto/node:10. This image contains the required dev tools to build, test and run the Node Sample App.
  • A file synchronization service is created to keep your changes up-to-date between your local filesystem and your application pods.
  • Container ports 3000 (the application) and 9229 (the debugger) are forwarded to localhost.
  • A remote shell is started in your Kubernetes development environment. Build, test and run your application as if you were in your local machine.

All of this (and more) can be customized via the okteto.yml manifest file.

1
2
3
4
5
6
name: hello-world
image: okteto/node:10
command: ["bash"]
forward:
- 3000:3000
- 9229:9229

You can also use the file .stignore to skip files from file synchronization. This is useful to avoid synchronizing binaries, build artifacts, git metadata or dependencies like node_modules.

To run the application in hot-reload mode, execute in the remote shell:

1
okteto> nodemon app.js
1
2
3
4
5
6
[nodemon] 2.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
Server running at http://0.0.0.0:3000/

Test your application by running the command below in a local shell:

1
$ curl localhost:3000
1
Hello world!

Step 4: Develop directly in Kubernetes

Open the app.js file in your favorite local IDE and modify the response message on line 9 to be Hello world from the cluster!. Save your changes.

1
res.end(`Hello world from the cluster!`);

Okteto will synchronize your changes to your development environment in Kubernetes. Take a look at the remote shell and notice how the changes are detected by nodemon and automatically hot reloaded.

1
2
3
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
Server running at http://0.0.0.0:3000/

Okteto automatically forwards port 8080 from your local computer to the remote development environment, making it accessible via localhost. Test your application by running the command below in a local shell:

1
$ curl localhost:3000
1
Hello world from the cluster!

Cool! Your code changes were instantly applied to Kubernetes. No commit, build or push required 😎!

Step 5: Debug directly in Kubernetes

Okteto enables you to debug your applications directly from your favorite IDE. Let’s take a look at how that works in VS Code, one of the most popular IDEs for Node development. If you haven’t done it yet, install the Node.js extension available from Visual Studio marketplace.

Cancel the execution of nodemon app.js from the remote shell by pressing ctrl + c. Rerun your application in debug mode:

1
okteto> node --inspect-brk=0.0.0.0:9229 app.js
1
2
Debugger listening on ws://0.0.0.0:9229/73d8d793-b0c3-4310-86ee-3a42938a5df1
For help, see: https://nodejs.org/en/docs/inspector

Open the Debug extension and run the Connect to okteto debug configuration (or press the F5 shortcut):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"version": "0.2.0",
"configurations": [
{
"name": "Connect to okteto",
"type": "node",
"request": "attach",
"address": "localhost",
"port": 9229,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/src",
"skipFiles": [
"<node_internals>/**"
]
},
]
}

Add a breakpoint on app.js, line 9, and call your application by running the command below from a local shell.

1
$ curl localhost:3000

The execution will halt at your breakpoint. You can then inspect the request, the available variables, etc…

Conclusions

Kubernetes has the potential to be a great development platform, providing replicable, resource-efficient and production-like development environments. We have shown you how to use Okteto to create a development workflow that also lets you take advantage of features like hot reloaders or debuggers while developing your application directly in Kubernetes.

Accelerate your development and start developing directly in Kubernetes today. Let us know what you think about it on Twitter, or in our #okteto channel in the Kubernetes community Slack.