Configure DRBD in Linux

Source: Internet
Author: User
1. working principle of Drbd: DRBD is a block device that can be used in high availability (HA) and its class & amp; 20284; in a network RAID-1 function (you can compare NFS in Linux) when you write data to a local file system, the data will be sent to another host on the network with the same I. working principle of Drbd
DRBD is a block device that can be used in HA. it is similar to a network RAID-1 function. (compare NFS in Linux) when you write data to a local file system, the data will be sent to another host on the network. record in the same format in a file system. Data on the local (master node) and remote (Slave node) can be synchronized in real time. when the local system fails, the same data is retained on the remote host. the DRBD function can be used in high availability (HA) instead of a shared disk array. because the data exists on both the local host and remote host, when switching, the remote host can continue to provide services as long as it uses the backup data above it.

II. environment configuration:
1. Operating system: radhat 5.4
2. master server:
Host Name: master
IP: 192.168.2.124
Image hard disk:/dev/hdb
3. backup server
Host Name: slave
IP: 192.168.2.125
Image hard disk:/dev/hdb

3. install drbd
1. download the source code: http://oss.linbit.com/drbd /. The version installed this time is drbd-8.3.5
2. extract the downloaded drbd-8.3.5.tar.gz file
[Root @ master] # tar? Zxvf drbd-8.3.5.tar.gz
[Root @ master] # drbd-8.3.5 cd
[Root @ master] # make
[Root @ master] # make install
[Root @ master] # modprobe drbd @ load and install the drbd module
[Root @ master] # lsmod | grep drbd
Drbd 271736 0
@ Check whether the installation is successful through lsmod. if similar content is output, the drbd installation is successful.
[Root @ master] # mknod/dev/drbd0 B 147 0 @ create a hardware device drbd

4. configure drbd
When DRBD is running, a configuration file/etc/drbd is read. conf. this file describes the ing between the DRBD device and the hard disk partition. Once the data is written to the disk and sent to the network, the write operation is completed.
1. configuration parameters of drbd. conf
Protocol
Protocol A @ the write operation is considered complete once data is written to the disk and sent to the network
Protocol B @ the write operation is completed after receiving the confirmation.
Protocol C @ after receiving the write confirmation, the write operation is considered complete.
2. global
Global {usage-count yes;} @ whether to participate in DRBD user statistics. the default value is yes.
3. common
Common {syncer {rate 1 M ;}}
@ Sets the maximum network rate for master-slave node synchronization, in bytes.
4. resource
A DRBD device (/dev/drbdX) is called a "resource ". It contains the ip address of the master and slave nodes of a DRBD device, the name of the underlying storage device, the size of the device, the storage method of meta information, and the name of the device provided by drbd.
Resource r0 {
Protocol C; @ use protocol C. after receiving the write confirmation from the remote host, the write is considered complete.
Net {
Cram-hmac-alg sha1; @ sets the information algorithm used for communication between the master and slave hosts.
Shared-secret "FooFunFactory ";
}
@ The description of each host starts with "on", followed by the host name. the host configuration is in.
On master {
Device/dev/drbd0;
Disk/dev/hdb;
Address 192.168.2.124: 7898; @ set the listening port of DRBD for communication with another host
Meta-disk internal;
}
On slave {
Device/dev/drbd0;
Disk/dev/hdb;
Address 192.168.2.124: 7898;
Meta-disk internal;
}
}
5. Complete configuration during the test is as follows:
Global {
Usage-count yes;
}
Common {
Protocol C;
}
Resource r0 {
On master {
Device/dev/drbd0;
Disk/dev/hdb;
Address 192.168.2.124: 7789;
Meta-disk internal;
}
On slave {
Device/dev/drbd0;
Disk/dev/hdb;
Address 192.168.2.125: 7789;
Meta-disk internal;
}
}

6. the configuration files of the two nodes are identical. The DRBD source code directory contains a sample, which is described in detail.

5. start drbd
1. create matadata
Before starting DRBD, you need to create a data block for the DRBD record information on the hdb partition of the two hosts. execute the following on the two hosts:
[Root @ master] # drbdadm create-md r0
[Root @ slave] # drbdadm create-md r0
Note:
1) "r0" is the resource name defined in drbd. conf.
2) when the command "drbdadm create-md r0" is executed, the following error message is displayed.
Device size wocould be truncated, which
Wocould Upload UPT data and result in
'Access beyond end of device' errors.
You need to either
* Use external meta data (recommended)
* Shrink that filesystem first
* Zero out of the device (destroy the filesystem)
Operation refused.
Command 'drbdmeta 0 v08/dev/xvdb internal create-MD' terminated with exit code 40
Drbdadm create-md r0: exited with code 40
Solution: initialize the disk file format. dd if =/dev/zero bs = 1 M count = 1 of =/dev/sdXYZ; sync
[Root @ master] # dd if =/dev/zero bs = 1 M count = 1 of =/dev/hda3; sync
2. start DRBD and run it on both hosts.
[Root @ master] #/etc/init. d/drbd start
[Root @ slave] #/etc/init. d/drbd start
Note:
If node cannot be found during startup, check whether/dev/hda3 is in the Mount status. If yes, run the umount command to cancel the mounting.
3. view the status of DRBD, which is executed on both hosts.
[Root @ slave drbd] # cat/proc/drbd

