{"id":328,"date":"2012-06-18T18:41:46","date_gmt":"2012-06-18T10:41:46","guid":{"rendered":"http:\/\/rmohan.com\/?p=328"},"modified":"2012-09-16T00:16:55","modified_gmt":"2012-09-15T16:16:55","slug":"load-balancer-haproxy-stunnel","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=328","title":{"rendered":"Load balancer HAPROXY STUNNEL"},"content":{"rendered":"<p><strong>Load balancer HAPROXY STUNNEL<\/strong><\/p>\n<p>HAProxy Software Load Balancer<\/p>\n<p>HAProxy is a bit more bare metal as it targets a very specific set of scenarios focused on TCPIP more than HTTP. You can use cookie based injection with HAProxy to do round robin and stick users to a specific server. However, you can not do this if your site is running SSL traffic. HAProxy can not decrypt the SSL traffic. This is more of the authors dead-fast belief that SSL should not be terminated because of CPU load on the load balancer preventing scaling as you would need to scale the load balancers at some point (we\u2019re talking millions of requests, facebook style).<\/p>\n<p>on all nodes please copy the files on all server \/ nodes<\/p>\n<p>lb1<\/p>\n<p>vi \/etc\/hosts<\/p>\n<p>127.0.0.1 localhost.localdomain localhost<br \/> ::1 localhost6.localdomain6 localhost6<\/p>\n<p>##### Load balancers<\/p>\n<p>192.168.60.11 lb1.rmohan.com lb1<br \/> 192.168.60.12 lb2.rmohan.com lb2<\/p>\n<p>### web servers<\/p>\n<p>192.168.60.14 web1.rmohan.com web1<br \/> 192.168.60.15 web2.rmohan.com web2<\/p>\n<p>### database servers<\/p>\n<p>192.168.60.17 db1.rmohan.com db1<br \/> 192.168.60.18 db2.rmohan.com db2<\/p>\n<p>##############VIPS<\/p>\n<p>192.168.60.10 load.rmohan.com load<br \/> 192.168.60.6 db.rmohan.com db<\/p>\n<p>Now generate ssh keys<\/p>\n<p>ssh-keygen -t rsa<\/p>\n<p>ssh-keygen -t dsa<\/p>\n<p>cd \/root\/.ssh<\/p>\n<p>cat *.pub &gt; authorized_keys<\/p>\n<p>ls<\/p>\n<p>authorized_keys id_das id_dsa.pub id_rsa id_rsa.pub known_hosts<\/p>\n<p>scp -r .ssh\/ lb2:root\/<\/p>\n<p>scp -r .ssh\/ web1:root\/<\/p>\n<p>scp -r .ssh\/ web2:root\/<\/p>\n<p>scp -r .ssh\/ db1:root\/<\/p>\n<p>scp -r .ssh\/ db2:root\/<\/p>\n<p>ssh-keyscan -t rsa lb1 lb2 www1 www2 db1 db2<\/p>\n<p>ssh-keyscan -t dsa lb1 lb2 www1 www2 db1 db2<\/p>\n<p>scp -r \/etc\/hosts lb2:\/etc\/<br \/> scp -r \/etc\/hosts web1:\/etc\/<br \/> scp -r \/etc\/hosts web2:\/etc\/<\/p>\n<p>Stop unwanted services<\/p>\n<p>NTP SETUP ON THE SERVER<\/p>\n<p>LB1<\/p>\n<p>ntp services<\/p>\n<p>rpm -qa | grep ntp<\/p>\n<p>vi \/etc\/ntp.conf<\/p>\n<p># restrict default kod nomodify notrap nopeer noquery<br \/> #restrict -6 default kod nomodify notrap nopeer noquery<\/p>\n<p>restrict 127.0.0.1<br \/> #restrict -6 ::1<\/p>\n<p># server 0.centos.pool.ntp.org<br \/> # server 1.centos.pool.ntp.org<br \/> # server 2.centos.pool.ntp.org<\/p>\n<p>server 127.127.1.0 # local clock<\/p>\n<p>#fudge 127.127.1.0 stratum 10<\/p>\n<p>\/etc\/init.d\/ntpd start<\/p>\n<p>chkconfig ntpd on<\/p>\n<p>watch ntpq -p -n<\/p>\n<p>ntpdate -u 192.168.1.10<\/p>\n<p>Note : It NEED SOME TIME TO SYNC<\/p>\n<p>LB2<\/p>\n<p>ntp services<\/p>\n<p>rpm -qa | grep ntp<\/p>\n<p>vi \/etc\/ntp.conf<\/p>\n<p># restrict default kod nomodify notrap nopeer noquery<br \/> #restrict -6 default kod nomodify notrap nopeer noquery<\/p>\n<p>#restrict 127.0.0.1<br \/> #restrict -6 ::1<\/p>\n<p>server 192.168.1.10<\/p>\n<p># server 0.centos.pool.ntp.org<br \/> # server 1.centos.pool.ntp.org<br \/> # server 2.centos.pool.ntp.org<\/p>\n<p>#server 127.127.1.0 # local clock<\/p>\n<p>#fudge 127.127.1.0 stratum 10<\/p>\n<p>\/etc\/init.d\/ntpd start<\/p>\n<p>chkconfig ntpd on<\/p>\n<p>watch ntpq -p -n<\/p>\n<p>ntpdate -u 192.168.1.10<\/p>\n<p>Note : It NEED SOME TIME TO SYNC<\/p>\n<p>yum install mod_ssl<\/p>\n<p>yum install openssl<\/p>\n<p>yum install stunnel<\/p>\n<p>wget http:\/\/download.fedora.redhat.com\/pub\/epel\/6\/i386\/epel-release-6-5.noarch.rpm<\/p>\n<p>yum install haproxy<\/p>\n<p>wget http:\/\/haproxy.1wt.eu\/download\/1.4\/src\/haproxy-1.4.19.tar.gz<\/p>\n<p>tar -zxvf haproxy-1.4.19.tar.gz<\/p>\n<p>yum install gcc<\/p>\n<p>make TARGET=linux26 ARCH=i386<\/p>\n<p>make TARGET=linux26 CPU=i686<\/p>\n<p>make install<\/p>\n<p>mkdir \/etc\/haproxy<\/p>\n<p>wget http:\/\/layer1.rack911.com\/haproxy\/haproxy-standard.cfg<\/p>\n<p>wget http:\/\/layer1.rack911.com\/haproxy\/haproxy.init<\/p>\n<p>cp haproxy-standard.cfg \/etc\/haproxy.cfg<\/p>\n<p>cp haproxy.init \/etc\/init.d\/haproxy<\/p>\n<p>chmod +x \/etc\/init.d\/haproxy<\/p>\n<p>cp haproxy.init \/etc\/init.d\/haproxy<\/p>\n<p>\/usr\/local\/sbin\/haproxy location<\/p>\n<p>cp \/usr\/local\/sbin\/haproxy \/usr\/sbin\/haproxy<\/p>\n<p>chkconfig &#8211;add haproxy<\/p>\n<p>chkconfig haproxy on<\/p>\n<p>useradd haproxy<\/p>\n<p>chown haproxy:haproxy \/etc\/haproxy.cfg<\/p>\n<p>mkdir haproxy<br \/> touch stats<\/p>\n<p>chown -R haproxy:haproxy \/var\/lib\/haproxy<\/p>\n<p># Global settings<br \/> global<br \/> log 127.0.0.1 local2<\/p>\n<p>chroot \/var\/lib\/haproxy<br \/> pidfile \/var\/run\/haproxy.pid<br \/> maxconn 4000<br \/> user haproxy<br \/> group haproxy<br \/> daemon<br \/> # turn on stats unix socket<br \/> stats socket \/var\/lib\/haproxy\/stats<\/p>\n<p>defaults<br \/> mode http<br \/> log global<br \/> option httplog<br \/> option dontlognull<br \/> option http-server-close<br \/> option forwardfor except 127.0.0.0\/8<br \/> option redispatch<br \/> retries 3<br \/> timeout http-request 10s<br \/> timeout queue 1m<br \/> timeout connect 10s<br \/> timeout client 1m<br \/> timeout server 1m<br \/> timeout http-keep-alive 10s<br \/> timeout check 10s<br \/> maxconn 2000<br \/> contimeout 5000<br \/> clitimeout 50000<br \/> srvtimeout 50000<\/p>\n<p># round robin balancing between the various backends<br \/> listen HTTP-80 192.168.60.11:80<br \/> mode http<br \/> stats enable<br \/> balance roundrobin<br \/> cookie SERVERID insert nocache indirect<br \/> cookie JSESSIONID prefix<br \/> option httpclose<br \/> option forwardfor<br \/> option dontlognull<br \/> option httpchk HEAD \/check.txt HTTP\/1.0<br \/> server web1 192.168.60.14:80 weight 1 maxconn 512 check<br \/> server web2 192.168.60.15:80 weight 1 maxconn 512 check<br \/> option persist<br \/> option redispatch<\/p>\n<p>echo 1 &gt; \/proc\/sys\/net\/ipv4\/conf\/all\/forwarding<br \/> echo 1 &gt; \/proc\/sys\/net\/ipv4\/conf\/all\/send_redirects<br \/> echo 1 &gt; \/proc\/sys\/net\/ipv4\/conf\/eth0\/send_redirects<\/p>\n<p>change the log format<\/p>\n<p>\/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>#LogFormat &#8220;%h %l %u %t \\&#8221;%r\\&#8221; %&gt;s %b \\&#8221;%{Referer}i\\&#8221; \\&#8221;%{User-Agent}i\\&#8221;&#8221; combined<\/p>\n<p>LogFormat &#8220;%{X-Forwarded-For}i %h %l %u %t \\&#8221;%r\\&#8221; %&gt;s %b \\&#8221;%{Referer}i\\&#8221; \\&#8221;%{User-Agent}i\\&#8221;&#8221; combined<\/p>\n<p>CustomLog logs\/access_log combined env=!dontlog<br \/> SetEnvIf Request_URI &#8220;^\/check\\.txt$&#8221; dontlog<\/p>\n<p>LogFormat &#8220;%h %l %u %t \\&#8221;%r\\&#8221; %&gt;s %b&#8221; common<br \/> LogFormat &#8220;%{Referer}i -&gt; %U&#8221; referer<br \/> LogFormat &#8220;%{User-agent}i&#8221; agent<\/p>\n<p> ServerAdmin webmaster@dummy-host.example.com<br \/> DocumentRoot \/var\/www\/html<br \/> SetEnvIf Request_URI &#8220;^\/check\\.txt$&#8221; dontlog<br \/> CustomLog logs\/access_log combined env=!dontlog<br \/> # ServerName dummy-host.example.com<br \/> # ErrorLog logs\/dummy-host.example.com-error_log<br \/> # CustomLog logs\/dummy-host.example.com-access_log common<\/p>\n<p>vim \/etc\/sysctl.conf<\/p>\n<p>net.ipv4.ip_nonlocal_bind = 1<\/p>\n<p>sysctl -p<\/p>\n<p><strong>Monitor url<\/strong><\/p>\n<p>http:\/\/192.168.60.11\/haproxy?stats<\/p>\n<p>wget ftp:\/\/ftp.nluug.nl\/pub\/networking\/stunnel\/stunnel-4.50.tar.gz<\/p>\n<p>tar -zxvf stunnel-4.50.tar.gz<\/p>\n<p>\/usr\/local\/etc\/stunnel<\/p>\n<p>cd stunnel-4.50<\/p>\n<p>.\/configure<br \/> make<br \/> install<br \/> make install<\/p>\n<p>\/usr\/local\/etc\/stunnel<\/p>\n<p>wget http:\/\/download.fedora.redhat.com\/pub\/epel\/6\/i386\/epel-release-6-5.noarch.rpm<\/p>\n<p>rpm -ivh epel-release-6-5.noarch.rpm<\/p>\n<p>yum install stunnel<\/p>\n<p>cp \/etc\/pki\/tls\/private\/localhost.key ca.key<br \/> cp \/etc\/pki\/tls\/certs\/localhost.crt stunnel.pem<\/p>\n<p>OR<\/p>\n<p>cd \/etc\/pki\/tls\/certs<\/p>\n<p>openssl req -x509 -nodes -newkey rsa:1024 -keyout \/etc\/pki\/tls\/certs\/pound.pem -out \/etc\/pki\/tls\/certs\/pound.pem<\/p>\n<p>chmod 600 \/etc\/pki\/tls\/certs\/pound.pem<\/p>\n<p>openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr<br \/> cp server.key server.key.org<\/p>\n<p>openssl rsa -in server.key.org -out server.key<\/p>\n<p>openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt<\/p>\n<p>[root@lb1 stunnel]# cat stunnel.conf<br \/> cert=\/etc\/stunnel\/stunnel.pem<br \/> key=\/etc\/stunnel\/ca.key<br \/> setuid=root<br \/> setgid=root<br \/> pid = \/var\/run\/stunnel.pid<br \/> output = \/var\/log\/stunnel.log<\/p>\n<p>socket=l:TCP_NODELAY=1<br \/> socket=r:TCP_NODELAY=1<\/p>\n<p># HTTPS<br \/> [https]<br \/> accept=192.168.60.11:443<br \/> connect=192.168.60.11:80<br \/> TIMEOUTclose = 0<\/p>\n<p>#cert=\/etc\/stunnel\/stunnel.pem<br \/> #key=\/etc\/stunnel\/ca.key<br \/> setuid=root<br \/> setgid=root<br \/> pid = \/var\/run\/stunnel.pid<br \/> output = \/var\/log\/stunnel.log<\/p>\n<p>socket=l:TCP_NODELAY=1<br \/> socket=r:TCP_NODELAY=1<\/p>\n<p># HTTPS<br \/> [https]<br \/> cert=\/etc\/stunnel\/server.crt<br \/> key=\/etc\/stunnel\/server.key<br \/> accept=192.168.60.11:443<br \/> connect=192.168.60.11:80<\/p>\n<p>TIMEOUTclose = 0<br \/> ~<\/p>\n<p>#!\/bin\/bash<\/p>\n<p># VARIAVEIS<br \/> GREP=&#8221;\/bin\/grep&#8221;<br \/> EGREP=&#8221;\/bin\/egrep&#8221;<br \/> PROG=&#8221;stunnel&#8221;<br \/> KILLALL=&#8221;\/usr\/bin\/killall&#8221;<\/p>\n<p># TESTANDO SE EXISTE O ARQUIVO<br \/> test -x \/usr\/bin\/stunnel || exit 0<br \/> RETVAL=0<\/p>\n<p>########## START ##########<br \/> start() {<br \/> if [ ! -f \/var\/lock\/subsys\/stunnel ]; then<br \/> \/usr\/bin\/stunnel<br \/> RETVAL=$?<br \/> if [ $RETVAL = 0 ]; then<br \/> touch \/var\/lock\/subsys\/stunnel<br \/> echo $&#8221;Starting $PROG: OK&#8221;<br \/> else<br \/> exit 1<br \/> fi<br \/> fi<br \/> return $RETVAL<br \/> }<br \/> stop() {<br \/> if [ -e \/var\/lock\/subsys\/stunnel ]; then<br \/> $KILLALL \/usr\/bin\/stunnel<br \/> RETVAL=$?<br \/> if [ $RETVAL = 0 ]; then<br \/> rm -rf \/var\/lock\/subsys\/stunnel<br \/> echo $&#8221;Stop $PROG: OK&#8221;<br \/> else<br \/> exit 1<br \/> fi<br \/> fi<br \/> return $RETVAL<br \/> }<br \/> restart(){<br \/> if [ -e \/var\/lock\/subsys\/stunnel ]; then<br \/> $KILLALL \/usr\/bin\/stunnel<br \/> RETVAL=$?<br \/> if [ $RETVAL = 0 ]; then<br \/> rm -rf \/var\/lock\/subsys\/stunnel<br \/> echo $&#8221;Stop $PROG: OK&#8221;<br \/> else<br \/> exit 1<br \/> fi<br \/> fi<\/p>\n<p>if [ ! -f \/var\/lock\/subsys\/stunnel ]; then<br \/> \/usr\/bin\/stunnel<br \/> RETVAL=$?<br \/> if [ $RETVAL = 0 ]; then<br \/> touch \/var\/lock\/subsys\/stunnel<br \/> echo $&#8221;Starting $PROG: OK&#8221;<br \/> else<br \/> exit 1<br \/> fi<br \/> fi<\/p>\n<p>return $RETVAL<br \/> }<br \/> case &#8220;$1&#8221; in<br \/> start)<br \/> start<br \/> ;;<br \/> stop)<br \/> stop<br \/> ;;<br \/> restart)<br \/> restart<br \/> ;;<br \/> *)<br \/> echo $&#8221;ESCOLHA UM ITEM AO LADO: $0 {start|stop|restart}&#8221;<br \/> exit 2<br \/> esac<\/p>\n<p>exit $?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Load balancer HAPROXY STUNNEL<\/p>\n<p>HAProxy Software Load Balancer<\/p>\n<p>HAProxy is a bit more bare metal as it targets a very specific set of scenarios focused on TCPIP more than HTTP. You can use cookie based injection with HAProxy to do round robin and stick users to a specific server. However, you can not do this [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/328"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=328"}],"version-history":[{"count":7,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/328\/revisions"}],"predecessor-version":[{"id":1447,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/328\/revisions\/1447"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}