Creating Servers For Load Balancing

In this step-by-step guide we create the underlying server structure required for successful load balancing.

We will create master server, which to the outside world will be just another server behind the load balancer. However, the master server node will have additional responsibilities including:

  • Serving as the main point for applications where uploads, installations etc are carried out.
  • Carrying out synchronisations of data across all other slave/clone server nodes.

To begin, create a new node. Make sure to create the node within the same region as all other Rackspace services you use.

  • Create the instance (here we use the smallest RAM size available 1gb).

Use SSH to login to the instance via terminal:

# ssh root@the.public.ip.for.the.node

Change root password:

# passwd

Create a new user for future regular usage:

# adduser mynewuser

Add the new user to the sudo group:

# usermod –a –G sudo mynewuser

Update the sudo group configuration to allow members to run as root:

# visudo

Add the following text at the bottom of the file opened:

%sudo  ALL=(ALL)       ALL

Save by using CTRL+X, Y and Enter.

Logout as root user and log back in under the mynewuser account:

# ssh mynewuser@the.public.ip.for.the.node

# sudo su

Update the package manger apt-get, the upgrade pre-installed packages:

# apt-get update
# apt-get upgrade

Set up a basic IPTABLES firewall for the purposes of running an apache web server. Enter the following rules at command line, substitute where required:

# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# iptables -A INPUT -p tcp --dport ssh -j ACCEPT
# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# iptables -A INPUT -j DROP
# iptables -I INPUT 1 -i lo -j ACCEPT

These will allow existing connections to continue, localhost connections, allow connections for SSH purposes and also allow incoming connections of the default HTTP port (80) and secure HTTP port (443). Everything else is blocked at firewall level.

Save the IPTABLES and create startup service to restore the rules on restart:

# iptables-save > /etc/iptables.rules
# nano /etc/network/if-pre-up.d/iptaload

Enter the following text in this new file:

#!/bin/sh 
iptables-restore < /etc/iptables.rules 
exit 0

Save and exit using CTRL+X, Y and Enter.

Create a service to run when the network is shut down so rules are saved:

# nano /etc/network/if-post-down.d/iptasave

Enter the following text in this new file:

#!/bin/sh
iptables-save -c > /etc/iptables.rules
if [ -f /etc/iptables.downrules ]; then    
iptables-restore < /etc/iptables.downrules
fi
exit 0

Make both of these previous new files executable:

# chmod +x /etc/network/if-post-down.d/iptasave
# chmod +x /etc/network/if-pre-up.d/iptaload 

Install NTP for date synchronization:

# apt-get install ntp

Prevent root login for SSH by editing the /etc/ssh/sshd_config file and amending the ‘PermitRootLogin’ value to ‘no’. You may skip this part if you require root login, but remember to change later. If you have amended the SSH config restart ssh using:

# sudo service ssh restart 

The previous steps were just a basic set up for a newly created cloud node. Please look further into security and how to secure against external threats.

At this point application specific to the server should be installed:

#  apt-get install apache2 libapache2-mod-php5 php5 php5-mysql mysql-client gcc –y

# apt-get install php5-imagick php5-mcrypt php5-gd php5-memcache php5-curl

The previous commands install the Apache Web Server, PHP 5, MySQL Extensions, Memchache Extensions, etc. You can add other extensions or options depending upon your own needs.

Set up the apache configuration as per the existing server setup, or copy it across from the old server by (whilst logged into your old server) using:

# scp -rp /etc/apache2/sites-available root@PUBLIC.IP.OF.MASTER-SERVER:/etc/apache2/

Copy across the old www directories that are relevant to the server configuration:

# scp -rp /path/to/www/dir/ root@PUBLIC.IP.OF.MASTER-SERVER: /path/to/www/dir/

At this point create an image backup of the master server. It will later be used as a basis for the slave/clone server setup. Name it ‘BaseImg’.

You can now install PhpMyAdmin on the master server if you wish to be able to maintain the cloud database via a web interface:

# apt-get install phpmyadmin

Update the following configuration file: '/etc/phpmyadmin/config.inc.php'. After the block of code that looks like:

