Showing posts with label Heartbeat. Show all posts
Showing posts with label Heartbeat. Show all posts

Wednesday, September 14, 2011

High Availability Hosting with DRBD


The term high availability is the buzz word nowadays. Nobody tolerates downtime, whether it is the owner of a web site, owner of a server or owner of a data center. The challenge is how you can offer the least down time and the term “high availbility”, has it all.
DRBD refers to “Distributed Replicated Block Device” and is used for building high availability (HA) clusters. This can be attained by mirroring or replicating a block device via network. DRBD can be considered as equivalent to a RAID 1 setup.
In RAID 1, a drive has its data duplicated on two different drives using a RAID controller. Same is the case with DRBD, where the local block holds the data to be replicated and then it is written to another host’s blocks. The only difference here is DRBD allows data replication for more than 2 nodes.
In simple words DRBD is a Linux Kernel module that supports a distributed storage system, by which you can share 2 or more block devices(data or file system).
DRBD works with two or more servers and each of these are denoted as nodes and the node which has the read/write access to data (also called DRBD data) is known as the primary node. The other node to which the data is replicated is referred to as the secondary node. If there are numerous secondary nodes in the high availability cluster, it is referred as a DRBD cluster.
In a nut shell, DRBD takes the data, writes it to the local disk, and sends it to the other nodes. This local disk can either be a physical disk partition, a partition from a volume group, RAID device or any other block device. This block holds the data to be replicated.
DRBD can also be used along with LVM2 Logical Volume Manager.

DRBD Requirements

Setting up OS for DRBD

  • The host names for the DRBD nodes should be unique and correct.
  • Each DRBD node should have a unique IP address.
  • There should be an unused disk or disk partition to store DRBD data to be replicated.
  • The disk or the partition should not be given a file system. The cases c and d are applicable for each DRBD node.
  • These partitions on each node, should be identical in size(most preferred).
  • Make sure that the kernel-devel packages and header files are installed for building kernel modules for DRBD.

Check for the following Packages and Tools

Note: Make sure that you upgrade the kernel to the latest stable version.
  • Kernel header files
  • Kernel source files
  • gcc
  • gcc-c++
  • glib2
  • glib-devel
  • glib2-devel
  • flex
  • bison
  • kernel-smp
  • kernel-smp-devel
  • pkg-config
  • ncurses-devel
  • rpm-devel
  • rpm-build
  • net-snmp-devel
  • lm_sensors-devel
  • perl-DBI
  • python-devel
  • perl-DBI
  • libselinux-devel
  • bzip2-devel

Other Optional Packages

If you need to have a secure communication between the DRBD nodes, you need to install the following packages:
  • openssl
  • openssl-devel
  • gnutls
  • gnutls-devel
  • libgcrypt
  • libgcrypt-devel
Install these via yum or up2date depending on your Linux distro.

Installing DRBD From Source

Since DRBD is a Linux module, it can only be used along with the Linux distros.
  • Download the source from any mirror location or fromhttp://oss.linbit.com/drbd/
  • Follow the instructions in the INSTALL file and choose the method that suits your UNIX flavor.
  • Once you have successfully built DRBD, test loading the DRBD module using insmod and verify using lsmod.
  • If it is successfully loaded remove it using rmmod.
To see the list of DRBD RPM’s for various Linux distros Click Here

Configuring DRBD Primary Node

Configuring DRBD Service

The main configuration file of DRBD is /etc/drbd.conf. This contains the definition of DRBD devices, block size, frequency of updates etc.

Synchronization

Set the synchronization between the two nodes with respect to the network connection speed as DRBD replication is going to take place over the network.
Giga bit Ethernet supports upto 125 MBps synchronization rate and 100Mbps Ethernet upto 12MBps.
In the /etc/drbd.conf set the synchronization rate as follows:
syncer
{
    rate SR;
}
;where SR=synchronization rate

Password Protecting DRBD Data

It is possible to set authentication for DRBD nodes so that only the hosts that has this shared secret joins the DRBD node group.
The format is as follows:
cram-hmac-alg "sha1"
shared-secret "password";

DRBD Configuration File

A basic drbd.conf file should have the following information:
  • device -> path of the logical block device that will be created by DRBD.
  • disk -> the block device that will be used to store the data.
  • address -> the IP address and port number of the host that will hold this DRBD device.
  • meta-disk -> the location where the metadata about the DRBD device will be stored.
    When meta-disk is set as internal, DRBD will use the physical block device to store data.
A sample configuration file is given below:
resource drbd0 {
  protocol=X
  fsck-cmd=fsck.ext2 -p -y

  on drbd-master {
    device=/dev/nbx
    disk=/dev/hdax
    address=x.x.x.x
    port=x
    meta-disk internal;
  }

  on drbd-slave {
    device=/dev/nby
    disk=/dev/hday
    address=y.y.y.y
    port=y
    meta-disk internal;
  }
}

