Step by Step Setup 2 Webservers with MySQL cluster and 1 Load Balancer on Amazon EC2


This time I’m going to show you how to setup 2 webservers with MySQL cluster and 1 Load Balancer using Amazon Elastic Compute Cloud (EC2) instances, as shown in the above diagram. If the diagram text is too small that you can’t read it, click on the diagram and you can see the bigger version.

Now, I assume that you have gone through my previous 2 guides:

as we are going to use the LAMP server instance that we have created in the Setting up a LAMP server guide. If you haven’t, you need to be familiar with running and saving Amazon EC2 instances, and also have a LAMP server AMI ready to be used and run. A LAMP server means that you need to have at least a Linux instance with Apache webserver, MySQL database client & server, and PHP installed.

Run 3 LAMP instances

First of all, launch 3 instances of the LAMP AMI. We’re going to assign each of this instances as one of the following servers.

  • Management Instance
    • Public DNS: ec2-67-202-62-113.compute-1.amazonaws.com
    • Public IP: 67.202.63.113
    • Private DNS: ip-10-251-235-15.ec2.internal
    • Private DNS: 10.251.235.14
  • LAMP Instance 1
    • Public DNS: ec2-75-101-246-9.compute-1.amazonaws.com
    • Public IP: 75.101.246.9
    • Private DNS: ip-10-251-182-160.ec2.internal
    • Private IP: 10.251.182.160
  • LAMP Instance 2
    • Public DNS: ec2-72-44-39-69.compute-1.amazonaws.com
    • Public IP: 72.44.39.69
    • Private DNS: ip-10-251-143-191.ec2.internal
    • Private IP: 10.251.143.191

Record your instances DNS and IP addresses details, and change the public DNS and private DNS on the above diagrams to match you instances. You can easily get a string copy of the DNSs using ElasticFox

Configure MySQL Management Instance

SSH and connect to the Management Instance. We don’t need Apache Web Server and mySQL server running on our Management Instance. Hence, we’re going to uninstall Apache and stop mySQL server from running.

Let’s stop Apache and mySQL from running first.

/etc/init.d/apache2 stop
/etc/init.d/mysql stop

I also would like to make sure that mySQL will not be run on startup. To do this I’m using an application called sysv-rc-conf. Install this using the following command.

apt-get install sysv-rc-conf

Once it’s installed run

sysv-rc-conf

and remove all the MySQL startup component.

To uninstall Apache, enter the following command.

apt-get remove apache2

I know that this way is not very efficient, but since we’re continuing from the previous guide, I just want to use the AMI that we’ve created before. Optionally, you can start from a clean base Linux AMI, and just install the mySQL management application.

Create the mySQL cluster management directory (if it doesn’t exist yet).

mkdir /var/lib/mysql-cluster

Then, go to the directory and setup the config.ini file.

cd /var/lib/mysql-cluster emacs config.ini

Note that I’m using emacs as the text editor here. But feel free to use any text editor you like.

Next, insert the following text and change the DNS addresses corresponding to your instances private DNS addresses. You can put either the DNS name as below or in the xxx.xxx.xxx.xxx format (Whichever you prefer).

[NDBD DEFAULT]
NoOfReplicas=2
[MYSQLD DEFAULT]
[NDB_MGMD DEFAULT]
[TCP DEFAULT]
# Managment Server
[NDB_MGMD]
HostName=ip-10-251-235-15.ec2.internal # the IP/DNS of THIS SERVER
# Storage Engines
[NDBD]
HostName=ip-10-251-182-160.ec2.internal # the IP/DNS of the FIRST SERVER
DataDir= /var/lib/mysql-cluster
[NDBD]
HostName=ip-10-251-143-191.ec2.internal # the IP/DNS of the SECOND SERVER
DataDir=/var/lib/mysql-cluster
# 2 MySQL Clients
# I personally leave this blank to allow rapid changes of the mysql clients;
# you can enter the hostnames of the above two servers here. I suggest you dont.
[MYSQLD]
[MYSQLD]

Next, start the management server. ndb_mgmd That’s all for the MySQL management server. You can keep this application running (it uses very small amount of memory and CPU power), or shut it down if you like.

Configure MySQL Servers

