Triển khai hệ thống lưu trữ đơn giản với DRDB

Mục tiêu, cần 1 server NFS để lưu trữ dữ liệu (chủ yếu là dữ liệu ảnh từ web upload lên)
Công nghệ lựa chọn là DRBD 2 node, export ra ngoài cho user bằng NFS, Failover giữa primary node và secondary node bằng keepalived

Bước 1: Cấu hình DRBD
– Cài đặt 2 máy Ubuntu 20.04:

10.144.136.41 mefin-ntl-drbd-01

10.144.136.42 mefin-ntl-drbd-02



Mỗi máy 2 ổ cứng:

- 1 ổ /dev/sda 40GB cài OS

- 1 ổ /dev/sdb 500GB để làm file server. Ổ này add vào thôi, sẽ format các thứ ở bước sau

– Thiết lập hostname cho 2 máy:

# cat /etc/hosts

127.0.0.1 localhost

10.144.136.41 mefin-ntl-drbd-01

10.144.136.42 mefin-ntl-drbd-02

# The following lines are desirable for IPv6 capable hosts

::1     ip6-localhost ip6-loopback

fe00::0 ip6-localnet

ff00::0 ip6-mcastprefix

ff02::1 ip6-allnodes

ff02::2 ip6-allrouters

– Format ổ /dev/sdb, làm trên cả 2 máy

root@mefin-ntl-nfs01:~# fdisk /dev/sdb

Command (m for help): n

Partition type

   p   primary (0 primary, 0 extended, 4 free)

   e   extended (container for logical partitions)

Select (default p): p

Partition number (1-4, default 1): 1

First sector (2048-1048575999, default 2048): 

Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1048575999, default 1048575999): 



Created a new partition 1 of type 'Linux' and of size 500 GiB.



Command (m for help): t

Selected partition 1

Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'.



Command (m for help): w

The partition table has been altered.

Calling ioctl() to re-read partition table.

– Tạo LVM, trên cả 2 máy

pvcreate /dev/sdb1

vgcreate storevolume /dev/sdb1

vgdisplay

lvcreate -n storevolumelogic -l 100%FREE storevolume

– Cài đặt DRBD, thao tác trên cả 2 máy

apt-get install -y drbd-utils

