Blog/How to Scale a Node.js App with MongoDB on Kubernetes Using Helm (Replica Set, Custom Chart, and HPA)

KubernetesHelmNode.jsMongoDBDevOps··greencrew

How to Scale a Node.js App with MongoDB on Kubernetes Using Helm (Replica Set, Custom Chart, and HPA)

To scale a Node.js app with MongoDB on Kubernetes, install MongoDB as a replica set with Helm (architecture=replicaset, replicaCount=3), point your app at the headless-service connection string stored in a Secret, package the app as its own chart with helm create, deploy it with helm upgrade --install, and scale with replicaCount or a HorizontalPodAutoscaler. This guide covers values.yaml, the connection string, templating, autoscaling, rollbacks, and troubleshooting.

Short answer: Install Helm, then deploy MongoDB from a chart with architecture: replicaset and replicaCount: 3, so you get a StatefulSet with one persistent volume per member. Your Node.js app connects with a replica set URI that lists each pod through the chart's headless Service (mongo-mongodb-0.mongo-mongodb-headless...:27017) and includes replicaSet=rs0. Keep that URI in a Kubernetes Secret. Scaffold a chart for the app with helm create, inject the Secret as an environment variable, and ship it with helm upgrade --install. You scale the stateless app with --set replicaCount=5 or a HorizontalPodAutoscaler, and you scale MongoDB by raising its replica count. If a release breaks, helm rollback returns it to the previous revision.

What Is Helm, and Why Use It for Node.js and MongoDB?

Helm is the package manager for Kubernetes. A chart is a folder of templated Kubernetes manifests plus a values.yaml file of defaults. A release is one installed instance of a chart. Each helm upgrade creates a new numbered revision, and you can roll back to any of them.

A Node.js + MongoDB stack has two very different halves:

ComponentKubernetes objectStateHow it scales
Node.js APIDeploymentStatelessAdd pods freely (replicaCount, HPA)
MongoDBStatefulSet + headless ServiceStateful (PersistentVolumes)Add replica set members; scale reads, not writes

Helm lets you manage both with the same workflow. For MongoDB you use a maintained community chart. For your app you write a small chart of your own.

If you haven't containerized the app yet, start with how to build a Node.js application with Docker. If you want a plain-manifest version first, without Helm, read how to deploy an Express app on Kubernetes.

Prerequisites

  • A Kubernetes cluster with a default StorageClass. A local or single-node cluster works fine. See how to set up a MicroK8s Kubernetes cluster on Ubuntu.
  • kubectl configured for that cluster (kubectl get nodes returns Ready)
  • A Node.js app image pushed to a registry your cluster can pull from
  • metrics-server if you want autoscaling (Step 8)

Step 1: Install Helm

Helm is a single binary. On Ubuntu, the snap is the quickest option:

sudo snap install helm --classic
helm version

For other platforms or the official install script, use the Helm installation guide. Helm uses the same kubeconfig as kubectl, so no further setup is needed.

Create a namespace for the stack:

kubectl create namespace shop

Step 2: Choose a MongoDB Chart or Operator

The Bitnami MongoDB chart has long been the default way to run MongoDB with Helm, and the rest of this guide uses its value names. Note that in 2025 Bitnami changed how it distributes its free container images and catalog. Before you install, check the Bitnami charts repository for the current image source and any values you need to set, and don't copy old image tags from blog posts.

The main alternative is the MongoDB Community Kubernetes Operator (MongoDB has since been folding it into its unified MongoDB Controllers for Kubernetes project). With an operator, you install a controller once and then declare a replica set as a custom resource.

OptionYou manageBest for
Bitnami Helm chartOne values.yaml, helm upgradeQuick, familiar Helm workflow
MongoDB operatorOperator + MongoDBCommunity resourceLong-lived clusters, operator-driven upgrades
Managed MongoDB (Atlas, etc.)Just the connection stringTeams that don't want to run databases

Whichever you pick, the app side (Steps 4–8) is the same: it only needs a replica set connection string.

Step 3: Install MongoDB as a Replica Set with Helm

Create mongo-values.yaml:

architecture: replicaset
replicaCount: 3
replicaSetName: rs0

auth:
  enabled: true
  rootPassword: "change-me-root"
  usernames:
    - shopapp
  passwords:
    - "change-me-app"
  databases:
    - shop

# Not needed with an odd number of data members
arbiter:
  enabled: false