Mounting a DRBD Logical Block

First, we will create a file system for the DRBD block:
# mkfs.ext3 /dev/nbx
Then mount the file system on a mount point:
# mount /dev/drbd0 /mnt/drbd
Now you can copy the files to be replicated to this mount point. Restart drbd service on both master and slave.

Configuring DRBD Secondary Node

The configuration settings on the drbd slave is the same as the master. The only difference here is that there is no need to create a file system on the slave, as the data is transferred from the master.
To get the exact settings you may even copy the drbd.conf file of the master and paste it in the slave node(s). The only things to be changed are the IP, port(if it has a different one), device etc.
Now create metadata on the underlying disk device using drbdadm command:
# drbdadm create-md all

# Restart drbd service.
You can check the /proc/drbd virtual file to check if the master and slave nodes are syncing.
This file shows the status and state of the DRBD nodes.

Status Codes in /proc/drbd File

# cs - connection state

# st - node state (local/remote)

# ld - local data consistency

# ds - data consistency

# ns - network send

# nr - network receive

# dw - disk write

# dr - disk read

# pe - pending (waiting for ack)

# ua - unack'd (still need to send ack)

# al - access log write count

DRBD Protocols

  • Protocol ‘A’ -> A write operation is complete as soon as the data is written to disk and sent to the network.
  • Protocol ‘B’ -> A write operation is complete as soon as a reception acknowledgment arrives.
  • Protocol ‘C’ -> A write operation is complete as soon as a write acknowledgment arrives.
You can use the protocol you need while configuring the DRBD block.The preferred and recommended protocol is C, as it is the only protocol which ensures the consistency of the local and remote physical storage.

Some Useful DRBD Commands

drbdsetup

This is a low level configuration tool of DRBD. This is normally used to configure DRBD blocks.
Let us know see the usage of drbdsetup
For Master:
# drbdsetup /dev/nbx disk /dev/hdcx
# drbdsetup /dev/nbx net
For Slave:
# drbdsetup /dev/nby disk /dev/hdcy
# drbdsetup /dev/nby
# drbdsetup /dev/nby primary

drbdadm

This is an administration tool of DRBD. There are numerous sub-commands for this. Here we will be discussing only the basic commands:
# drbdadm primary all -> sets the state of local device to be primary.
# drbdadm secondary all -> sets the state of local device to be secondary.
If you want to change the state of a resource or resources rather than all, you can specify the resource name:
In our sample configuration file, we had the DRBD resource as drbd0, so to change its state execute:
# drbdadm primary/secondary drbd0
# drbdadm disconnect all -> temporarily disconnects all the DRBD nodes.
# drbdadm connect all -> connects all the DRBD nodes.

Additional Notes

DRBD module can be combined with Linux Heartbeat service for automatic fail-over support.
Heartbeat is the first software which is developed for the Linux-HA project. It can be downloaded from http://www.linux-ha.org/download
Using DRBD, we can replicate any of the web applications like Apache, MySQL, NFS,… etc. All you need to do is to copy the data to be replicated to the DRBD block. I will explain the steps to configure MySQL with DRBD.

Configure MySQL for DRBD

Once the DRBD is set up as above, you can configure MySQL to use the DRBD block to store MySQL data.
Note: If you are going to install MySQL once the DRBD is set, you can configure the data directory of MySQL as the DRBD mount point. Also note that these configurations need to be done in the DRBD primary node.

Migrating an Existing MySQL Installation

To set up DRBD for an existing MySQL you need to follow the steps given below:
As already explained, we have our DRBD block mounted on /mnt/drbd.

Copy the Data Files (Say, MyISAM Files), Binary Log Files and MySQL Configuration File to the DRBD Block

a) Shut down MySQL Administrator.
$ mysqladmin shutdown
b) Copy the databases.
cp -R /var/lib/mysql /mnt/drbd/mysql
c) Copy the MySQL configuration file.
cp /etc/my.cnf /mnt/drbd

Configure my.cnf To Reflect the New Changes

a) Change the data directory.
datadir = /mnt/drbd/mysql
b) If you haven’t enabled binary log before, enable it now. This will be handy in checking the status.
log-bin = mysql-bin
c) Create a symbolic link from /etc/my.cnf to the new configuration file on the DRBD device file-system.
ln -s /mnt/drbd/my.cnf /etc/my.cnf
d) Now restart MySQL and check if the configuration is working.
/etc/init.d/mysql(d) restart
You are done. Now restart the DRBD service and monitor the /proc/drbd file to verify if the data is correctly replicated and synced.