rm -rf /etc/drbd.d/*

– Tạo resource cho DRBD, thao tác trên cả 2 máy, tạo file cấu hình

cat >/etc/drbd.d/global_common.conf <<EOL

resource nfs-fintech {

        protocol C;

#        handlers {

#                pri-on-incon-degr "echo o > /proc/sysrq-trigger ; halt -f";

#                pri-lost-after-sb "echo o > /proc/sysrq-trigger ; halt -f";

#                local-io-error "echo o > /proc/sysrq-trigger ; halt -f";

#                outdate-peer "/usr/lib/heartbeat/drbd-peer-outdater -t 5";      

#        }

#        startup {

#                degr-wfc-timeout 2;

#                become-primary-on mefin-ntl-nfs01;

#        }

disk {

        on-io-error             detach;

        no-disk-flushes ;

        no-disk-barrier;

        c-plan-ahead 0;

        c-fill-target 5M;

        c-min-rate 2400M;

        c-max-rate 3600M;

} 

net {

        # max-epoch-size          20000;

        max-buffers             36k;

        sndbuf-size            9072k ;

        rcvbuf-size            9072k;

}



        syncer {

                rate 4096M;

                verify-alg sha1;

                al-extents 257;

                c-fill-target 24M;

                c-min-rate 600M;

                c-max-rate 720M;

        }



        on mefin-ntl-drbd-01 {

                device  /dev/drbd0;

                disk    /dev/mapper/storevolume-storevolumelogic;

                address 10.144.136.41:7788;

                meta-disk internal;

        }



        on mefin-ntl-drbd-02 {

                device  /dev/drbd0;

                disk    /dev/mapper/storevolume-storevolumelogic;

                address 10.144.136.42:7788;

                meta-disk internal;

        }

}

EOL

– Khởi tạo metadata cho DRBD theo file cấu hình, thao tác trên cả 2 máy

#drbdadm create-md nfs-fintech

WARN:

  You are using the 'drbd-peer-outdater' as fence-peer program.

  If you use that mechanism the dopd heartbeat plugin program needs

  to be able to call drbdsetup and drbdmeta with root privileges.



  You need to fix this with these commands:

  dpkg-statoverride --add --update root haclient 4750 /lib/drbd/drbdsetup-84



initializing activity log

initializing bitmap (16000 KB) to all zero

Writing meta data...

New drbd meta data block successfully created.

– Bật DRBD trên cả 2 máy

systemctl start drbd

– Thao tác trên node 1, tiến hành chuyển node1 làm node primary

drbdadm primary nfs-fintech --force

Sau bước này 2 máy sẽ tiến hành đồng bộ ban đầu với nhau, tiến hành kiểm tra trạng thái của cluster và quá trình đồng bộ như sau:

root@mefin-ntl-nfs01:~# cat /proc/drbd 	

version: 8.4.11 (api:1/proto:86-101)	

srcversion: FC3433D849E3B88C1E7B55C 	

 0: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r-----	

    ns:92560384 nr:0 dw:0 dr:92562504 al:8 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:431707484	

	[==>.................] sync'ed: 17.7% (421588/511980)M

	finish: 2:54:07 speed: 41,316 (38,940) K/sec

Đợi cho quá trình đồng bộ hoàn tất, trạng thái cả 2 node là UpToDate/UpToDate

root@mefin-ntl-drbd-02:~# cat /proc/drbd 

version: 8.4.11 (api:1/proto:86-101)

srcversion: FC3433D849E3B88C1E7B55C 

 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----

    ns:277046300 nr:7370548 dw:284415448 dr:131389 al:72035 bm:0 lo:4 pe:4 ua:0 ap:4 ep:1 wo:d oos:0

root@mefin-ntl-drbd-02:~# 

Tiến hành format ổ drbd và mount vào máy chủ, việc này thực hiện trên node primary

mkfs.ext4 /dev/drbd0

mount /dev/drbd0 /srv

Mount thành công là OK, xong phần dựng DRBD, tiền hành umount trước khi thực hiện bước tiếp theo.

Tiến hành cài đặt nfs server và keepalived cho cả 2 máy

apt install -y nfs-server keepalived

systemctl enable keepalived

Cấu hình cho nfs mount vào /srv
Tuy nhiên không để cho nfs-server khởi động cùng máy chủ. Để keepalived làm việc đó.
File export của NFS trên cả 2 máy

root@mefin-ntl-drbd-01:~# cat /etc/exports 

# /etc/exports: the access control list for filesystems which may be exported

#		to NFS clients.  See exports(5).

#

# Example for NFSv2 and NFSv3:

# /srv/homes       hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)

#

# Example for NFSv4:

# /srv/nfs4        gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)

# /srv/nfs4/homes  gss/krb5i(rw,sync,no_subtree_check)

/srv *(rw,sync)

Cấu hình cho keepalived check node primary và tự mount , tự bật NFS
Thực hiện trên node primary

cat >/etc/keepalived/keepalived.conf <<EOL

global_defs {

  # Keepalived process identifier

  router_id nfsserver

  enable_script_security

  script_user root

}



# Script to check whether Nginx is running or not

vrrp_script check_nfs {

  script "bash /etc/keepalived/trackdrbd.sh"

  interval 2

  weight 50

}



# Virtual interface - The priority specifies the order in which the assigned interface to take over in a failover

vrrp_instance VI_01 {

  state MASTER

  interface ens160

  virtual_router_id 152

  priority 110



  virtual_ipaddress {

        10.144.136.40/26

  }

  track_script {

        check_nfs

  }

    notify_master /etc/keepalived/notify_master.sh

    notify_backup /etc/keepalived/notify_backup.sh

    notify_stop /etc/keepalived/notify_stop.sh

  authentication {

        auth_type PASS

        auth_pass secret

  }

}

EOL

File cấu hình keepalive cho Secondary

global_defs {

  # Keepalived process identifier

  router_id nfsserver

  enable_script_security

  script_user root

}



# Script to check whether Nginx is running or not

vrrp_script check_nfs {

  script "bash /etc/keepalived/trackdrbd.sh"

  interval 2

  weight 50

}



# Virtual interface - The priority specifies the order in which the assigned interface to take over in a failover

vrrp_instance VI_01 {

  state BACKUP

  interface ens160

  virtual_router_id 152

  priority 100



  virtual_ipaddress {

        10.144.136.40/26

  }

  track_script {

        check_nfs

  }

    notify_master /etc/keepalived/notify_master.sh

    notify_backup /etc/keepalived/notify_backup.sh

    notify_stop /etc/keepalived/notify_stop.sh

  authentication {

        auth_type PASS

        auth_pass secret

  }

}



File script cho keepalived

root@mefin-ntl-drbd-01:~# cat /etc/keepalived/trackdrbd.sh 

#!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

MOUNTPONT=/srv

VOLUMENAME=nfs-fintech

if [ -e /proc/drbd ]

then

	# check_drbd=$(drbdadm status)

	CURRENT_ROLE=$(cat /proc/drbd | grep -oE "ro:[A-Za-z]+/[A-Za-z]+")

	if [ $CURRENT_ROLE == "ro:Secondary/Secondary" ]

	then

		#Promote to Primary

		drbdadm primary $VOLUMENAME

		mount /dev/drbd0 $MOUNTPONT && systemctl start nfs-server

		# if grep -qs "$MOUNTPONT " /proc/mounts

		# then

			# echo "MOUNTPONT exist"

		# else

			# echo "MOUNTPONT not exist"

		# fi

	elif [ $CURRENT_ROLE == "ro:Secondary/Primary" ]

	then

		echo "second node, do nothing"

		exit 97

	elif [[ "$CURRENT_ROLE" == *"ro:Primary/"* ]]

	then

		if grep -qs "$MOUNTPONT " /proc/mounts

		then

			echo "MOUNTPONT exist"

			cat /srv/system/flag_nodelete

			exit 0

		else

			echo "MOUNTPONT not exist, doing now"

			mount /dev/drbd0 $MOUNTPONT && systemctl start nfs-server && echo "Mount OK" || echo "Mount Fail" && exit 97

		fi

	elif [ $CURRENT_ROLE == "ro:Secondary/Primary" ]

	then

		echo "second node, do nothing"

		exit 97

	elif [ $CURRENT_ROLE == "ro:Secondary/Unknown" ]

	then

		echo "Primary not found, promote to pri and mount system"

		drbdadm primary $VOLUMENAME

		mount /dev/drbd0 $MOUNTPONT

		systemctl start nfs-server

# xu ly cac truong hop ngoai le khac

	else

		echo $CURRENT_ROLE

		exit 98

	fi

else

	echo "DRBD not running"

	exit 99

fi

Các file notify còn lại là file rỗng, không có giá trị sử dụng. các bạn có thể comment cấu hình trong keepalived lại