Extent/Expand/Grow disk space đối với máy chủ ảo chạy linux

Máy chủ có 500GB ổ cứng, theo nhu cầu cần mở rộng lên 1TB
Máy chủ chạy centos 7 trên VMware
1, Lên VMWare, edit disk của máy chủ lên TB
2, Truy cập máy chủ centos:
– Scan lại disk

[root@mm-adap1 ~]# ls /sys/class/scsi_device/

0:0:0:0  3:0:0:0

[root@mm-adap1 ~]# echo 1 > /sys/class/scsi_device/0\:0\:0\:0/device/rescan 

[root@mm-adap1 ~]# echo 1 > /sys/class/scsi_device/3\:0\:0\:0/device/rescan 

[root@mm-adap1 ~]# fdisk -l



Disk /dev/sda: 500 GB, 1099511627776 bytes, 2147483648 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk label type: dos

Disk identifier: 0x000add72



   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *        2048     2099199     1048576   83  Linux

/dev/sda2         2099200   629145599   313523200   8e  Linux LVM

/dev/sda3       629145600  1048575999   209715200   8e  Linux LVM



Disk /dev/mapper/centos_mm--adap1-root: 500 GB, 1098425303040 bytes, 2145361920 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Đã thấy dung lượng tăng lên,  ổ đĩa tác động ở đây là /dev/sda, tiến hành fdisk

[root@mm-adap1 ~]# fdisk /dev/sda

Welcome to fdisk (util-linux 2.23.2).



Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.





Command (m for help): n

Partition type:

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

   e   extended

Select (default p): p

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

First sector (2048-20971519, default 2048):

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):

Using default value 20971519

Partition 1 of type Linux and of size 10 GiB is set



Command (m for help): t

Selected partition 4

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.

Syncing disks.

Khởi tạo physical volume

partprobe

[root@mm-adap1 ~]# pvcreate /dev/sda4

  Physical volume "/dev/sda4" successfully created.

Lấy thông tin volume group hiện tại

[root@mm-adap1 ~]# vgdisplay 

  --- Volume group ---

  VG Name               centos_mm-adap1

  System ID             

  Format                lvm2

  Metadata Areas        2

  Metadata Sequence No  4

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                1

  Open LV               1

  Max PV                0

  Cur PV                2

  Act PV                2

  VG Size               498.99 GiB

  PE Size               4.00 MiB

  Total PE              127742

  Alloc PE / Size       127742 / 498.99 GiB

  Free  PE / Size       0 / 0   

  VG UUID               eyBefC-npQ3-Kl7Q-lHeC-j08s-OLCG-zl33QK

Ta được name VG Name = centos_mm-adap1, tiến hành nhét sda4 vừa tạo vào volume group

[root@mm-adap1 ~]# vgextend centos_mm-adap1 /dev/sda4

  Volume group "centos_mm-adap1" successfully extended

Làm tương tự, mở rộng tiếp logical volume

[root@mm-adap1 ~]# lvdisplay

  --- Logical volume ---

  LV Path                /dev/centos_mm-adap1/root

  LV Name                root

  VG Name                centos_mm-adap1

  LV UUID                dzkVGt-aTiT-bWyI-kYDm-cUES-7WEm-tAtFva

  LV Write Access        read/write

  LV Creation host, time mm-adap1, 2020-10-18 16:46:50 +0700

  LV Status              available

  # open                 1

  LV Size                498.99 GiB

  Current LE             127742

  Segments               2

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     8192

  Block device           253:0

   

[root@mm-adap1 ~]# lvextend /dev/centos_mm-adap1/root /dev/sda4

  Size of logical volume centos_mm-adap1/root changed from 498.99 GiB (127742 extents) to <1022.99 GiB (261885 extents).

  Logical volume centos_mm-adap1/root successfully resized.

Kiểm tra mount

[root@mm-adap1 ~]# mount | grep -i mapper

/dev/mapper/centos_mm--adap1-root on / type ext4 (rw,relatime,data=ordered)

Thấy LVM /dev/mapper/centos_mm–adap1-root
tiến hành resize, ở đây phân vùng xài ext4 nên sử dụng resize2fs

[root@mm-adap1 ~]# resize2fs /dev/mapper/centos_mm--adap1-root

resize2fs 1.42.9 (28-Dec-2013)

Filesystem at /dev/mapper/centos_mm--adap1-root is mounted on /; on-line resizing required

old_desc_blocks = 63, new_desc_blocks = 128

The filesystem on /dev/mapper/centos_mm--adap1-root is now 268170240 blocks long.



Kiểm tra lại thấy / đã có 1TB

[root@mm-adap1 ~]# df -h

Filesystem                         Size  Used Avail Use% Mounted on

/dev/mapper/centos_mm--adap1-root 1007G  107G  857G  12% /

devtmpfs                            16G     0   16G   0% /dev

tmpfs                               16G     0   16G   0% /dev/shm

tmpfs                               16G  169M   16G   2% /run

tmpfs                               16G     0   16G   0% /sys/fs/cgroup

/dev/sda1                          976M  100M  810M  11% /boot

tmpfs                              3.2G     0  3.2G   0% /run/user/0

 

 

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

 

Xử lý lỗi trên ubuntu dh key too small

Khi gọi http request trên ubuntu, ở đây là ubuntu 20 thì hay gặp lỗi
curl: (35) error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small
(các ngôn ngữ khác như java, python báo lỗi tương tự)
Cách fix:
Sửa file /etc/ssl/openssl.cnf
Thêm vào đầu file

openssl_conf = default_conf

Thêm vào cuối file

[default_conf]

ssl_conf = ssl_sect



[ssl_sect]

system_default = system_default_sect



[system_default_sect]

CipherString = DEFAULT:@SECLEVEL=1



Thế là xong