References

Saturday, September 3, 2011

HA on RHEL5




Showing newest posts with label Linux Performance n MonitoringShow older posts

HA on RHEL5

Heartbeat is a High Availabily cluster software in linux platform. Here we will discuss how to
install and configure heartbeat-3.0.3 in redhat enterprise linux. In this example we will configue
a webserver using apache and we will cluster it. It can be implemented on centos, fedora and other redhat flavors.

Heartbeat Version is : heartbeat-3.0.3

Requirements:

2 linux nodes, rhel5.4.
Node1: 192.168.0.33 hb_test1.lap.work
Node2: 192.168.0.34 hb_test2.lap.work
LAN & Internet connection.
A yum server.

Initial Steps:

Set the fully qualified hostnames and give corresponding entries in /etc/hosts and
/etc/network/network.

Configuring Apache:

#yum install httpd*

On node1

#vi /var/www/html/index.html
This is node 1 of Heartbeat HA cluster

On node2
 
#vi /var/www/html/index.html
This is node 2 of Heartbeat HA cluster

On both nodes:

#vi /etc/httpd/conf/httpd.conf
Listen 192.168.0.222:80

Now start the service in both nodes.

#service httpd start                                #it wont work untill heartbeat is started. So dont worry

#chkconfig httpd on
Confirm them from broswer.

Install the following packages in both nodes:

#yum install glibc*
#yum install gcc*
#yum install lib*
#yum install flex*
#yum install net-snmp*
#yum install OpenIPMI*
#yum install python-devel
#yum install perl*
#yum install openhpi*

Save the repo file for clusterlabs online repository in both machines:

Its availabile in http://www.clusterlabs.org/rpm/epel-5/clusterlabs.repo

it is as follows:

[clusterlabs]
name=High Availability/Clustering server technologies (epel-5)
baseurl=http://www.clusterlabs.org/rpm/epel-5
type=rpm-md
gpgcheck=0
enabled=1

[root@hb_test2 ~]# cat /etc/yum.repos.d/clusterlabs.repo
[clusterlabs]
name=High Availability/Clustering server technologies (epel-5)
baseurl=http://www.clusterlabs.org/rpm/epel-5
type=rpm-md
gpgcheck=0
enabled=1

[root@hb_test2 ~]#

After that install heartbeat packages on both nodes:

#yum install cluster-glue*

Four packages will be installed

cluster-glue
cluster-glue-libs
cluster-glue-libs-devel
cluster-glue-debuginfo

#yum install heartbeat*

Five packages will be installed including one dependency

heartbeat.i386 0:3.0.3-2.el5
heartbeat-debuginfo.i386 0:3.0.3-2.el5
heartbeat-devel.i386 0:3.0.3-2.el5
heartbeat-libs.i386 0:3.0.3-2.el5

Dependency:

resource-agents.i386 0:1.0.3-2.el5

#yum install resource-agents*

One package will be installed

resource-agents-debuginfo.i386 0:1.0.3-2.el5

Setting Configuration files:

We can do all configuration in one system and copy the /etc/ha.d to the second node.

#cd /etc/ha.d
#cat README.config

The details about configuration files are explained in this file. We have to copy three
configuration files to this directory from samples in documentation.

[root@hb_test1 ha.d]# cp /usr/share/doc/heartbeat-3.0.3/authkeys /etc/ha.d/
[root@hb_test1 ha.d]# cp /usr/share/doc/heartbeat-3.0.3/ha.cf /etc/ha.d/
[root@hb_test1 ha.d]# cp /usr/share/doc/heartbeat-3.0.3/haresources /etc/ha.d/

We have to edit the authkeys file:

We are using sha1 algorithm:

#vi authkeys
edit as follows
auth 2
#1 crc
2 sha1 test-ha
#3 md5 Hello!

And change the permission of authkeys to 600
#chmod 600 authkeys

We have to edit the ha.cf file:

#vi ha.cf
uncomment following lines and make edits

logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 15
warntime 10
initdead 120
udpport 694
bcast eth0
auto_failback on
node hb_test1.lap.work # in both nodes command #uname -n should
node hb_test2.lap.work # give the these hostnames
We have to edit the haresources file:

#vi haresources

hb_test2.lap.work 192.168.0.222 httpd

NOTE: You dont have to create an interface and set this IP or make a IP alias. Heartbeat
will take care of it. Automaticaly.


Now exchange and save authorized keys between node1 and node2.

Key exchange:

On node1:

Generate the key:

