Thursday, August 25, 2011

Setting Up Network RAID1 With DRBD On Debian Squeeze


 This tutorial shows how to set up network RAID1 with the help of DRBD on two Debian Squeeze systems. DRBD stands for Distributed Replicated Block Device and allows you to mirror block devices over a network. This is useful for high-availability setups (like a HA NFS server) because if one node fails, all data is still available from the other node.
I do not issue any guarantee that this will work for you!

1 Preliminary Note

I will use two servers here (both running Debian Squeeze):
  • server1.example.com (IP address 192.168.0.100)
  • server2.example.com (IP address: 192.168.0.101)
Both nodes have an unpartitioned second drive (/dev/sdb) with identical size (30GB in this example) that I want to mirror over the network (network RAID1) with the help of DRBD.
It is important that both nodes can resolve each other, either through DNS or through /etc/hosts. If you did not create DNS records for server1.example.com and server2.example.com, you can modify /etc/hosts on both nodes as follows:
server1/server2:
vi /etc/hosts
127.0.0.1       localhost.localdomain   localhost
192.168.0.100   server1.example.com     server1
192.168.0.101   server2.example.com     server2

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

2 Synchronize Time

server1/server2:
It is very important that both nodes have the same time. Therefore we install the ntp packages:
apt-get install ntp ntpdate

3 Partition /dev/sdb

server1/server2:
Right now, our partitioning looks as follows:
fdisk -l
root@server1:~# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00029d5c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        3793    30461952   83  Linux
/dev/sda2            3793        3917      992257    5  Extended
/dev/sda5            3793        3917      992256   82  Linux swap / Solaris

Disk /dev/sdb: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table
root@server1:~#
As you see, /dev/sdb is not partitioned. We change that now and create one big partition on it, /dev/sdb1:
fdisk /dev/sdb
root@server1:~# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x8042e800.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help):
 <-- n
Command action
   e   extended
   p   primary partition (1-4)

<-- p
Partition number (1-4): <-- 1
First cylinder (1-3916, default 1): <-- ENTER
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-3916, default 3916):
 <-- ENTER
Using default value 3916

Command (m for help):
 <-- t
Selected partition 1
Hex code (type L to list codes):
 <-- 83

Command (m for help): <-- w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
root@server1:~#

Now run
fdisk -l
again, and you should find /dev/sdb1 in the output:
root@server1:~# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00029d5c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        3793    30461952   83  Linux
/dev/sda2            3793        3917      992257    5  Extended
/dev/sda5            3793        3917      992256   82  Linux swap / Solaris

Disk /dev/sdb: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x78f21e78

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        3916    31455238+  83  Linux
root@server1:~#