We need to perform this section on both LAMP Instances 1 and LAMP instance 2.

First, SSH to the instance and stop any running mySQL server

/etc/init.d/mysql stop

Next, we need to modify the /etc/my.cnf file.

emacs /etc/mysql/my.cnf

Add the the following lines in the appropriate section of the file. Make sure to change the DNS/IP addresses to your Management Instance address.

[mysqld]
ndbcluster
ndb-connectstring=ip-10-251-235-15.ec2.internal # the IP/DNS of the MANAGEMENT INSTANCE SERVER
[mysql_cluster]
ndb-connectstring=ip-10-251-235-15.ec2.internal # the IP/DNS of the MANAGEMENT INSTANCE SERVER

Create the data directory /var/lib/mysql-cluster (if it’s not already exist) .

mkdir /var/lib/mysql-cluster

Then start the storage engine and start mySQL server.

ndbd
/etc/init.d/mysql start

Now we need to repeat the same procedure on the second instance (LAMP Instance 2).

Testing MySQL cluster configuration

Once you have completed the setup on both LAMP instances, it’s time to test the cluster configuration.

In the Management Server, enter the management console with this command.

/usr/local/mysql/bin/ndb_mgm

Then enter the

show

command to see the connection status of the MySQL cluster, as shown below.


Now you can test the configuration on the MySQL servers on both LAMP instances.

Enter MySQL console on either LAMP Instance 1 or LAMP Instance 2 and enter the following commands (assuming you have setup a root password form mysql console).

mysql -u root -p
use test;

Note: If database test doesn’t exist in your mysql, enter create database test to create the database.

CREATE TABLE mytest (i INT) ENGINE=NDBCLUSTER;
INSERT INTO mytest () VALUES (10);
SELECT * FROM mytest;


Now, go to the the other LAMP instance, and run the following commands.

mysql -u root -p
use test;
SELECT * FROM mytest;

If the cluster configuration is working properly, you should get the same output of the SELECT command.


If it’s working for you, CONGRATULATIONS!!!!

Setting Up HAProxy on Management Instance

SSH to the management instance, and install HAProxy.

apt-get install haproxy

We need to modify the configuration file. But before that is better to backup the original file.

cp /etc/haproxy.cfg /etc/haproxy.cfg.orig
cat /dev/null > /etc/haproxy.cfg
emacs /etc/haproxy.cfg

Now, add the following into the configuration file. Don’t forget to change the webserver IP/DNS addresses to your instances addresses.

global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
#debug
#quiet
user haproxy
group haproxy

defaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000

listen webfarm *:80 #HAPROXY listens to ALL IP’s Port 80
mode http
stats enable
stats auth someuser:somepassword
balance roundrobin
cookie JSESSIONID prefix
#cookie SERVERID insert indirect
option httpclose
option forwardfor
server webA ip-10-251-143-191.ec2.internal:80 cookie A check #LAMP server instance 1 IP/DNS
server webB ip-10-251-182-160.ec2.internal:80 cookie B check #LAMP server instance 2 IP/DNS

Save and close the file once you’re done.

Next, we need to set ENABLED to 1 in /etc/default/haproxy

# Set ENABLED to 1 if you want the init script to start haproxy.
ENABLED=1
# Add extra flags here.
#EXTRAOPTS=”-de -m 16″

We can start HAProxy on the Management Instance using the following command/

etc/init.d/haproxy start

You can now test the load balancer by opening the Management Instance public DNS on your web browser. In my example, its http://ec2-67-202-62-113.compute-1.amazonaws.com

You might want to put make 2 different index.html file for the web servers to test that the load balancer does send your http request in round robin method to the web servers.

Fox example, in LAMP Instance 1 you can add the following test in /var/www/index.html

You have been directed to LAMP Instance 1

While the following can be added to the /var/www/index.html in LAMP Instance 2.

You have been directed to LAMP Instance 2

Have a go at testing it. Let me know if it doesn’t work or you found any errors in my guide.

That’s all for now folks. I’m still playing around to get high availability working in Amazon EC2 (that is having a standby load balancer, in case of fault on Management Instance). If anyone have any tips on doing this, I would appreciate if you can share it with me.

 

Advertisements
Leave a comment

Leave your opinion

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: