Xây dựng mô hình High Availability Mysql Cluster – DRBD

Xây dựng mô hình High Availability Mysql Cluster – DRBD

Mô hình:

Phần 1: Cài đặt và cấu hình heartbeat

Chuẩn bị

– Mỗi máy gắn 1 harddisk 1G ( đồng bộ database)

– Cấu hình địa chỉ IP

node1.ha-mysql01.com [eth0:192.168.1.11] [eth1:172.16.1.1]
node2.ha-mysql02.com [eth0:192.168.1.12] [eth1:172.16.1.2]

– Trong file /etc/hosts , bổ sung thêm tên hostname và địa chỉ IP để hai nút có thể giao tiếp được với nhau.

192.168.1.11 node1.ha-mysql01.com 
192.168.1.12 node2.ha-mysql02.com

B1: Cài các gói heartbeat package

# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# yum install heartbeat

B2:Cấu hình heartbeat (thực hiện trên 2 node)

Chép các file ha.cf, haresources, authkeys vào thư mục /etc/ha.d

# cp /usr/share/doc/heartbeat-2.1.3/authkeys /etc/ha.d/
# cp /usr/share/doc/heartbeat-2.1.3/ha.cf /etc/ha.d/
# cp /usr/share/doc/heartbeat-2.1.3/haresources /etc/ha.d/
# vi /etc/ha.d/authkeys

Thêm 2 dòng sau:

 auth 1             ; use authentication method 2 (sha1)
 1 sha1 test-ha
# chmod 600 /etc/ha.d/authkeys
#  vi /etc/ha.d/ha.cf

Thêm các dòng sau vào cuối file

            ldebugfile /var/log/ha-debug ;(co the bo qua)
            logfile /var/log/ha-log
            logfacility local0     
            keepalive 2               
            deadtime 30                          
            initdead 120                         
            bcast eth1                            
            udpport 694                          
            auto_failback on                 
            node node1.ha-mysql01.com
            node node2.ha-mysql02.com
#  vi /etc/ha.d/haresources
   node1.ha-mysql01.com IPaddr::192.168.1.13/24/eth0 drbddisk::disk1 Filesystem::/dev/drbd0::/var/lib/mysql::ext3::defaults mysqld

Ý nghĩa: tên nút chính, địa chỉ IP ảo (cluster IP) và tài nguyên dùng để bắt đầu.

Phần 2: Cài đặt và cấu hình drbd (trên 2 node)

 B1: Trên 2 node cài đặt drbd

Cài các gói trên 2 node

# yum install mysql mysql-server -y 
# rpm -Uvh http://www.elrepo.org/elrepo-release-6-8.el6.elrepo.noarch.rpm
# yum install -y kmod-drbd83 drbd83-utils

B2: Tổ chức phân khu đĩa

 # fdisk /dev/sdb

Tạo logical volume (1gb dùng chứa database đồng bộ giữa 2 node)

chọn  /dev/sdb5

B3: Cấu hình drbd

Create the Distributed Replicated Block Device resource file (/etc/drbd.d/disk1.res)

– Quan sát file cấu hình:

 # cat  /etc/drbd.conf

include “drbd.d/global_common.conf”;

include “drbd.d/*.res”;

– Cấu hình drbd:

# vi /etc/drbd.d/disk1.res
resource disk1

{
startup {
wfc-timeout 30;
outdated-wfc-timeout 20;
degr-wfc-timeout 30;
}
net {
cram-hmac-alg sha1;
shared-secret sync_disk;
}
syncer {
rate 100M;
verify-alg sha1;
}
on node1.ha-mysql01 .com {
device /dev/drbd0;
disk /dev/sdb5;
address 192.168.1.11:7789;
meta-disk internal;
}
on node2.ha-mysql02.com {
device /dev/drbd0;
disk /dev/sdb5;
address 192.168.1.12:7789;
meta-disk internal;
}
}

-Nạp module drbd vào kernel

# modprobe drbd minor_count=1

– Kiểm tra module drbd có được nạp:

#  lsmod |grep drbd

Chú ý: 2 máy đồng bộ thời gian

Khởi tạo DRBD metadata trên cả 2 node

 # drbdadm create-md disk1

Trên 2 node bây giờ đã có DRBD metadata

# chkconfig heartbeat on
# reboot

reboot 2 máy, dịch vụ: drbdmysqld sẽ tự được khởi động

[root@node1 ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37
m:res    cs         ro                   ds                         p  mounted  fstype
0:disk1  Connected  Secondary/Secondary  Inconsistent/Inconsistent  C

Xem thông tin bằng lệnh

 # drbd-overview
            kq: 0:mysql  Connected Secondary/Secondary Inconsistent/Inconsistent C r---

Quan sát sẽ thấy cả 2 node là secondary, chạy lệnh sau trên node1 để gán node này là master: 

drbdadm -- --overwrite-data-of-peer primary disk1

Trên cả 2 node, kiểm tra lạ:

# drbd-overview

Tại node1:

[root@node1 ~]# drbd-overview

  0:disk1  SyncSource Primary/Secondary UpToDate/Inconsistent C r-----

        [======>.............] sync'ed: 38.1% (650876/1044092)K

Đợi quá trình đồng bộ thành công

Tại node2:

[root@node2 ~]# drbd-overview
  0:disk1  Connected Secondary/Primary UpToDate/UpToDate C r-----

Định dạng và mount (chỉ làm tại node1)

# mkfs.ext3 /dev/drbd0

# mount /dev/drbd0 /var/lib/mysql/ ; ko phải tạo mount trong /etc/fstab,
# mkfs.ext3 /dev/drbd0
# mount /dev/drbd0 /var/lib/mysql/

Mounted point sẽ tự động dược tạo

# df -h /var/lib/mysql
# df -h /var/lib/mysql
# drbd-overview
  Cấu hình mysql
 # vi /etc/my.cnf
datadir=/var/lib/mysql/                   ; chỉ đến nơi chứ dữ liệu, mặc định ko cần thay đổi
# service heartbeat restart
node1: ifconfig | more
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:02:C5:9B 
          inet addr:192.168.1.13  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Base address:0x2000
# service mysqld status
mysqld (pid 5520) is running...
[root@node1 ha.d]#

Mysqld được gọi tự động bởi heartbeat

Đặt password cho tài khoản root của mysql:

# mysqladmin -u root password '123456'

Tạo database: nhansu

HighAvailabilityMysqlClusterDRBD

# service heartbeat stop

node2:

# ifconfig | more          ; quan sát thấy ip 192.168.1.13
# service mysqld status

mysqld (pid 5330) is running…

#  mysql -p

Thấy database nhansu

HighAvailabilityMysqlClusterDRBD

Như vậy, qua bài lab này mình đã hướng dẫn các bạn xây dựng được hệ thống HA dùng mysql làm cơ sở dữ  liệu trên thực tế có thể dùng ứng dụng khác như httpd,tomcat,.. khi có một node bị chết hệ thống sẽ chuyển sang node còn lại sẽ đảm bảm hệ thống được xuyên suốt không ảnh hưởng đến dịch vụ. Mọi người có ý kiến chia sẻ vui lòng để lại bình luận phía dưới mình sẽ phản hồi. Cảm ơn các bạn đã xem bài viết!

Chúc các bạn thành công!

Bài viết liên quan

Leave a Reply

Your email address will not be published. Required fields are marked *