4 Install And Configure DRBD

 
server1/server2:
Now install DRBD on both nodes as follows:
apt-get install drbd8-utils
Load the DRBD kernel module:
modprobe drbd
To check if it is loaded, run:
lsmod | grep drbd
Output should be similar to this one:
root@server1:~# lsmod | grep drbd
drbd                  193312  0
lru_cache               5042  1 drbd
cn                      4563  1 drbd
root@server1:~#
Now we back up the original /etc/drbd.conf file and create a new one on both nodes as follows:
cp /etc/drbd.conf /etc/drbd.conf_orig
cat /dev/null > /etc/drbd.conf
vi /etc/drbd.conf
global { usage-count no; }
common { syncer { rate 100M; } }
resource r0 {
        protocol C;
        startup {
                wfc-timeout  15;
                degr-wfc-timeout 60;
        }
        net {
                cram-hmac-alg sha1;
                shared-secret "secret";
        }
        on server1.example.com {
                device /dev/drbd0;
                disk /dev/sdb1;
                address 192.168.0.100:7788;
                meta-disk internal;
        }
        on server2.example.com {
                device /dev/drbd0;
                disk /dev/sdb1;
                address 192.168.0.101:7788;
                meta-disk internal;
        }
}
Make sure you use the correct node names in the file (instead of server1.example.com and server2.example.com) - please make sure you use the node names that the command
uname -n
shows on both nodes. Also make sure you fill in the correct IP addresses in the address lines and the correct disk in the disk lines (if you don't use /dev/sdb1).
Now we initialize the meta data storage. On both nodes run:
drbdadm create-md r0
root@server1:~# drbdadm create-md r0
Writing meta data...
initializing activity log
NOT initialized bitmap
New drbd meta data block successfully created.
root@server1:~#
Then start DRBD on both nodes:
/etc/init.d/drbd start
root@server1:~# /etc/init.d/drbd start
Starting DRBD resources:[ d(r0) s(r0) n(r0) ]....
root@server1:~#
The next step has to be carried out on server1 only:
server1:
Now make server1 the primary node:
drbdadm -- --overwrite-data-of-peer primary all
Afterwards, data will start to synchronize between server1 and server2.
server2:
Take a look at
cat /proc/drbd
to see the synchronization progress:
root@server2:~# cat /proc/drbd
version: 8.3.7 (api:88/proto:86-91)
srcversion: EE47D8BF18AC166BE219757
 0: cs:SyncTarget ro:Secondary/Primary ds:Inconsistent/UpToDate C r----
    ns:0 nr:15790400 dw:15790144 dr:0 al:0 bm:963 lo:9 pe:29622 ua:8 ap:0 ep:1 wo:b oos:15664096
        [=========>..........] sync'ed: 50.3% (15296/30716)M
        finish: 0:02:44 speed: 95,212 (85,352) K/sec
root@server2:~#
(You can run
watch cat /proc/drbd
to get an ongoing output of the process. To leave watch, press CTRL+C.)
Wait until the synchronization has finished - output should be as follows:
root@server2:~# cat /proc/drbd
version: 8.3.7 (api:88/proto:86-91)
srcversion: EE47D8BF18AC166BE219757
 0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r----
    ns:0 nr:31454240 dw:31454240 dr:0 al:0 bm:1920 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
root@server2:~#
The snippet ro:Secondary/Primary tells you that this node is the secondary node.
server1:
On server1, the output of
cat /proc/drbd
is as follows (after the synchronization has finished):
root@server1:~# cat /proc/drbd
version: 8.3.7 (api:88/proto:86-91)
srcversion: EE47D8BF18AC166BE219757
 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----
    ns:31454240 nr:0 dw:0 dr:31454440 al:0 bm:1920 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
root@server1:~#
The snippet Primary/Secondary tells you that this is the primary node.
Now that we have our new network RAID1 block device /dev/drbd0 (which consists of /dev/sdb1 from server1 and server2), let's create an ext3 filesystem on it and mount it to the /data directory. This has to be done only on server1!
mkfs.ext3 /dev/drbd0
mkdir /data
mount /dev/drbd0 /data
Afterwards you should see /dev/drbd0 in the outputs of...
mount
root@server1:~# mount
/dev/sda1 on / type ext3 (rw,errors=remount-ro)
tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
udev on /dev type tmpfs (rw,mode=0755)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
/dev/drbd0 on /data type ext3 (rw)
root@server1:~#
... and:
df -h
root@server1:~# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              29G  775M   27G   3% /
tmpfs                 249M     0  249M   0% /lib/init/rw
udev                  244M  112K  244M   1% /dev
tmpfs                 249M     0  249M   0% /dev/shm
/dev/drbd0             30G  173M   28G   1% /data
root@server1:~#

5 Test

server1:
Now let's create some files or directories in the /data directory and check whether they get replicated to server2.
touch /data/test1.txt
touch /data/test2.txt
ls -l /data/
root@server1:~# ls -l /data/
total 16
drwx------ 2 root root 16384 Aug  8 12:45 lost+found
-rw-r--r-- 1 root root     0 Aug  8 12:48 test1.txt
-rw-r--r-- 1 root root     0 Aug  8 12:48 test2.txt
root@server1:~#
Now let's unmount the /data directory on server1:
umount /data
Then assign the secondary role to server1:
drbdadm secondary r0
Now we go to server2, make it the primary node and check if we can see the files/directories we created on server1 in the /data directory on server2.
server2:
First we assign the primary role to server2:
drbdadm primary r0
Check the output of
cat /proc/drbd
... and you should see that server2 is the primary node now:
root@server2:~# cat /proc/drbd
version: 8.3.7 (api:88/proto:86-91)
srcversion: EE47D8BF18AC166BE219757
 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----
    ns:4 nr:32083300 dw:32083304 dr:325 al:1 bm:1920 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
root@server2:~#
Next we create the /data directory and mount /dev/drbd0 to it:
mkdir /data
mount /dev/drbd0 /data
Let's check the contents of the /data directory:
ls -l /data/
If everything went fine, it should contain the files/directories that we created on server1:
root@server2:~# ls -l /data/
total 16
drwx------ 2 root root 16384 Aug  8 12:45 lost+found
-rw-r--r-- 1 root root     0 Aug  8 12:48 test1.txt
-rw-r--r-- 1 root root     0 Aug  8 12:48 test2.txt
root@server2:~#
server1:
Now that we have switched roles, the output of
cat /proc/drbd
on server1 should show you that server1 has the secondary role:
root@server1:~# cat /proc/drbd
version: 8.3.7 (api:88/proto:86-91)
srcversion: EE47D8BF18AC166BE219757
 0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r----
    ns:32083300 nr:4 dw:629064 dr:31454797 al:529 bm:2044 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
root@server1:~#

6 Links


No comments:

Post a Comment