persistence:
  enabled: true
  size: 10Gi

resources:
  requests:
    cpu: 250m
    memory: 512Mi
  limits:
    memory: 1Gi

Install it as a release named mongo (the chart is published as an OCI artifact):

helm install mongo oci://registry-1.docker.io/bitnamicharts/mongodb \
  --namespace shop \
  -f mongo-values.yaml

Watch the members come up one at a time:

kubectl -n shop get pods -l app.kubernetes.io/name=mongodb -w
kubectl -n shop get pvc

The chart creates a StatefulSet (mongo-mongodb), one PersistentVolumeClaim per member, and a headless Service (mongo-mongodb-headless). A headless Service gives every pod a stable DNS name, which is exactly what a replica set needs.

Keep real passwords out of Git. In production, create a Secret yourself and reference it with the chart's auth.existingSecret value instead of putting passwords in the values file.

MongoDB chart values reference

ValuePurposeExample
architecturestandalone or replicasetreplicaset
replicaCountNumber of data-bearing members3
replicaSetNameName used in replicaSet= in the URIrs0
auth.rootPasswordAdmin passwordfrom Secret
auth.usernames / passwords / databasesApp user created at initshopapp / shop
auth.existingSecretUse your own Secret for passwordsmongo-auth
arbiter.enabledVoting member with no datafalse with 3 members
persistence.sizePVC size per member10Gi

Step 4: Build the Replica Set Connection String

Pod DNS names behind a headless Service follow this pattern:

<pod>.<headless-service>.<namespace>.svc.cluster.local

So the connection string for this release is:

mongodb://shopapp:change-me-app@mongo-mongodb-0.mongo-mongodb-headless.shop.svc.cluster.local:27017,mongo-mongodb-1.mongo-mongodb-headless.shop.svc.cluster.local:27017,mongo-mongodb-2.mongo-mongodb-headless.shop.svc.cluster.local:27017/shop?replicaSet=rs0&authSource=shop

Listing every member and adding replicaSet=rs0 lets the MongoDB driver find the current primary and fail over automatically. See MongoDB's connection string reference. Use authSource=shop because the chart creates the app user in the shop database.

Store it in a Secret:

kubectl -n shop create secret generic shop-api-mongo \
  --from-literal=MONGODB_URI='mongodb://shopapp:change-me-app@mongo-mongodb-0.mongo-mongodb-headless.shop.svc.cluster.local:27017,mongo-mongodb-1.mongo-mongodb-headless.shop.svc.cluster.local:27017,mongo-mongodb-2.mongo-mongodb-headless.shop.svc.cluster.local:27017/shop?replicaSet=rs0&authSource=shop'

Test the app user from inside the first MongoDB pod before you deploy the app:

kubectl -n shop exec -it mongo-mongodb-0 -- \
  mongosh "mongodb://shopapp:change-me-app@localhost:27017/shop?authSource=shop" --eval 'rs.status().members.map(m => m.stateStr)'

You should see one PRIMARY and two SECONDARY members.

Step 5: Make the Node.js App Kubernetes-Friendly

Read the URI from the environment, expose a health route, and shut down cleanly on SIGTERM. A minimal Express + Mongoose server.js:

const express = require('express');
const mongoose = require('mongoose');

const app = express();
const port = process.env.PORT || 3000;

app.get('/', (req, res) => res.send('shop-api ok'));

// Readiness: only receive traffic when connected to MongoDB
app.get('/health', (req, res) => {
  const ok = mongoose.connection.readyState === 1;
  res.status(ok ? 200 : 503).json({ mongo: ok ? 'up' : 'down' });
});

mongoose.connect(process.env.MONGODB_URI)
  .then(() => console.log('Connected to MongoDB'))
  .catch((err) => { console.error(err); process.exit(1); });

const server = app.listen(port, '0.0.0.0', () => console.log(`Listening on ${port}`));

process.on('SIGTERM', () => {
  server.close(async () => {
    await mongoose.disconnect();
    process.exit(0);
  });
});

Build and push the image, for example registry.example.com/shop-api:1.0.0. Because the app is stateless, any number of copies can run against the same replica set.

Step 6: Create a Helm Chart for the Node.js App

Scaffold a chart:

helm create shop-api

That generates:

