简体   繁体   中英

How to support several nodejs services by upstart and monit?

I have created an upstart script in order to daemonize a node.js app:

description "app_name"

start on startup
stop on shutdown

script
    export HOME="/home/ubuntu/nodeapp"

    exec sudo -u nodejs /usr/local/bin/node $HOME/app/server.js production 2>>/var/log/app_name.error.log >>/var/log/app_name.log
end script

My monit script is the following:

check host app_name with address 127.0.0.1
    start "/sbin/start app_name"
    stop "/sbin/stop app_name"
    if failed port 80 protocol HTTP
        request /ok
        with timeout 5 seconds
        then restart

It works fine, but now I want to add nginx as load balancer with upstream like the following:

upstream cluster1 {
  least_conn;
  server 127.0.0.1:8000;
  server 127.0.0.1:8001;
}

server {
    listen 0.0.0.0:80;

    location / {
    proxy_pass http://cluster1;
    }
}

So how should I change upstart and monit scripts to support that?

You should create three monit checks, one for nginx and one for each nodejs instance:

check host nginx with address 127.0.0.1
    start "/sbin/start nginx"
    stop "/sbin/stop nginx"
    if failed port 80 protocol HTTP
        request /ok
        with timeout 5 seconds
        then restart

check host app_name_on_8000 with address 127.0.0.1
    start "/sbin/start app_name_on_8000"
    stop "/sbin/stop app_name_on_8000"
    if failed port 8000 protocol HTTP
        request /ok
        with timeout 5 seconds
        then restart

check host app_name_on_8001 with address 127.0.0.1
    start "/sbin/start app_name_on_8001"
    stop "/sbin/stop app_name_on_8001"
    if failed port 8000 protocol HTTP
        request /ok
        with timeout 5 seconds
        then restart

That will restart nodejs instances when they fail, and nginx if fails or both nodejs instances are down

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM