{"id":125,"date":"2015-10-05T11:46:13","date_gmt":"2015-10-05T10:46:13","guid":{"rendered":"http:\/\/www.haxed.me.uk\/?p=125"},"modified":"2015-10-05T12:33:40","modified_gmt":"2015-10-05T11:33:40","slug":"creating-a-distributed-rackspace-load-balancer-website","status":"publish","type":"post","link":"https:\/\/haxed.me.uk\/index.php\/2015\/10\/05\/creating-a-distributed-rackspace-load-balancer-website\/","title":{"rendered":"Creating a Distributed Rackspace Load balancer Website"},"content":{"rendered":"<p>So, today I was taking a look at Rackspace&#8217;s Load Balancers. I wanted to put together a small tutorial how to spin up multiple cloud servers and add them to a normal HTTP load balancer. This is traditionally a use case for sites that good a lot of traffic, and\/or require great redundancy both at the load balancer, and at the server level. i.e. multiple ip addresses and hardware that is failover redundant. If a server or load balancer goes down, then there is provisions to allow a new load balancer or cloud server to take over.<\/p>\n<p>It&#8217;s a simple setup.<\/p>\n<p><strong>Step 1. Creating 2 or more Cloud Servers from the mycloud.rackspace.co.uk Control Panel.<\/strong><\/p>\n<p><a href=\"http:\/\/www.haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.38.09-AM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-126\" src=\"http:\/\/www.haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.38.09-AM.png\" alt=\"Screen Shot 2015-10-05 at 11.38.09 AM\" width=\"719\" height=\"443\" srcset=\"https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.38.09-AM.png 719w, https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.38.09-AM-300x185.png 300w, https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.38.09-AM-487x300.png 487w\" sizes=\"auto, (max-width: 719px) 100vw, 719px\" \/><\/a><\/p>\n<p>Create two servers, using the above process.<\/p>\n<p><strong> Step 2. I am using SSH Keys, so I provide my public ssh id_dsa.pub for SSH, (for a guide on making SSH keys, see my tutorial on this site) <\/strong><\/p>\n<p>Create two servers with SSH KEY AUTHENTICATION (optional), using the above process.<\/p>\n<p><a href=\"http:\/\/www.haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.39.44-AM.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.39.44-AM.png\" alt=\"Screen Shot 2015-10-05 at 11.39.44 AM\" width=\"729\" height=\"130\" class=\"alignnone size-full wp-image-127\" srcset=\"https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.39.44-AM.png 729w, https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.39.44-AM-300x53.png 300w, https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.39.44-AM-500x89.png 500w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/a><\/p>\n<p><strong> Step 3. Install httpd and netcat service, in my case I am using CENTOS 7, which is a nice secure version of RHEL. <\/strong><br \/>\nRedHat\/CentOS Distributions:<\/p>\n<pre>\r\nyum install httpd nc\r\n<\/pre>\n<p>Debian\/Ubuntu Distributions:<\/p>\n<pre> \r\napt-get install httpd netcat\r\n<\/pre>\n<p><strong> Step 4. Creating A Rackspace Load Balancer <\/strong><\/p>\n<p><a href=\"http:\/\/www.haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.43.41-AM.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.43.41-AM.png\" alt=\"Screen Shot 2015-10-05 at 11.43.41 AM\" width=\"752\" height=\"502\" class=\"alignnone size-full wp-image-128\" srcset=\"https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.43.41-AM.png 752w, https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.43.41-AM-300x200.png 300w, https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.43.41-AM-449x300.png 449w\" sizes=\"auto, (max-width: 752px) 100vw, 752px\" \/><\/a><\/p>\n<p><strong> Step 5. Add 2 or more server nodes to the Load Balancer <\/strong><\/p>\n<p><a href=\"http:\/\/www.haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.44.09-AM.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.44.09-AM.png\" alt=\"Screen Shot 2015-10-05 at 11.44.09 AM\" width=\"767\" height=\"461\" class=\"alignnone size-full wp-image-129\" srcset=\"https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.44.09-AM.png 767w, https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.44.09-AM-300x180.png 300w, https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.44.09-AM-500x300.png 500w\" sizes=\"auto, (max-width: 767px) 100vw, 767px\" \/><\/a><\/p>\n<p>Be sure to tick the number of servers you want to add. Please note, it&#8217;s possible to add servers to the Load Balancer that aren&#8217;t part of the rackspace Network. To do that you can use the &#8216;add external node&#8217; button. Please note though, that the requests between load balancer and the destination machine goes over the public network interface. Whereas requests from the load balancer to other rackspace servers will always go thru the service net by default. (that is the local 10.x.x.x IP addresses and networks).<\/p>\n<p><strong> Step 6. Configure Cloud Server Firewall Settings to Accept port 80 and (optionally) port 443 <\/strong><\/p>\n<pre>\r\n# Allow negotiating of connections on port 80 incoming (HTTP), and port 443 incoming (HTTPS)\r\nsudo iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT\r\nsudo iptables -I INPUT 1 -p tcp --dport 443 -j ACCEPT\r\n# Allow negotiated connections replies to reach us\r\nsudo iptables -A INPUT -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT\r\n<\/pre>\n<p>Step 7. Restart apache2 \/ httpd service, and check localhost on port 80<\/p>\n<pre>\r\nservice httpd restart\r\ncurl localhost\r\n<\/pre>\n<p>Step 8. Configure the Load Balancer Algorithm (optional). I wanted to use a round-robin approach, each request increments the serverid. So with 3 servers. It takes 3 incremental requests for serverid 1 to serve http request. With 3 servers, i t takes 4 incremental requests for serverid 2 to serve a request twice!<\/p>\n<p><a href=\"http:\/\/www.haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.49.41-AM.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.49.41-AM.png\" alt=\"Screen Shot 2015-10-05 at 11.49.41 AM\" width=\"520\" height=\"466\" class=\"alignnone size-full wp-image-132\" srcset=\"https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.49.41-AM.png 520w, https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.49.41-AM-300x269.png 300w, https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-11.49.41-AM-335x300.png 335w\" sizes=\"auto, (max-width: 520px) 100vw, 520px\" \/><\/a><\/p>\n<p>There are various different settings to use.<\/p>\n<p>Step 9. All Done! You&#8217;ve configured the load balancer, and attached 2 cloud servers to the load balancer. Requests can come into the load balancer on port 80, and it sends requests to either server1 or server2 on the same port, 80. It is however possible to send requests to the cloud servers on a different port, which is covered in another article on this website. You could for instance create a server using 10 SSL certificates on a single IP, using different ports. Which could work out a lot cheaper than leasing 10 IPV4 or wrestling with your service provider for additional IP&#8217;s for SSL usage <\/p>\n<p><strong> Step 10. Test requests to the load balancer <\/strong><\/p>\n<p>In this setup I have 2 cloud server IP&#8217;s: 5.79.24.207 and 5.79.24.205. They both listen on port 80.<\/p>\n<p>In this setup I have 1 load balancer IP address: 134.213.160.178<\/p>\n<p>I can now connect to the load balancer running on http:\/\/134.213.160.178 and it forwards the connections to 5.79.24.207 or 5.79.24.205. If I wanted I could have added another 100 cloud servers to the load balancer, and each time I load a HTTP request to the load balancer, a different cloud server will respond. This allows very large numbers of transactions to go to a website, and for multiple servers to respond individually to seperate customers requesting thru the load balancer. In a production environment after confirming these changes I would then.<\/p>\n<p><strong> Step 11: SET DNS to point to the load balancer IP instead of cloud server IP. <\/strong><\/p>\n<pre>\r\nA RECORD ADDED AS BELOW:\r\nmywebsite.com -> 134.213.160.178 \r\n<\/pre>\n<p><strong> Step 12: Confirm Load Balancer is working <\/strong><\/p>\n<p>For my purposes I am using the default www website for CENTOS 7 HTTPD \/var\/www\/html\/index.html . I changed server1 index.html to say only &#8216;server1&#8217; and I changed server2 index.html to say only &#8216;server2&#8217;. This way I can check the load balancer is giving a different server every second request. It was:<\/p>\n<p><a href=\"http:\/\/www.haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-12.01.12-PM.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-12.01.12-PM.png\" alt=\"Screen Shot 2015-10-05 at 12.01.12 PM\" width=\"394\" height=\"107\" class=\"alignnone size-full wp-image-137\" srcset=\"https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-12.01.12-PM.png 394w, https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-12.01.12-PM-300x81.png 300w\" sizes=\"auto, (max-width: 394px) 100vw, 394px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/www.haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-12.01.21-PM.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-12.01.21-PM.png\" alt=\"Screen Shot 2015-10-05 at 12.01.21 PM\" width=\"399\" height=\"96\" class=\"alignnone size-full wp-image-138\" srcset=\"https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-12.01.21-PM.png 399w, https:\/\/haxed.me.uk\/wp-content\/uploads\/2015\/10\/Screen-Shot-2015-10-05-at-12.01.21-PM-300x72.png 300w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><\/a><\/p>\n<p><strong> Step 13: Testing for Client IP information with Netcat <\/strong><\/p>\n<pre>\r\ntailf \/var\/log\/httpd\/access.log\r\n10.190.255.250 - - [05\/Oct\/2015:10:14:37 +0000] \"GET \/ HTTP\/1.1\" 200 8 \"-\" \"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko\/20100101 Firefox\/39.0\"\r\n0.190.255.250 - - [05\/Oct\/2015:10:14:39 +0000] \"GET \/ HTTP\/1.1\" 200 8 \"-\" \"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko\/20100101 Firefox\/39.0\"\r\n10.190.255.250 - - [05\/Oct\/2015:10:14:40 +0000] \"GET \/ HTTP\/1.1\" 200 8 \"-\" \"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko\/20100101 Firefox\/39.0\"\r\n10.190.255.250 - - [05\/Oct\/2015:10:14:42 +0000] \"GET \/ HTTP\/1.1\" 200 8 \"-\" \"Mozilla\/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; MS-RTC LM 8; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\"\r\n<\/pre>\n<p>Presently we can see that requests reaching our servers from cloud load balancer are seeing only the load balancer IP. We need each apache httpd server to know the client IP of each request.<\/p>\n<pre>\r\nservice httpd stop\r\n\r\nSo, I run netcat on port 80 to see the requests from the load balancer.\r\n\r\n[root@server1 html]# nc -l 80\r\nGET \/ HTTP\/1.1\r\nUser-Agent: Mozilla\/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko\/20100101 Firefox\/39.0\r\nX-Forwarded-For: 94.236.7.190\r\nAccept: text\/html,application\/xhtml+xml,application\/xml;q=0.9,*\/*;q=0.8\r\nCache-Control: max-age=0\r\nX-Forwarded-Proto: http\r\nAccept-Language: en-US,en;q=0.5\r\nHost: 134.213.160.178\r\nIf-Modified-Since: Mon, 05 Oct 2015 10:11:57 GMT\r\nX-Cluster-Client-Ip: 94.236.7.190\r\nVia: 1.1 542204-LON4WWSG01.secops.rackspace.com 0A02CC2D\r\nX-Forwarded-Port: 80\r\nIf-None-Match: \"8-52158be551fbf\"\r\nAccept-Encoding: gzip, deflate\r\n<\/pre>\n<p>As we can see there is an X-Forwarded-For header from the load balancer which does reach the cloud server, but by default apache doesn&#8217;t know about it and doesn&#8217;t put the X-Forwarded-For variable in the Logs, only the src_ip, which is presently the load balancers IP and not our client. So we need to make a small modification to the apache2 default httpd configuration:<\/p>\n<p><strong> Step 14: Modify Apache HTTPD Log &#8216;combined&#8217; configuration <\/strong><\/p>\n<p>cat \/etc\/httpd\/conf\/httpd.conf <\/p>\n<p>Inside the above file we see the directives:<\/p>\n<pre>  \r\n\r\n    # The following directives define some format nicknames for use with\r\n    # a CustomLog directive (see below).\r\n    #\r\n    LogFormat \"%h %l %u %t \\\"%r\\\" %>s %b \\\"%{Referer}i\\\" \\\"%{User-Agent}i\\\"\" combined\r\n\r\n    #\r\n    # If you prefer a logfile with access, agent, and referer information\r\n    # (Combined Logfile Format) you can use the following directive.\r\n    #\r\n    CustomLog \"logs\/access_log\" combined\r\n<\/IfModule>\r\n<\/pre>\n<p>The part we want to change is very small<\/p>\n<pre>\r\nLogFormat \"<strong>%{X-Forwarded-For}i<\/strong> %h %l %u %t \\\"%r\\\" %>s %b \\\"%{Referer}i\\\" \\\"%{User-Agent}i\\\"\" combined\r\n<\/pre>\n<p>We want to add the section above <strong> %{X-Forwarded-For}i<\/strong>. This will include the real Client_IP before the Load Balancer IP in the server logs, so it looks like so:<\/p>\n<pre>\r\n94.236.7.190 10.190.255.250 - - [05\/Oct\/2015:11:01:11 +0000] \"GET \/ HTTP\/1.1\" 200 8 \"-\" \"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko\/20100101 Firefox\/39.0\"\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So, today I was taking a look at Rackspace&#8217;s Load Balancers. I wanted to put together a small tutorial how to spin up multiple cloud servers and add them to a normal HTTP load balancer. This is traditionally a use &hellip; <a href=\"https:\/\/haxed.me.uk\/index.php\/2015\/10\/05\/creating-a-distributed-rackspace-load-balancer-website\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":0,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"wp:attachment":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}