FileWhat it does
Chart.yamlChart name, chart version, and appVersion
values.yamlDefaults: image, replicas, service, probes, resources
templates/deployment.yamlThe Deployment, templated from values
templates/service.yamlClusterIP Service in front of the pods
templates/hpa.yamlHorizontalPodAutoscaler (off by default)
templates/ingress.yamlOptional Ingress
templates/_helpers.tplName and label helpers

Edit shop-api/values.yaml. These are the parts you change:

replicaCount: 2

image:
  repository: registry.example.com/shop-api
  pullPolicy: IfNotPresent
  tag: "1.0.0"

service:
  type: ClusterIP
  port: 3000

# Name of the Secret holding MONGODB_URI
mongoSecretName: shop-api-mongo

livenessProbe:
  httpGet:
    path: /
    port: http
readinessProbe:
  httpGet:
    path: /health
    port: http

resources:
  requests:
    cpu: 100m
    memory: 128Mi
  limits:
    memory: 256Mi

autoscaling:
  enabled: false
  minReplicas: 2
  maxReplicas: 10
  targetCPUUtilizationPercentage: 70

In templates/deployment.yaml, find the container spec and add the environment block below imagePullPolicy. The scaffold already wires containerPort to .Values.service.port and names it http:

          env:
            - name: PORT
              value: {{ .Values.service.port | quote }}
            - name: MONGODB_URI
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.mongoSecretName }}
                  key: MONGODB_URI

The scaffold's Deployment only sets replicas: {{ .Values.replicaCount }} when autoscaling.enabled is false, so the HPA and Helm don't fight over the replica count. Check the rendered output before you install:

helm lint ./shop-api
helm template shop-api ./shop-api -n shop | less

See the Helm chart template guide for the template syntax.

Step 7: Deploy with helm upgrade --install

Use one command for both the first install and every later update:

helm upgrade --install shop-api ./shop-api \
  --namespace shop \
  --set image.tag=1.0.0 \
  --wait

--install creates the release if it doesn't exist, and --wait blocks until the pods pass their readiness probes. Verify:

helm -n shop list
kubectl -n shop get deploy,pods,svc -l app.kubernetes.io/instance=shop-api
kubectl -n shop port-forward svc/shop-api 3000:3000
curl localhost:3000/health

To expose the app publicly, enable the chart's Ingress and add TLS. See how to secure Kubernetes with cert-manager, Traefik, and Let's Encrypt.

Step 8: Scale the Node.js App (Manually and with an HPA)

Manual scaling goes through Helm so the value is recorded in the release:

helm upgrade shop-api ./shop-api -n shop --reuse-values --set replicaCount=5

Avoid kubectl scale on Helm-managed Deployments. The next helm upgrade will reset the count to whatever is in your values.

Autoscaling uses a HorizontalPodAutoscaler, which needs metrics-server and CPU requests on the pods (we set 100m). On MicroK8s, run microk8s enable metrics-server. Elsewhere, follow the metrics-server README. Then:

kubectl top pods -n shop      # confirms metrics work
helm upgrade shop-api ./shop-api -n shop --reuse-values \
  --set autoscaling.enabled=true \
  --set autoscaling.minReplicas=2 \
  --set autoscaling.maxReplicas=10 \
  --set autoscaling.targetCPUUtilizationPercentage=70
kubectl -n shop get hpa -w

The HPA adds pods when average CPU passes 70% of the request and removes them when load drops. Each pod is a single Node.js process. Kubernetes scales by adding pods, so you usually don't need the in-process cluster module from how to scale Node.js applications with clustering.

Step 9: Scale MongoDB

Scaling MongoDB is different from scaling the app. A replica set has one primary that accepts all writes, and the extra members provide redundancy and optional read capacity.

helm upgrade mongo oci://registry-1.docker.io/bitnamicharts/mongodb \
  -n shop -f mongo-values.yaml --set replicaCount=5
GoalWhat to do
Survive one node failure3 data members (majority = 2)
Survive two node failures5 data members
Offload readsAdd readPreference=secondaryPreferred to the URI for read-tolerant queries
Scale writesBigger primary (resources), better indexes, or sharding

After adding members, update the MONGODB_URI Secret to list them (drivers discover new members on their own, but a complete seed list helps on cold starts). Then restart the app with kubectl -n shop rollout restart deploy/shop-api. Keep an odd number of voting members, and spread them across nodes with pod anti-affinity if your cluster has more than one node.

Step 10: Roll Back a Bad Release

