Thursday, April 23, 2009

High Availability-Installing DRBD and Hearbeat in Ubuntu

TS93-55 - 192.168.1.93 10.0.0.55
TS94-56 - 192.168.1.94 10.0.0.56

Virtual Ip - 192.168.1.95

Each machine has 2 network cards: one for public and one for private traffic. The public interfaces connect to my main switch on the 192.168.1.X network. The private interfaces are connected via a crossover cable on the 10.0.0.X network.

Once you have installed Ubuntu 8.04 Server, you need to install some build tools.

apt-get install build-essential binutils cpp gcc autoconf automake1.9 libtool \
autotools-dev g++ make flex

In theory, that should get all of the development tools installed that you'll need.

Next we need to get the entire kernel source code. Just the kernel headers won't cut it. We need to compile the kernel so that it builds some of the scripts needed to compile the DRBD driver. We'll also install the ncurses library so that menuconfig works.

apt-get install libncurses5-dev linux-source-2.6.24

Then extract the kernel source:

cd /usr/src
tar -xvf linux-source-2.6.24.tar.bz2
cd /usr/src/linux-source-2.6.24

Next, lets clean up any unneeded files (which there shouldn't be any the first time):

make mrproper

Before you can build the kernel, you need to copy your existing kernel build configuration into the kernel source directory:

cp /boot/config-2.6.24-16-server /usr/src/linux-source-2.6.24/.config

Now we run the menuconfig which will read in our kernel build configuration and build some version files. As soon as the GUI appears, just exit immediately. You don't have to change any of the settings.

make menuconfig

Finally we need to prepare the kernel and compile it. This will take quite some time.

make prepare
make

Now that we have the kernel source compiled and ready to go, let's get the DRBD source.

cd /root
wget http://oss.linbit.com/drbd/8.2/drbd-8.2.5.tar.gz
tar -xvf drbd-8.2.5.tar.gz
cd /root/drbd-8.2.5

We need to build the DRBD driver and specify the path to the kernel source, then install the driver in the /lib path:

make KDIR=/usr/src/linux-source-2.6.24
make install

Once the driver is compiled, we need to move/copy it to the appropriate lib directory:

mv /lib/modules/2.6.24.3/kernel/drivers/block/drbd.ko \
/lib/modules/2.6.24-16-server/kernel/drivers/block

Next we need to start the driver and tell Linux to load it the next time it boots:

modprobe drbd
echo 'drbd' >> /etc/modules
update-rc.d drbd defaults

Now that everything is installed, verify the driver is loaded:

lsmod | grep drbd

It might be a good idea to reboot and make sure it loads.

At this point, you should set up the /etc/drbd.conf, which mine looks like this:

global {
usage-count no;
}

common {
protocol C;

syncer {
rate 30M;
al-extents 1801;
}

startup {
wfc-timeout 0;
degr-wfc-timeout 15;
}

disk {

on-io-error detach;
# fencing resource-and-stonith;
}

net {
sndbuf-size 512k;
timeout 60; # 6 seconds (unit = 0.1 seconds)
connect-int 10; # 10 seconds (unit = 1 second)
ping-int 10; # 10 seconds (unit = 1 second)
ping-timeout 5; # 500 ms (unit = 0.1 seconds)
max-buffers 8000;
max-epoch-size 8000;
cram-hmac-alg "sha1";
shared-secret "secret";
}
}

resource r0 {
on TS93-55 {
disk /dev/mapper/ts-ts--ts1-1;
address 10.0.0.55:7788;
device /dev/drbd0;
meta-disk internal;
}

on TS94-56 {
disk /dev/mapper/ts-ts2-1;
address 10.0.0.56:7788;
device /dev/drbd0;
meta-disk internal;
}
}


With the configuration set, you need to restart/reload DRBD, create the meta disk, and bring the drive up:

/etc/init.d/drbd restart
drbdadm create-md r0
drbdadm up r0


* If any error like the following one occurs:

TS93-55 # drbdadm create-md r0
md_offset 1069215744
al_offset 1069182976
bm_offset 1069150208

Found ext3 filesystem which uses 1044160 kB
current configuration leaves usable 1044092 kB

Device size would be truncated, which
would corrupt 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 the device (destroy the filesystem)
Operation refused.

Command 'drbdmeta /dev/drbd0 v08 /dev/sdb5 internal create-md'
terminated with exit code 40
drbdadm aborting

try

dd if=/dev/zero of=/dev/sda5 bs=1M count=128

* And then you can successfully create your resource on both machines:

[paras1]# drbdadm create-md r0


Wait the end of the synchronization.

At this point you can view the DRBD status:

TS93-55:~$ cat /proc/drbd
version: 8.0.11 (api:86/proto:86)
GIT-hash: b3fe2bdfd3b9f7c2f923186883eb9e2a0d3a5b1b build by phil@mescal, 2008-02-12
0: cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate C r---
ns:221871972 nr:7160 dw:3856764 dr:227396211 al:763 bm:17504 lo:0 pe:0 ua:0 ap:0
resync: used:0/31 hits:13841287 misses:13628 starving:0 dirty:0 changed:13628
act_log: used:0/1801 hits:961638 misses:790 starving:0 dirty:27 changed:763

chris@app1:~$ /etc/init.d/drbd status
drbd driver loaded OK; device status:
version: 8.0.11 (api:86/proto:86)
GIT-hash: b3fe2bdfd3b9f7c2f923186883eb9e2a0d3a5b1b build by phil@mescal, 2008-02-12
m:res cs st ds p mounted fstype
0:r0 Connected Primary/Secondary UpToDate/UpToDate C

* Now it’s time to create your EXT3 filesystem on the DRBD device! (only in the case there was no existing data on /dev/sdb5)

[TS93-55]# mke2fs -j /dev/drbd0

You're now ready to mount /dev/drbd0 and put data on it. There's still other things you need to do if you plan on using Heartbeat for failover monitoring.

Step1:

sudo apt-get install heartbeat



Step 2.
Now we’re going to configure the /etc/ha.d/ha.cf file for our machine. Here is what I’ve put into the /etc/ha.d/ha.cf file ON EACH MACHINE:

vim /etc/ha.d/ha.cf

logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
initdead 120
bcast eth0
udpport 694
auto_failback on
node TS93-55
node TS94-56

Step 3.
Now configure the /etc/ha.d/authkeys file ON EACH MACHINE for what kind of security and file checking you want, I don’t care about security in this example so I put this is the file since it’s the fastest:
auth 2
2 crc


Step 4:

vim /etc/ha.d/hareresource

TS93-55 drbddisk::r0 Filesystem::/dev/drbd0::/pgsql/data::ext3 192.168.1.95 postgresql-8.2 apache2

Step 5.
Make sure heartbeat and the service(s) you’re watching DO NOT start at boot, otherwise things get really ugly if when you screw up:
chkconfig heartbeat off
chkconfig httpd off
/etc/init.d/httpd stop
(on both machines)


Step 6.
Alright, it’s finally time to test your failover configuration. First, we need to start heartbeat on the primary machine:
TS93-55# /etc/init.d/heartbeat start
Then, start it on the secondary machine
TS94-56# /etc/init.d/heartbeat start

You should now be able to ping the cluster IP (192.168.1.95). You can also check that the /dev/drbd0 filesystem is mounted on the primary node using df. Check the /var/log/messages file on either machine for debugging information



For more informations follow the links below
http://gcharriere.com/blog/?p=1

http://www.cb1inc.com/2008/05/18/installing-drbd-8.2.5-on-ubuntu-8.04-hardy-heron

http://writequit.org/blog/?cat=61

Thursday, December 4, 2008

Import data from CSV or Excel in to database

Simple Way To Import CSV/Excel data.

If u have excel file then save it as CSV file.
Consider a table foo and it has two columns (name,status)
Put ur CSV file in config folder
Create a migration file.. Put the below code in ur self.up method

def self.up
filename = "#{RAILS_ROOT}/config/Datafile.csv"
file=File.new(filename,"r")
while (line = file.gets)
c = Foo.new
columns = line.split(",")
c.name=columns[1]
c.status=columns[2]
c.save
end

Run the migration using rake command 'rake db:migrate --trace'
and thats it..!! You Got Ur Table Loaded With Data u Need..!!

Cheers..!!

Wednesday, December 3, 2008

How to Improve Performace in Rails Application

1.Do not use dynamic finders like find_by_name.. find_by_columnname..
This will internally call a method named 'method_missing' in activerecord then it will construct a query and execute it.

2.Do not use rails helpers like link_to

3.Avoid associations

4.Avoid too many before_filters

5.While writing query for eg. Model.find(:all)
try to use select in this so that u limit only the required rows from the database

6.Move your javascript and css to the bottom of ur page..
<%=javascript_include_tag "prototype"%>

7.If u r using n-1 relation then it can be optimized thru piggy back technique http://railsexpress.de/blog/articles/2005/11/06/the-case-for-piggy-backed-attributes

8.Also do indexing in your database.

What is Indexing and why Indexing?
In a database, you have a few different kinds of objects. You’ve got tables, which are just big containers in which you put your data. The data is not sorted, so if you want to find something in a table, you need to look through everything until you find what you want.

You also have indexes. An index is a sorted list of rows in a table. It contains a small subset of the table’s data, just enough to sort the data and point to the real data rows in the real table. So, when you want to find something in a table, you look at a suitable index, not the table. The index will point to the rows of the table that match what you are looking for.


9.Monitor ur rails app performance using tools like RPM,Production Log Analyzer (http://rails-analyzer.rubyforge.org/pl_analyze/),clientperf(http://austinentrepreneur.wordpress.com/2008/06/21/announcing-clientperf-simple-client-side-rails-performance/)

10.Also check for any memory leak in ur application since ruby is prone to it.