[root@hb_test1 ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
9f:5d:47:6b:2a:2e:c8:3e:ee:8a:c2:28:5c:ad:57:79 root@hb_test1.lap.work

Pass the key to node2:
[root@hb_test1 ~]# scp .ssh/id_dsa.pub hb_test2.lap.work:/root/.ssh/authorized_keys

On node2:

Generate the key:

[root@hb_test2 ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
40:66:t8:bd:ac:bf:68:38:22:60:d8:9f:18:7d:94:21 root@hb_test2.lap.work

Pass the key to node1:
[root@hb_test2 ~]# scp .ssh/id_dsa.pub hb_test1.lap.work:/root/.ssh/authorized_keys

Now copy the /etc/ha.d of node1 to node2:
[root@hb_test1 ~]# scp -r /etc/ha.d hb_test2.lap.work:/etc/

Starting the service:

On both nodes:

#/etc/init.d/heartbeat start

You may have to restart the heartbeat service a few times. Check #ifconfig in one node you can
see an interface eth0:1 is up with IP 192.168.0.222. In that node httpd is running and in the other
node it is stopped. When the running node fails, the other one will start.

Saturday, August 14, 2010

Configuring High Availability Linux Cluster


This document shows how you can set up a two node, high-availability HTTP cluster with heartbeat on linux. Both nodes use the Apache web server to serve the same content.
Pre-Configuration Requirements:
1. Assign hostname host01 to primary node with IP address 192.168.0.1 to eth0.
2. Assign hostname host02 to secondry node with IP address 192.168.0.2.
Note: on host01
# uname -n
host01
On host02
uname -n
host02
192.160.2.1 is the virtual IP address that will be used for our Apache webserver (i.e., Apache will listen on that address).

Configuration:

1. Download and install the heartbeat package. In our case we are using linux so we will install heartbeat with yum:
yum install heartbeat
or download these packages:
heartbeat-2.08
heartbeat-pils-2.08
heartbeat-stonith-2.08
2. Now we have to configure heartbeat on our two node cluster. We will deal with three files. These are:
authkeys
ha.cf
haresources
3. Now moving to our configuration. But there is one more thing to do, that is to copy these files to the /etc/ha.d directory. In our case we copy these files as given below:

cp /usr/share/doc/heartbeat-2.1.2/authkeys /etc/ha.d/
cp /usr/share/doc/heartbeat-2.1.2/ha.cf /etc/ha.d/
cp /usr/share/doc/heartbeat-2.1.2/haresources /etc/ha.d/
4. Now let's start configuring heartbeat. First we will deal with the authkeys file, we will use authentication method 2 (sha1). For this we will make changes in the authkeys file as below.
vi /etc/ha.d/authkeys
Then add the following lines:
auth 2
2 sha1 test-ha
Change the permission of the authkeys file:
# chmod 600 /etc/ha.d/authkeys
5. Moving to our second file (ha.cf) which is the most important. So edit the ha.cf file with vi:
vi /etc/ha.d/ha.cf
Configuring Heartbeat High Availability Cluster On linux
Add the following lines in the ha.cf file:
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
initdead 120
bcast eth0
udpport 694
auto_failback on
node host01
node host02
Note: host01 and host02 is the output generated by
# uname -n
6. The final piece of work in our configuration is to edit the haresources file. This file contains the information about resources which we want to highly enable. In our case we want the webserver (httpd) highly available:
# vi /etc/ha.d/haresources
Add the following line:
host01 192.160.2.1 httpd
7. Copy the /etc/ha.d/ directory from host01 to host02:
# scp -r /etc/ha.d/ root@host02:/etc/
8. As we want httpd highly enabled let's start configuring httpd:
# vi /etc/httpd/conf/httpd.conf
Add this line in httpd.conf:
Listen 192.160.2.1:80
9. Copy the /etc/httpd/conf/httpd.conf file to host02:
# scp /etc/httpd/conf/httpd.conf root@host02:/etc/httpd/conf/
10. Create the file index.html on both nodes (host01 & host02):
On host01:
echo "host01 apache test server" > /var/www/html/index.html
On host02:
echo "host02 apache test server" > /var/www/html/index.html
11. Now start heartbeat on the primary host01 and secondary host02:
/etc/init.d/heartbeat start
12. Open web-browser and type in the URL:
It will show host01 apache test server.
13. Now stop the heartbeat daemon on host01:
# /etc/init.d/heartbeat stop
In your browser type in the URL http://192.160.2.1 and press enter.
It will show host02 apache test server.
14. We don't need to create a virtual network interface and assign an IP address (192.160.2.1) to it. Heartbeat will create this and start the service (httpd) itself.
Don't use the IP addresses 192.168.0.1 and 192.168.0.2 for services. These addresses are used by heartbeat for communication between host01 and host02. When any of them will be used for services/resources, it will disturb heartbeat and will not work. Be carefull!!!