Every upgrade is a revision:

helm -n shop history shop-api
helm -n shop rollback shop-api 3

The rollback re-applies revision 3's manifests and values. It doesn't touch MongoDB data, so if a release ran a destructive migration, restore from backup rather than relying on a rollback. Back up with mongodump or volume snapshots before major upgrades.

Helm Commands Cheat Sheet

CommandWhat it does
helm create <name>Scaffold a new chart
helm lint ./chartCheck a chart for errors
helm template <rel> ./chartRender manifests without installing
helm install <rel> <chart> -f values.yamlInstall a release
helm upgrade --install <rel> <chart>Install or upgrade (idempotent)
helm upgrade <rel> <chart> --reuse-values --set k=vChange one value
helm get values <rel>Show the values a release is using
helm list -n <ns>List releases
helm history <rel>List revisions
helm rollback <rel> <rev>Return to a previous revision
helm uninstall <rel>Remove a release (PVCs from StatefulSets are kept)

The full list is in the Helm commands docs.

Troubleshooting Node.js and MongoDB on Kubernetes

SymptomCauseFix
MongoDB pods stuck PendingNo default StorageClass, PVC can't bindkubectl get sc; enable a storage addon or set persistence.storageClass
App logs MongoServerSelectionErrorWrong host, missing replicaSet=, or members not readyTest DNS with nslookup mongo-mongodb-0.mongo-mongodb-headless.shop; check rs.status()
Authentication failedWrong authSource or password changed after first installUse authSource=shop; passwords are set at init, so change them inside MongoDB, not just in values
App pods 0/1 Ready/health returns 503 until Mongo connectsCheck the Secret name/key and the app logs
HPA shows <unknown>/70%metrics-server missing or no CPU requestsInstall metrics-server; set resources.requests.cpu
Replica count snaps back after upgradeUsed kubectl scale on a Helm releaseScale with helm upgrade --set replicaCount= or enable the HPA
ImagePullBackOffImage tag or registry auth wrongkubectl describe pod; add imagePullSecrets in values
502 from the ingress/proxyNo ready endpoints behind the Servicekubectl get endpoints shop-api; see how to fix 502 Bad Gateway

Monitor the Public Endpoint

Kubernetes probes restart unhealthy pods, but they run inside the cluster. They won't tell you when the ingress, DNS, certificate, or the whole cluster is unreachable. Add an outside check on the same /health route. Because that route returns 503 when MongoDB is down, one HTTP check covers both the API and the database.

GreenCrew monitors your URL over HTTP and alerts you on Discord, Slack, or Telegram when it stops responding correctly. It also gives you a public status page to share with users during an incident. See how to get website downtime alerts on Discord, Slack, and Telegram.

Frequently Asked Questions

Can I scale MongoDB writes by adding replica set members?

No. All writes go to the single primary, so extra members add redundancy and can serve reads, but they don't add write capacity. To scale writes, give the primary more resources, tune indexes, or move to a sharded cluster.

Why does the connection string list every pod instead of one Service?

The driver needs to reach each member directly to discover which one is primary and to fail over. A normal ClusterIP Service would load-balance across members and send writes to secondaries. The headless Service gives each pod a stable DNS name for the seed list.

Should I put MongoDB in the same Helm chart as my app?

Usually not. Keep them as separate releases. The database has a different lifecycle (rare, careful upgrades, persistent data), while the app is redeployed often. Separate releases also mean helm rollback shop-api can never touch the database. If you need a relational database instead, see how to deploy PostgreSQL on Kubernetes.

Do I still need Nginx or PM2?

Not inside the cluster. Kubernetes restarts crashed pods (the job PM2 does on a VM) and the Service load-balances across them. You still need an ingress controller such as Traefik or Nginx Ingress for public traffic. For a single-server setup without Kubernetes, PM2 with Nginx is simpler.

Conclusion

Running Node.js and MongoDB on Kubernetes with Helm comes down to treating the two halves differently. Install MongoDB as a three-member replica set from a chart or operator, and connect to it with a headless-service URI that includes replicaSet=rs0 and lives in a Secret. Package the stateless API as its own chart with helm create, deploy it with helm upgrade --install, and scale it with replicaCount or an HPA. Use helm rollback when a release goes wrong. Then add an outside HTTP check on /health with GreenCrew, so you get a Discord, Slack, or Telegram alert the moment your API or database stops answering.