Back to articles
DevOps & Systems

Green DevOps Strategy: Configuring Keda to Automatically Shut Down Node.js Bots on VPS During Low Load

Green DevOps emphasizes energy efficiency and operational cost optimization by dynamically adjusting resources based on real-time demand. When running Node.js bots on VPS,...

DPTCloud Engineering5 min read
Green DevOps Strategy: Configuring Keda to Automatically Shut Down Node.js Bots on VPS During Low Load

Green DevOps emphasizes energy efficiency and operational cost optimization by dynamically adjusting resources based on real-time demand. When running Node.js bots on VPS, configuring Keda to automatically shut down bots during low load reduces power consumption, extends server lifespan, and lowers operational costs. This guide explains how to implement this strategy on NVMe Cloud VPS using Keda as an event-driven autoscaler to manage resources effectively.

Introduction to Green DevOps and Keda

Green DevOps is a modern DevOps approach that prioritizes minimizing environmental impact and operational expenses. One effective strategy is automating the shutdown of unnecessary services during periods of low load. Keda (Kubernetes Event-driven Autoscaler) is an open-source tool for Kubernetes that enables scaling applications based on external events such as message queues, databases, or custom metrics. In non-Kubernetes environments, Keda can be deployed as a scaling controller for applications running on VPS through custom controllers.

For Node.js bots on VPS, using Keda to monitor CPU or memory usage and automatically stop the bot when load falls below a threshold optimizes energy consumption. For example, if CPU usage remains below 20% for 10 consecutive minutes, Keda triggers a shutdown command or stops the container. When load increases again, it restarts the bot.

Enabling Deployment on VPS

To deploy this strategy, you need a VPS that supports Docker and can run control services. NVMe Cloud VPS is ideal due to high read/write speeds and stable performance, making it suitable for fast data-processing applications like Node.js bots. Learn more about the service at NVMe Cloud VPS for Docker, Nginx & Databases.

First, install Docker and Keda on the VPS. Keda can be deployed using scripts or tools like Helm. Below is a basic configuration to have Keda monitor a Node.js bot running in a Docker container:

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: node-bot-scaledobject
spec:
  scaleTargetRef:
    name: node-bot
  triggers:
  - type: cpu
    metadata:
      value: "20"
  - type: memory
    metadata:
      value: "50"

In this configuration, Keda monitors CPU and memory usage. When CPU usage drops below 20% or memory usage falls below 50%, Keda reduces the number of pods to zero, effectively shutting down the bot.

Automating Bot Shutdown

To implement bot shutdown, configure a shell script or Docker command to stop the container when Keda activates. For example, when Keda scales down, it can call a webhook or script on the VPS to execute:

#!/bin/bash
# stop-bot.sh
docker stop node-bot-container
docker rm node-bot-container

This script must be triggered when Keda detects low load. Tools like systemd or cron can manage script execution, or integrate with Keda via webhook.

To verify operation, use the following command to check container status:

docker ps -a

Regularly run this check to ensure the bot shuts down and restarts correctly when load changes.

Alternative Solutions and Considerations

If direct Keda deployment on VPS is not feasible, use tools like cron or custom scripts to monitor system load and shut down bots. However, this approach lacks the flexibility and responsiveness of Keda.

For applications requiring continuous operation, consider platforms like Kubernetes or serverless services such as AWS Lambda or Google Cloud Functions, where Keda can be fully deployed. However, for small businesses or personal projects, VPS combined with Keda offers an efficient and cost-effective solution.

For more information on automation and optimization solutions, see Appsmith and n8n: The Perfect Duo for Building an Optimized Operating System for Small Businesses.

Requirements and Verification

To successfully deploy this strategy, ensure the following conditions:

  • VPS supports Docker and can run control services.
  • Keda is installed and configured correctly on the VPS.
  • stop-bot.sh script is created and executable.
  • Cron or systemd is configured to handle commands when Keda triggers.

Troubleshooting

Common issues and solutions:

Issue Causes Solutions
Keda does not trigger bot shutdown Missing webhook or incorrect script call Check webhook configuration and ensure the script is called correctly.
Bot does not shut down during low load Threshold set too high or inaccurate Adjust load thresholds in Keda configuration.
Bot does not restart when load increases Missing start command or script not triggered Add bot start command to the script or webhook.
DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478