Skip to content

Instantly share code, notes, and snippets.

@acsrujan
Last active June 2, 2016 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acsrujan/fbd7588a7a147df96b73e002d86b7c30 to your computer and use it in GitHub Desktop.
Save acsrujan/fbd7588a7a147df96b73e002d86b7c30 to your computer and use it in GitHub Desktop.
nginx load balancing nodejs
#!/bin/bash
start_new_node() {
BASE=8080
INCREMENT=1
port=$BASE
isfree=$(netstat -tapln | grep $port)
while [[ -n "$isfree"]]; do
port = $[port+INCREMENT]
isfree=$(netstat -tapln | grep $port)
done
PORT=$isfree forever start /var/www/hello-world-node/main.js
echo "server 127.0.0.1:$isfree max_fails=0 fail_timeout=10s weight=1" >> /etc/nginx/upstream.conf
sed -i -e '/##START_SERVERS/,/##END_SERVERS/{//!d;/##START_SERVERS/r /etc/nginx/upstream.conf' -e '}' /etc/nginx/nginx.conf
service nginx reload
}
stop_new_node() {
lastserver=sed -n '$p' /etc/nginx/upstream.conf
#lastserver=grep -B1 '##END_SERVERS' /etc/nginx/upstream.conf | grep -v '##END_SERVERS'
lastport=${lastserver##*:} | head -n1 | awk '{print $1;}'
#Condition to be handled: Ensure that all servers aren't killed.
if [ "$lastport" -ne 8080]
then
PORT=$lastport forever stop /var/www/hello-world-node/main.js
fi
sed "/$PORT/d" /etc/nginx/upstream.conf
sed "/$PORT/d" /etc/nginx/conf.d/nodejs.conf
service nginx reload
}
output1=`wget -O -q -t 3 -T 3 http://test.com/basic_status`
sleep 60
output2=`wget -O -q -t 3 T 3 http://test.com/basic_status`
tmp1_reqpmin=`echo ${out1}|awk '{print $10}'`
tmp2_reqpmin=`echo ${out2}|awk '{print $10}'`
reqpmin=`expr $tmp2_reqmin - $tmp1_reqmin`
if [ "$reqpmin" gt 100]
then
start_new_node();
fi
if [ "$reqpmin" lt 100]
then
stop_new_node();
fi

Download Repo in /var/www/ folder.

#!/bin/bash
sudo apt-get update
sudo apt-get install -y openssh-server libssl-dev git g++ make python-software-properties
sudo apt-get install nginx nginx-extras
upstream node_cluster {
#START_SERVERS
server 127.0.0.1:8080 max_fails=0 fail_timeout=10s weight=1;
#END_SERVERS
ip_hash;
keep_alive 512;
}
server {
listen 80;
server_name test.com www.test.com;
location / {
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://node_cluster/;
proxy_redirect off;
}
location /basic_status {
stub_status on;
}
}
#!/bin/bash
# This script installs node in ' $HOME/local/node ' directory without sudo
# Store script's filename in $SELF_NAME
SELF_NAME=$(basename $0)
# Prints warning/error $MESSAGE in red foreground color (for [E]rror messages)
red_echo() {
echo -e "\e[1;31m[E] $SELF_NAME: $MESSAGE\e[0m"
}
# Prints success $MESSAGE in green foreground color (for [S]uccess messages)
green_echo() {
echo -e "\e[1;32m[S] $SELF_NAME: $MESSAGE\e[0m"
}
# Prints $MESSAGE in blue foreground color (for [I]nfo messages)
blue_echo() {
echo -e "\e[1;34m[I] $SELF_NAME: $MESSAGE\e[0m"
}
# Function to ensure success of previous command.
# This causes the script to exit if previous command exited with non-zero status
function ensure_success {
return_status="$1"
error_message="$2"
success_message="$3"
if [ "$return_status" -ne 0 ];then
MESSAGE=$error_message ; red_echo
exit
else
MESSAGE=$success_message ; green_echo
fi
}
# Creating local directories
MESSAGE="Creating directory $HOME/local/node\n"; blue_echo
mkdir -p $HOME/local/node
MESSAGE="Directory created\n";blue_echo
# creating an cd-ing to Downolads directory
MESSAGE="Creating a Node directory and entering in to it \n"; blue_echo
mkdir -p $HOME/Node/
cd $HOME/Node
MESSAGE="Now in Node directory\n";blue_echo
#Downloading the linux tar.gz node package from http://nodejs.org/download/
MESSAGE="Downloading the node package for linux\n"; blue_echo
wget https://nodejs.org/dist/v5.0.0/node-v5.0.0-linux-x64.tar.gz
ensure_success "$?" "Downloading error " " Successfully downloaded "
NODE_PACKAGE=`ls *.tar.gz`
# Extracting the node package
MESSAGE="Extracting the node package \n";blue_echo
tar xzf $NODE_PACKAGE -C $HOME/local/node --strip-components=1
MESSAGE="nodejs Enviroment Setup\n"; blue_echo
echo '#nodejs Enviroment Setup' >> $HOME/.bashrc
MESSAGE="exporting PATH ==> PATH=$HOME/local/node/bin:$PATH\n";blue_echo
echo 'export PATH=$HOME/local/node/bin:$PATH' >> $HOME/.bashrc
MESSAGE="exporting NODE_PATH ==> NODE_PATH=$HOME/local/node/lib/node_modules\n";blue_echo
echo 'export NODE_PATH=$HOME/local/node/lib/node_modules' >> $HOME/.bashrc
MESSAGE="Removing Node directory packages\n"; blue_echo
rm -rf $HOME/Node
ensure_success "$?" "Error removing Downloads directory" "Removed Downloads directory"
MESSAGE="Loading node and npm \n";green_echo
echo 'source $HOME/.bashrc'
MESSAGE="After sourcing bashrc : You can verify the versions as below \n"; blue_echo
echo 'node -v '
echo 'npm -v '
echo 'npm install -g forever'
description "Make sure reqmin is running"
start on local-filesystem
stop on shutdown
respawn
exec /etc/check_rpm start
#!/bin/bash
sudo nginx_install.sh
sudo nodejs_install.sh
sudo touch /etc/nginx/upstream.conf
mkdir -p /var/www/
cd /var/www/
git clone git@github.com:chetandhembre/hello-world-node.git
cd /var/www/hello-world-node/
forever start main.js
PORT=8081 forever start main.js
chmod +x check_rpm.sh
cp check_rpm.sh /etc/check_rpm
sudo service reqpmin start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment