Contents
Part 1、Install && Configure    drbd.8.4.1-ins-cfg.sh
Part 2、Set primary
Part 3、Test sync, manual change role


Part 1

[root@localhost~]# vim drbd.8.4.1-ins-cfg.sh

#!/bin/bash
# 2012-07-29
# OS: rhel 6.2
# Primary node: node8.example.com 192.168.0.218
# Secondary node: node9.example.com 192.168.0.219
# Pre Conditions: disabled iptables,selinux,NetworkManager
# Both node has a virto disk: vdb

###############################################
# Opertions on Both Primary && Secondary node #
###############################################

# Configure resolve
cat >> /etc/hosts <<EOF
node8.example.com 192.168.0.218
node9.example.com 192.168.0.219
EOF

# Prepare partition for drbd device
# disk: vdb
# partition: vdb1
fdisk -cu /dev/vdb <<EOF
n
p
1


w
EOF

# Prepare mountpoint of drbd
mkdir -p /drdbdata

# Install dependent packages
yum install -y gcc flex make kernel-devel kernel-headers

# Download drbd from website(Here is link of source code package)
wget http://oss.linbit.com/drbd/8.4/drbd-8.4.1.tar.gz

# Decompress tar.gz
tar -zvxf drbd-8.4.1.tar.gz

# Change work directory
cd drbd-8.4.1

# Compile configure
# Meanning of parameter:
# --prefix= assign progress install path
# --with-km
./configure --prefix=/usr/local/drbd --with-km

# Compile
# Meanning of parameter:
# KDIR= assign system kernel source code path
make KDIR=/usr/src/kernels/`uname -r`/

# Compile install
make install

# Install drbd moudle
# drbd-8.4.1/drbd/drbd.ko
cd drbd
make clean
make KDIR=/usr/src/kernels/`uname -r`/
cp drbd.ko /lib/modules/`uname -r`/kernel/lib/
depmod

# Drbd daemon process
mkdir -p /usr/local/drbd/var/run/drbd
cp /usr/local/drbd/etc/rc.d/init.d/drbd /etc/rc.d/init.d/
chkconfig --add drbd
chkconfig drbd on

# Configure drbd
# Below is basic sample configure
cd /usr/local/drbd/etc/drbd.d/

# Global: global_common.conf
cp global_common.conf global_common.conf.bak
cat > global_common.conf <<EOF
global {
usage-count yes;
}
common {
net {
protocol C;
}
}
EOF

# Define resource,named as *.res
# Resource name: r0
cat > r0.res<<EOF
resource r0 {
on node8.example.com {
device /dev/drbd1;
disk /dev/vdb1;
address 192.168.0.218:7788;
meta-disk internal;
}
on node9.example.com {
device /dev/drbd1;
disk /dev/vdb1;
address 192.168.0.219:7788;
meta-disk internal;
}
}
EOF

# Load drbd module
# Check drbd module is loaded
# Command: lsmod | grep drbd
modprobe drbd

# Push something to partition
dd if=/dev/zero of=/dev/vdb1 bs=1M count=1

# Create drbd resource
drbdadm create-md r0

# Up drbd resource
drbdadm up r0

# Look drbd status
# Command: cat /proc/drbd
# At first both node are secondary

Part 2

###############################
# Operation only Primary node #
###############################

#Set primary node

drbdadm primary --force r0 # First time
#drbdadm primary r0 # Other time
# Now drbd status: Primary/Secondary
# Time needed to initial /dev/drbd1

# Format drbd partition
# Only operate once!
mkfs.ext4 /dev/drbd1

# Mount drbd partition
mount /dev/drbd1 /drbddata

# Now we can write to /drbddata on Primary node
# For example: touch test.txt
# And drbd will synchro to Secondary node

Part 3

###########################
# Test synchro if success #
###########################

# Attention!
# Only one primary at one time if not two-primarys pattern
# Mount can only on primary node

####### Operation on Primary node #######
# Umount must be done first
umount /dev/drbd1

# Downgrade primary node
drbdadm secondary r0

######## Operation on secondary node #######

# Upgrade secondary node
drbdadm primary r0

# Mount
mount /dev/drbd1 /drbddata

# Use command "ls /drbddata" we can see test.txt


附:DRBD中文应用指南链接
http://blog.csdn.net/liuyunfengheda/article/details/6460814