Version: 8.3.10 (api: 88/proto: 86-96)
GIT-hash: 5c0b0469666682443d4785d90a2c603366f9017b build by phil @ fat-tyre, 12:17:35

1: cs: Connected ro: Secondary/Secondary ds: Inconsistent/Inconsistent C r -----
Ns: 0 nr: 0 dw: 0 dr: 0 al: 0 bm: 0 lo: 0 pe: 0 ua: 0 ap: 0 ep: 1 wo: B oos: 2096028

The output description is as follows:
Ro indicates the role Information. when drbd is started for the first time, both drbd nodes are in the Secondary status by default,
Ds indicates the disk status information. "Inconsistent/Inconsisten" indicates that the disk data of the two nodes is Inconsistent.
Ns indicates the packet information sent by the network.
Dw is the disk write information
Dr indicates the disk read information.
4. set the master node
Because there are no primary and secondary nodes by default, you need to set the primary and secondary nodes of the two hosts, select the host that needs to be set as the primary node, and then execute the following command:
[Root @ master] # drbdsetup/dev/drbd0 primary? O
Or execute the following command.
[Root @ master] # drbdadm -- overwrite-data-of-peer primary all
After executing this command for the first time, you can use another command if you need to set which is the master node:

[Root @ master] #/sbin/drbdadm primary r0 or/sbin/drbdadm primary all
View the drbd status again,
[Root @ master] # cat/proc/drbd
Version: 8.3.10 (api: 88/proto: 86-96)
GIT-hash: 5c0b0469666682443d4785d90a2c603366f9017b build by phil @ fat-tyre, 12:17:35

1: cs: SyncSource ro: Primary/Secondary ds: UpToDate/Inconsistent C r -----
Ns: 2432 nr: 0 dw: 0 dr: 3104 al: 0 bm: 0 lo: 0 pe: 0 ua: 0 ap: 0 ep: 1 wo: B oos: 2093596
[>...] Sync 'Ed: 0.2% (2093596/2096028) K
Finish: 0:12:27 speed: 2,432 (2,432) K/sec


We can see that the current status is primary/secondary, and the disk information is upTodat/Inconsistent (real-time/Inconsistent), and the data on the disks of the two machines has been synchronized.
After a while, we will check the drbd status again, as shown below:
[Root @ master] # cat/proc/drbd
Version: 8.3.10 (api: 88/proto: 86-96)
GIT-hash: 5c0b0469666682443d4785d90a2c603366f9017b build by phil @ fat-tyre, 12:17:35

1: cs: Connected ro: Primary/Secondary ds: UpToDate/UpToDate C r -----
Ns: 2096028 nr: 0 dw: 0 dr: 2096700 al: 0 bm: 128 lo: 0 pe: 0 ua: 0 ap: 0 ep: 1 wo: B oos: 0

In this case, the status information is primary/secondary, and the disk information is UpToDate/UpToDate, indicating that the synchronization has been completed.

6. test synchronization
Mount the DRBD device on the host to a directory for use. The DRBD device of the standby machine cannot be mounted because it is used to receive host data and is operated by DRBD.
There are two ways to switch between the master and slave nodes: stop the drbd service switch and switch normally:
1. stop switching the drbd service
Disable the master node service. The drbd partition is automatically detached from the master node, and the switchover command is executed on the Slave node:
[Root @ drbd2 ~] # Drbdadm primary all
An error is reported:
2: State change failed: (-7) Refusing to be Primary while peer is not outdated
Command 'drbdsetup 2 primary' terminated with exit code 11
Therefore, the following command must be executed on the Slave node:
[Root @ drbd2 ~] # Drbdsetup/dev/drbd0 primary? O
Or
[Root @ drbd2 ~] # Drbdadm -- overwrite-data-of-peer primary all
In this case, the switchover is normal.
After the slave Node executes the switch to master node command, the original master node automatically becomes a slave node. You do not need to run the command to switch to the Slave node again on the master node.
2. normal switch
Unmount the disk partition on the master node, and then execute
[Root @ drbd1 ~] # Drbdadm secondary all
If you do not execute this command and directly execute the command to switch to the master node on the Slave node, an error is returned:
2: State change failed: (-1) Multiple primaries not allowed by config
Command 'drbdsetup 2 primary' terminated with exit code 11
Then, run
[Root @ drbd2 ~] # Drbdadm primary all
Finally, mount the disk partition on the Slave node:
[Root @ drbd2 ~] # Mount/dev/drbd0/mnt
3. Notes
1) before you mount a drbd device, you must switch the device to the primary status.
2) only one of the two nodes is in primary state and the other is in secondary state at the same time.
3) the drbd device cannot be loaded on a server in the secondary state.
4) The two partitions synchronized by the master and slave servers must have the same size, so that disk space is not wasted, because the drbd disk image is equivalent to the network raid 1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.