DRBD and MySQL – Virtualbox Setup

Introduction

DRBD is
a Linux project to provide a real time distributed filesystem.  Part two
of this series will dig deep into a real-world installation example.  We’ll
use Sun’s virtualbox software to create a couple of VMs, then configure those VMs
with DRBD, and finally install and test MySQL running on volumes sitting on
DRBD.

Virtualbox – An Easy and Powerful VM Solution

Sun has
a great free virtual machine solution called Virtualbox.  There are many
such options, but I chose theirs because it runs on Linux, Windows and Mac, so
it should work for everyone.  

The
download can be found here:

http://www.virtualbox.org/wiki/Downloads

It of
course comes with a simple GUI installer, so I won’t go through that in too
much detail.  That is pretty straightforward.  Once you have that
installed, fire it up and we’ll run through the steps to create a vm. 
Since we will also be installing CentOS on our virtual machines, I recommend
you go find that here:

http://mirror.centos.org/centos/5/isos/

Create Your VMs

I went
with penguin names.  The first host is called adelie, and the second one
called african.  The steps are as follows:

1. Create
the vm using the virtualbox GUI by clicking "New".  A wizard
will take you through the process.  Enter the name, specify
"Linux" as the OS, and select Redhat 64 bit as the OS. 
Unfortunately it won’t install that for  you too, but it wouldn’t really
be a true vm if it did that for you.  Leave base memory, boot hard disk,
create new hard disk, etc. all at their defaults.  It creates an 8GB disk
and I left that at default too.  Then click finish and it will create the
virtual disk.  Click finish again and it will create the vm.

2. You’ll
need to add a second disk for your DRBD use.  A small 25M disk will be
fine. Click the "Settings" for your new vm, and then select the
"Storage" tab. For simplicities sake we used a new SCSI controller. Click
the add disk button a few times until the wizard comes up.  Click
"Create".  Use defaults, and give it a name like adelie_data.vdi,
then make it 25M in size.

3. We’ll
also need to configure the networking interface.  By default, virtualbox
makes it NAT.  This doesn’t allow us to see other machines in the
sandbox.  So click the "Network" tab, and select "Bridged
Adapter" from the popup menu.

4. Per
our recommendation above, if you haven’t already, download your CentOS ISO for
5.4.

5. We’ll
also need to specify the ISO media.   This can be a little tricky as the
interface is a little odd in this regard.  Go back to the
"Storage" tab, and click on the CD/DVD symbol under IDE
Controller.  Click the little folder icon on the right hand side, and a
popup comes up.  If you don’t see your ISO image, use the "Add"
button, and find your ISO and select it.  The virtualbox will treat your
ISO as a mounted CD image.

6. Start
your engines.  That’s right, finally we can start the vm.  Hopefully
the CentOS installer comes up.  If you have trouble, use the disk symbol
at the bottom of the window, click for the menu, and select your DVD ISO of CentOS
5.4.

7. 
Install CentOS 5.4.  Run through the screens and click OK.

8.
Verify and start your network.  Hopefully this has already happened by
default with DHCP.  See also these commands:


$ /sbin/ifconfig eth0
$ /sbin/ifup eth0

You’ll
now need to run through these same eight steps again to create the second node,
we called it african.

Install DRBD on both Nodes

1. Use
yum to install the drbd tools, and the kernel module:


$ yum install drbd83.x86_64
$ yum install kmod-drbd83.x86_64

2.
Install the mysql server software:


$ yum install mysql-server.x86_64

3.
Install emacs:


$ yum install emacs

4.
Configure drbd.conf as follows (same on both boxes).  Note the IP
addresses, device and disk specification.  If you created your virtualbox
just like we did, these should all be the same.  IP address will still
depend on your DHCP server.


common {
protocol C;
}
resource sean {
on adelie {
device /dev/drbd0;
disk /dev/sda1;
meta-disk internal;
address 192.168.0.106:7789;
}
on african {
device /dev/drbd0;
disk /dev/sda1;
meta-disk internal;
address 192.168.0.105:7789;
}
}

Start DRBD

This
part turned out to be a huge struggle.  I configured it, but tried over
and over to no avail to get things working.  It turned out that the
firewall that Linux by default installs, blocked the ports 7789 on both
machines.  So DRBD couldn’t reach each other on the two nodes.  This
failed silently and repeatedly and all I knew was that it wasn’t
connecting.  So be sure to disable iptables.  Since these are virtual
boxes, I recommend just deleting the file:  /etc/rc.d/rc3.d/S08iptables

Use fdisk
to create a partition roughly 25M in size on each node.


$ fdisk /dev/sda

Use drbdadm to create the initial mirrored device as follows:


$ drbdadm create-md sean

This
will synchronize all data between the two volumes for the first time. 
Note that if you have data you want to preserve you should (a) do a backup of
it of course, and then (b) perform this step on the node whose data you want to
preserve!


$ drbdadm — –overwrite-data-of-peer primary sean

Verify syncronization
with:


$ watch cat /proc/drbd

Create a
filesystem in the usual way:


$ mkfs.ext3 /dev/drdb0

Mount it
on /mnt/mysql (create that dir if necessary):


$ mount /dev/drbd0 /mnt/mysql

Configure MySQL and Start

Edit the
/etc/my.cnf file and set the datadir as follows:


datadir=/mnt/mysql

We’ll
simple start mysql manually now:


$ mysqld_safe &

Let’s
create a table and add a row:


$ mysql
mysql> use test;
mysql> create table sean_test (c1 varchar(64));
mysql> insert into sean_test values (‘hi there’);
mysql> commit;
mysql> quit

Test Switching to the Other Node

To
verify what’s happening on the other node you’ll need to "promote" it
to being the primary.  Do the following:

On your
current primary do:


$ ps auxw | grep mysql
$ kill <procid>

$ umount /mnt/mysql

$ drbdadm secondary sean

Then on
the other node do:


$ drbdadm primary sean

Mount
it:


$ mount /dev/drbd0 /mnt/mysql

Start
MySQL:


$ mysqld_safe &

Lastly
login and see your change:


$ mysql
mysql> use test;
mysql> select * from sean_test;
mysql> quit

Conclusion

DRBD is
some sophisticated technology.  Luckily, with today’s VM technology such
as Sun’s Virtualbox, we can set up a couple of virtual machines quite easily,
and test out the technology.  We’ve tried to break that whole process down
into really simple steps.  You should be able to follow those and get your
whole DRBD setup in no time.  Hopefully the lessons we learned in the
process will save you some time.

In our
next installment of this series, we’ll explain how to set up heartbeat to
handle the failover automatically and gracefully.  This is the piece that
we describe in "test switching to the other node" above.  Heartbeat
will automatically perform all of these steps, and determine when a node is out
of commission.

»
See All Articles by Columnist Sean Hull

Sean Hull
Sean Hull
Sean Hull is a business and technology consultant, author and speaker. He is the founder and senior consultant at Heavyweight Internet Group in New York and has worked as a database consultant and expert for ten years. He is the author of "Oracle and Open Source" on O'Reilly & Associates and his articles have appeared in Oracle's own OTN website, DBA Zine, ChangeThis.com, and many others. Using techniques, technologies, and perspectives discussed in the book, articles, and seminars, he has taken his career into exciting areas. He has served as Technology Consultant, Project Manager, CEO, and mentor with a background in programming, systems integration & administration, project development and management. He can be reached at shull@iheavy.com or visit http://www.iheavy.com for more info about consulting services.

Get the Free Newsletter!

Subscribe to Cloud Insider for top news, trends & analysis

Latest Articles