if(!empty($dbname)){ //*  a block of code */   }

Add the following:

/*
 * Cloud Database config added
 */
$i++;
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = ‘PRIVATE HOSTNAME OF CLOUD DB INSTANCE’;
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
/* Select mysqli if your server has it */
$cfg['Servers'][$i]['extension'] = 'mysql';

Save the file and exit.

Moving away from the master server for the moment, use the Rackspace control panel to create a new server node. Use the ‘BaseImg’ saved earlier as the boot image. Make a note of the IP and password details provided by the control panel.

We will now create the load balancer and configure it to use the master server and clone server.

Use the Rackspace interface to create a Load balancer. Set the session persistence option to ‘on’ and the algorithm to ‘round robin’. Add the master server and clone server as part of the cluster available to the load balancer.

On the master server you will now create an SSH key which will be provided to the clone server so the master server can access the clone server without passwords.

Whilst logged into the master server, run:

# ssh-keygen

You will be asked to enter a filename, leave this blank and Enter. This will create two files corresponding to a private and public key. The public key is what we need to provide to the clone server. it is stored at ‘public/root/.ssh/id_rsa.pub.

Copy the key over to the clone server by running:

# ssh-copy-id root@clone.server.private.ip

If the copying process was successful, you should be able to SSH log into the clone server from the master server without requiring a password.

You can re-secure SSH at this point on both master and clone servers by preventing SSH root access:

  • On clone server change /etc/ssh/sshd_config PermitRootLogin no to PermitRootLogin without-password
  • On master server change /etc/ssh/sshd_config PermitRootLogin yes to PermitRootLogin no

Now that the master and clone servers are configured to talk to each other without interference, we can install a tool for replication. This needs to take place on the master server.

Install LSync on the master server:

# apt-get install lua5.1 liblua5.1-dev pkg-config rsync asciidoc make –y

# cd /var/tmp

# wget http://lsyncd.googlecode.com/files/lsyncd-2.1.4.tar.gz

# tar xzvf lsyncd-2.1.4.tar.gz

# cd lsyncd-2.1.4

# ./configure && make && make install

Create a startup script so LSync automatically runs at boot. Put the block below inside "/etc/init/lsyncd.conf":


description "lsyncd file syncronizer"

start on (starting network-interface
		or starting network-manager
		or starting networking)

stop on runlevel [!2345]

expect fork

respawn
respawn limit 10 5

exec /usr/local/bin/lsyncd /etc/lsyncd.lua

create the symbolic link:

# ln -s /lib/init/upstart-job /etc/init.d/lsyncd

Configure logging for Lsync:

# mkdir /var/log/lsyncd

Put the following block inside "/etc/logrotate.d/lsyncd":

			
/var/log/lsyncd/*log {
    			missingok
    			notifempty
    			sharedscripts
    			postrotate
    			if [ -f /var/lock/lsyncd ]; then
      			/sbin/service lsyncd restart > /dev/null 2>/dev/null || true
    			fi
    			endscript
			}

Create a configuration file for Lsyncd. Inside the /etc/lsyncd.lua place the following configuration:

settings {
    logfile = "/var/log/lsyncd/lsyncd.log",
    statusFile = "/var/log/lsyncd/lsyncd-status.log",
    statusInterval = 20
}

sync {
    default.rsync,
    source="/path/to/www/dir/",
    target="PRIVATE.IP.OF.CLONE.SERVER:/path/to/www/dir/",
    rsync = {
        compress = true,
        acls = true,
        verbose = true,
        rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"
    }
}

You can now start LSyncd:

# start lsyncd

You can test the configuration by creating a file within the selected directory and checking to see if the file appears on the clone server. You can use the ‘touch’ command to create a file.

# touch /path/to/www/dir/testfile.test

The master server is now fully configured! Make an image for backup purposes called ‘MasterServerBackup’.

The final step is to configure the clone server with a reverse proxy. There may be certain tasks that must take place on the master server in order to propagate changes across the clone servers using replication.

A good tool for this is the varnish reverse proxy application. Follow the following steps to install and configure it:

# apt-get install varnish –y

Get ready to update the basic configuration by making a backup of the default configuration provided by the package:

# mv /etc/varnish/default.vcl /etc/varnish/default.vcl.backup

I’ve created a configuration just for redirection purposes. Varnish can be used to cache certain request too. Create the following script within /etc/varnish/default.vcl:

backend default {
  .host = "127.0.0.1";
  .port = "8080";
}

backend master {
  .host = "PRIVATE.IP.OF.MASTER.SERVER";
  .port = "80";
}

sub vcl_recv {
  if (!req.http.X-Forwarded-For) {
    set req.http.X-Forwarded-For = client.ip;
  }

  if (req.restarts > 0 ||
      req.url ~ "(phpmyadmin|cron|another-specific-url-string-that-should-go-to-master-server-pipe-separated)") {
    set req.backend = master;
    return(pass);
  }

  return(pass);

}

sub vcl_fetch {
  if (beresp.status == 404 && req.restarts == 0) {
    return(restart);
  }
}

Make varnish listen on port 80 so it is the first contact point for incoming requests:

# perl -pi -e 's/6081/80/;' /etc/default/varnish

As Varnish is now listening to outside requests, Apache must now listen on a secondary port. Change the port that apache listens on from ‘80’ to ‘8080’:

# sed -i 's/Listen 80/Listen 8080/g' /etc/apache2/ports.conf

Restart both apache and varnish so the changes take effect:

# service apache2 restart

# service varnish restart

You can at this point make a final image backup for the clone server called ‘CloneFinalBackup’.

When you are scaling the servers in the future this image is central. You create as many servers as required using the image. Then, add the private IP’s to the load balancer pool. On the master server, you would need to update the Lsync configuration so the new clones are included in replication tasks.

The servers are now ready for incoming requests. If you update your DNS records for associated domains to point to the public IP of the load balancer everything should work in the following sequence:

  • External HTTP request is routed to the Load balancer.
  • Load balancer selects a server to fulfil the request based upon its pool and selection algorithm. If the visitor has been to a particular server before, the request is routed to the same server.
  • If the request lands on a non-master server, Varnish checks the request against urls that should be routed to the master server. If a match is found the request goes back to the master server from the clone server.
  • Request is fulfilled.

Category: cloud server

Tags: load balancing server structure

0 comments

Login or Register to post comments.

My Work!

Value My Car Android app logo

Value My Car Android app

Value any car for free - all makes and models available.

Get a free valuation of any car based on car depreciation trends for the specific make and model, the price you paid, your length of ownership and mileage covered. You can find out how much a car purchased at any point during its life-cycle will continue to depreciate.

view app
Value My Car iOS app logo

Value My Car iOS app

Value any car for free - all makes and models available.

Get a free valuation of any car based on car depreciation trends for the specific make and model, the price you paid, your length of ownership and mileage covered. You can find out how much a car purchased at any point during its life-cycle will continue to depreciate.

view app
NiceAreas.co.uk Website logo

NiceAreas.co.uk Website

Search For UK Areas Based on any Criteria

A number of tools to help research parts of the UK down to postcode sector level using multiple datasets and a variety of data combination and aggregation.

Including crime, house prices, travel links, jobs and more!

visit website
Mortgage Calculator Expert iOS app logo

Mortgage Calculator Expert iOS app

This comprehensive mortgage calculator includes eight different calculators in one app!

Adjust repayment types, rate types, initial offers/periods, base rates, terms, SVR's, overpayments, collars/caps, any fees (add to loan or upfront), mortgage start dates, simulated rate changes and more!

view app
Mortgage Calculator Expert Android app logo

Mortgage Calculator Expert Android app

This comprehensive mortgage calculator includes eight different calculators in one app!

Adjust repayment types, rate types, initial offers/periods, base rates, terms, SVR's, overpayments, collars/caps, any fees (add to loan or upfront), mortgage start dates, simulated rate changes and more!

view app
Amazon Alexa Skills logo

Amazon Alexa Skills

Multiple Alexa Skills

Voice-first (with additional display elements too) skills for Alexa devices.

Including:

BrainPickers! Guess The ... series of games.

SalaryBee - Salary calculations by voice.

TheMoneyCalculator - Finance calculations by voice.

NiceAreas - Property valuations, Area information and census information by voice.

Who Said That? Friends Quiz.

..plus...more to come!

visit website
UKTaxCalculators.co.uk iOS app logo

UKTaxCalculators.co.uk iOS app

The UK's most comprehensive FREE tax calculator app

The website condensed into a full featured and easy to use iPhone/iPad/iPod app!

Check tax on any income source, compare salaries and much more!

view app
UKTaxCalculators.co.uk Android app logo

UKTaxCalculators.co.uk Android app

The UK's most comprehensive FREE tax calculator app

The website condensed into a full featured and easy to use Android app!

Check tax on any income source, compare salaries and much more!

view app
UKTaxCalculators.co.uk Amazon app logo

UKTaxCalculators.co.uk Amazon app

The UK's most comprehensive FREE tax calculator app

The website condensed into a full featured and easy to use Amazon app!

Check tax on any income source, compare salaries and much more!

view app
UK Tax Pro iOS app logo

UK Tax Pro iOS app

Calculate UK Tax on any income type with single or multiple income sources.

Get a full breakdown of your tax, print/email from within the app.

Full compliment of professional features such as rates and allowances, news, guides and calendars.

view app
Parcel Dogs Game For iOS logo

Parcel Dogs Game For iOS

This 'top-down' racing game involves the player taking the role of Jim, our Speedy Parcel delivery dog. Now Jim has to go around his picking up parcels with the help of his map but has a hard time of it thanks to his nemesis Crazy Dave.
visit website
UKTaxCalculators.co.uk Website logo

UKTaxCalculators.co.uk Website

The UK's most comprehensive FREE online tax calculator suite

The Tax Calculator provides a full breakdown of tax for PAYE, CIS or Self Employed individuals providing options for Pension Calculations, Tax Codes, NIC Letters, Bonuses, Salary Sacrifice Schemes and more.

For people with multiple income sources, the UK Tax Calculators Wizard calculates every UK tax across all incomes in the same manner a tax return would.

visit website
Walleto.co.uk Website logo

Walleto.co.uk Website

The first truly easy to use, free online budget planner

There has never been an easier way to forecast your finances in less 5 minutes!

The budget calculator takes all your income details, across any type of income, calculates the taxes due, subtracts your expenses (both from extensive presets to your own defined expenses). You are presented with an online forecast, comparisons with other users with similar households as well as a printable report version to use as income and expenses chart.

visit website
TheMoneyCalculator.com Website logo

TheMoneyCalculator.com Website

Full featured online mortgage and loan calculators

You can use this website to calculate mortgage payments, forecast future interest rate effects, calculate remaining balances, check debt consolidation options, plan your savings, plan your pension as well as plan for your future house purchase. A lot of planning made easy!

Find a professional mortgage adviser or financial adviser online through our UK-wide network.

visit website
WhereDidMyTaxGo.co.uk Website logo

WhereDidMyTaxGo.co.uk Website

Ever wanted to know where the UK Government has been spending YOUR tax payments?

The WhereDidMyTaxGo.co.uk website takes your gross income since 2003, calculates your taxes (PAYE only), and uses the Governments expenditure data to give you an approximation of how much of your actual payments were spent where.

The personalised aspect of this website is a unique use of Government data.

visit website
TaxPenny.co.uk Website logo

TaxPenny.co.uk Website

Finding an Accountant the Smart Way

TaxPenny provide an online platform for accountants and those seeking an accountant to connect. Potential clients describe their circumstances, the work required and set a maximum price rage. Quotes are returned to clients within 1 working day. Accountants are able to search for new clients through various criteria and bid auction style for leads or purchase adhoc.

visit website
World Tax Calculator iOS app logo

World Tax Calculator iOS app

Compare Tax and Cost of Living of Major Economies

Calculate and/or compare taxes for major economies across the world, see differences in take home pay and see a cost of living comparison with a list of common purchases people make.

view app
World Tax Calc Android app logo

World Tax Calc Android app

Compare Tax and Cost of Living of Major Economies

Calculate and/or compare taxes for major economies across the world, see differences in take home pay and see a cost of living comparison with a list of common purchases people make.

view app

My Blog!

27/01/15 19:37:10

Using Cloud-based Load Balancing To Horizontally Scale Effectively

In this quick guide, we will go through what is required to convert an existing singular LAMP setup to a horizontally distributed network of servers sitting behind a Load Balancer. This guide will utilise a number of services from Rackspace Cloud.

cloud server

0

19/12/14 11:13:49

Creating Servers For Load Balancing

In this step-by-step guide we create the underlying server structure required for successful load balancing.

cloud server

0

14/09/14 14:40:59

Creating a Shared Memcached Cloud Instance

Key-value memory based storage can be a significant performance boost, as well as load reducer, for database heavy websites. Follow our step-by-step guide to setting one up using the Memcached application.

cloud server

0

27/06/14 12:12:01

Copying Data Across From Existing MySQL Databases to a Cloud Database

Now that you have created a database in the cloud, how do you get it populated as quickly as possible. Follow our step-by-step guide.

cloud server

0

27/03/14 13:14:05

Converting a Single Node MySQL Application to a Cloud Databases Instance

Switching from an existing single database application to a database in the cloud requires two steps. First is to actually create the cloud database instance. The second is to migrate your existing data across. We describe the steps required.

cloud server

0

.. or read all of my blog!

Get in touch!

Do you have any questions for me? An idea for an exciting new website or mobile application? Maybe you just want to leave a comment regarding one of my websites or apps? If yes then you've found the right place!

Complete the form below and I will get back to you as soon as possible. If you would prefer to get in touch via Twitter, you can reach me there using @rayarmanappdev.