To automatically register with the load balancer when my EC2 node starts up, I placed the following script in /etc/init.d/registerlb:
#!/bin/bash
#
# registerlb Startup script for registering and deregistering with the load balancer
#
# chkconfig: - 90 10
# description: Script for registring and deregistering with load balancer
LB_REGISTER_URL=http://wslb-p.webappwishlist.com:8080/pound/subscribe
LB_UNREGISTER_URL=http://wslb-p.webappwishlist.com:8080/pound/unsubscribe
LB_STATUS_URL=http://wslb.webappwishlist.com:8080/pound/list
PRIVATE_IP=`ip -4 -o addr show dev eth0 | awk '{print substr($4, 1, length($4)-3)}'`
PUBLIC_IP=`wget -q http://swamp.homelinux.net/cgi-bin/ipaddress --output-document=-`
USER=mah292
start() {
echo -n "Registering with load balancer (public: $PUBLIC_IP, private: $PRIVATE_IP):"
wget -q ${LB_REGISTER_URL}?user=${USER}&publicip=${PUBLIC_IP}&privateip=${PRIVATE_IP} --output-document=-
RETVAL=$?
echo
}
stop() {
echo -n "Unregistering with load balancer (public: $PUBLIC_IP):"
wget -q ${LB_UNREGISTER_URL}?publicip=${PUBLIC_IP}
RETVAL=$?
echo
[ $RETVAL = 0 ] || exit $RETVAL
}
status() {
echo “Public IP: $PUBLIC_IP”
echo “Private IP: $PRIVATE_IP”
echo
echo “Load balancer list output:”
wget -q $LB_STATUS_URL –output-document=-
}
RETVAL=0
case “$1″ in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo $”Usage: $0 {start|stop|restart|status}”
exit 1
esac
exit $RETVAL
Make sure you change the user name on line 14 to something other than “mah292″ if you use this script!
Make sure the script is executable by doing ‘chmod +x registerlb’. Now you can do ‘/etc/init.d/registerlb status’ to see which nodes are registered with the load balancer, ‘/etc/init.d/registerlb start’ to register with the load balancer, and ‘/etc/init.d/registerlb stop’ to unsubscribe from the load balancer.
Once the script is working, you can have it run automatically when your EC2 node starts and stops by doing:
chkconfig --level 0126 registerlb off chkconfig --level 345 registerlb on
The script uses wget to execute web requests. I could have just as easily used curl.
To get the private ip address, I use the ‘ip’ command. The -4 indicates that I want the IPv4 information. The -o show addr eth0 indicates that I want to show the address information for interface eth0. I then pipe the output into awk so that I can parse the output of ‘ip’ to only output the IP address without the additional text. I then place this in the PRIVATE_IP environment variable.
To get the public ip address, I created a trivial CGI script on my home server. The CGI script looks like:
#!/bin/bash echo "Content-Type: text/plain" echo echo $REMOTE_ADDR
Feel free to use this service for your own scripts.
I invoke this service with wget and place the text returned by the service in PUBLIC_IP.
In the start(), stop(), and status() functions, I use the IP addresses I extracted as described above and wget to send requests to the load balancer host.
If you have any questions about this script, please let me know.
RSS feed for comments on this post · TrackBack URI
Leave a reply