AskHandle

AskHandle Blog

Running Node.js Apps in Kubernetes

December 27, 2025Emily Henderson3 min read

Running Node.js Apps in Kubernetes

As a developer who works with Node.js applications daily, I've found that running them in Kubernetes brings a new level of flexibility and scalability to my projects. Let me share my experiences and practical tips for getting your Node.js applications up and running in Kubernetes.

Setting Up Your Node.js App for Kubernetes

Before moving your Node.js application to Kubernetes, you need to make sure it's container-ready. First, create a solid Dockerfile. Here's a basic example I use for most projects:

dockerfile
1FROM node:18-alpine
2WORKR DIR /app
3COPY package*.json ./
4RUN npm install
5COPY . .
6EXPOSE 3000
7CMD ["node", "app.js"]

I prefer using the Alpine-based Node.js image because it's lightweight and includes only the necessary components. This keeps the container size small and reduces potential security risks.

Container Health Checks

Your Node.js application should include health check endpoints. I add these routes to check if my app is running properly:

javascript
1app.get('/health', (req, res) => {
2  res.status(200).send('OK');
3});
4
5app.get('/ready', (req, res) => {
6  res.status(200).send('Ready');
7});

These endpoints help Kubernetes monitor your application's status and manage traffic effectively.

Kubernetes Configuration

Creating Kubernetes manifests is the next step. I split my configurations into separate files for better organization. The deployment file looks like this:

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: nodejs-app
5spec:
6  replicas: 3
7  selector:
8    matchLabels:
9      app: nodejs-app
10  template:
11    metadata:
12      labels:
13        app: nodejs-app
14    spec:
15      containers:
16      - name: nodejs-app
17        image: your-registry/nodejs-app:1.0.0
18        ports:
19        - containerPort: 3000

Environment Variables and Secrets

I store configuration settings in Kubernetes ConfigMaps and Secrets. This makes it easy to change settings without rebuilding containers. Here's an example ConfigMap:

yaml
1apiVersion: v1
2kind: ConfigMap
3metadata:
4  name: nodejs-config
5data:
6  NODE_ENV: "production"
7  API_URL: "https://api.example.com"

Scaling and Updates

One thing I love about running Node.js in Kubernetes is automatic scaling. You can set up Horizontal Pod Autoscaling (HPA) to handle traffic spikes:

yaml
1apiVersion: autoscaling/v2
2kind: HorizontalPodAutoscaler
3metadata:
4  name: nodejs-hpa
5spec:
6  scaleTargetRef:
7    apiVersion: apps/v1
8    kind: Deployment
9    name: nodejs-app
10  minReplicas: 3
11  maxReplicas: 10
12  metrics:
13  - type: Resource
14    resource:
15      name: cpu
16      target:
17        type: Utilization
18        averageUtilization: 70

Production Tips

After running several Node.js apps in production with Kubernetes, I've learned these valuable lessons:

  1. Set resource limits for your containers to prevent resource hogging
  2. Use node affinity rules to spread your pods across different nodes
  3. Set up proper logging with tools like Fluentd or EFK stack
  4. Monitor your applications with Prometheus and Grafana
  5. Use rolling updates to avoid downtime during deployments

Security Considerations

Security is crucial when running Node.js applications in Kubernetes. I always run containers as non-root users and scan images for vulnerabilities before deployment. Here's a security context configuration I use:

yaml
1securityContext:
2  runAsUser: 1000
3  runAsGroup: 3000
4  fsGroup: 2000

Running Node.js applications in Kubernetes has made my deployments more reliable and easier to manage. The combination provides excellent scalability and maintainability, making it a solid choice for modern web applications. Start small, test thoroughly, and gradually add more complex features as you become comfortable with the setup.