Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js scaling out on Kubernetes

I built an app on node.js using Docker and I'm not sure how to scale it on a Kubernetes cluster so that I take the most out of my cluster hardware.

From a performance perspective which of the following is better:

clusterize my node app and run as many containers as needed

or

just run as many containers as needed without clustering ?

When I say clustering I mean this https://nodejs.org/api/cluster.html

My app is a simple CRUD Api backed by mongoDB. We estimate that it will have 1000 concurrent users. Our cluster has 3 nodes.

like image 261
Thiago Caramelo Avatar asked Jan 07 '18 19:01

Thiago Caramelo


People also ask

Is node JS good for scaling?

Scaling your Node. js application horizontally across multiple machines is similar to scaling across multiple cores on a single machine. As long as your application can run as an independent process, it can be distributed to run across several machines.

How do I scale up an application in Kubernetes?

The kubectl scale command lets your instantaneously change the number of replicas you want to run your application. If successful, this command's output should be similar to deployment "my-app" scaled . Note: It may take several minutes for scaling to complete.


1 Answers

The NodeJS cluster mechanism is useful to allow NodeJS to more effectively use greater than a single core, so depending on your code it may benefit you, but it's highly dependent on your code and the various dependencies and how well they work (or not) with clustering.

As a general practice, if you can break your containers down into nicely parallelized efforts that can be run as pods within kubernetes, then I'd recommend the following as a process to see what works for you:

  1. set up a single pod with your code in it, and run a load test against it. Use the data that Kubernetes has from cAdvisor to characterize how much resources (cpu & memory) your pod likes to have.
  2. set a resource limit for cpu and memory based on what you see above.
  3. run a load test to validate what your single pod handles in terms of scale

And from there, you have a baseline where you can use Kubernetes to scale this horizontally to validate the 1000 user concurrent baseline you want to achieve. There's a good talk on this process from the 2017 Kubecon called Load Testing Kubernetes: How to optimize your cluster resource allocation in production

Once you have a baseline, you can run a prototype out leveraging the clustering in your code, and then compare against the non-clustered version. If you do this, I'd double-check that any limits you set are > 1 core for CPU, or you'll be self-limiting outside of the NodeJS runtime to get access to multiple cores, which would defeat the purpose of using clustering.

Depending on what you're doing in your code, there may be significant re-work needed to enable clustering, as it wants to leverage its own worker concept, and it's not clear what frameworks you're using and if they'll fit reasonably into that structure.

like image 146
heckj Avatar answered Oct 29 '22 19:10

heckj