@saltyang
2018-06-19T03:38:53.000000Z
字数 9325
阅读 2679
NFS, Keepalived, Rsync, Inotify
存储服务器采用NFS的方式向接收服务器提供存储服务,存储服务器本身采用rsync + inotify实现文件的实时同步,其中Keepalived保证NFS服务的高可用,实现故障转移和自动切换。
Linux: Centos 6.9Rsync Version: 3.0.6master_nfs ip: 192.168.1.176salve_nfs ip: 192.168.1.177VIP: 192.168.1.178
ENV Prepare
yum install rpcbind nfs-utils;yum install keepalived rsync inotify-tools;chkconfig keepalived on; chkconfig nfs on;
Rsync Server Config
Rsync Server is the backup machine, the first is salve_nfs.
vim /etc/rsyncd.cnf
##rsyncd.conf start###工作中指定用户(需要指定用户)uid = rootgid = rootuse chroot = no#有多少个客户端同时传文件max connections = 36000#超时时间timeout = 300#进程号文件pid file = /var/run/rsyncd.pid#日志文件lock file = /var/run/rsync.lock#日志文件log file = /var/log/rsyncd.log#模块开始#这个模块对应的是推送目录#模块名称随便起[backup]#需要同步的目录path = /data/#表示出现错误忽略错误ignore errors = yes#表示网络权限可写(本地控制真正可写)read only = false#这里设置IP或让不让同步list = false#指定允许的网段hosts allow = 192.168.1.0/24#拒绝链接的地址,一下表示没有拒绝的链接。hosts deny = 0.0.0.0/32#不要动的东西(默认情况)#虚拟用户auth users = rsync_backup#虚拟用户的密码文件secrets file = /etc/rsync.password
echo "rsync_backup:ems" > /etc/rsync.passwordchmod 600 /etc/rsync.password
rsync --daemonps -ef | grep "rsync" | grep -v "grep" # check daemon success or notecho "/usr/bin/rsync --daemon" >>/etc/rc.local # add daemon to machine start
Rsync Client Config
Rsync Client is the machine which satrted nfs service, the first is master_nfs.
#!/bin/bash# rsync server addressRSYNC_SERVER=192.168.1.177# rsync client monitor docSYC_DIR=/data/# rsync server service module nameDST_DIR=backup# rsync server service auth userUSER=rsync_backup# rsync service password fileRSYNC_PASSFILE=/etc/rsync_client.passwordINOTIFYWAIT=/usr/bin/inotifywaitLOG_FILE=/var/log/rsyncd.logif [ ! -e "$SYC_DIR" ] || [ ! -e "${RSYNC_PASSFILE}" ] || [ ! -e "${INOTIFYWAIT}" ] \|| [ ! -e "/usr/bin/rsync" ];thenecho "Check File and Folder" > $LOG_FILEexit 9fi${INOTIFYWAIT} -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $SYC_DIR \| while read filedocd $SYC_DIRrsync -aruz -R --delete ./ --timeout=100 $USER@$RSYNC_SERVER::$DST_DIR --password-file=${RSYNC_PASSFILE} >/dev/null 2>&1doneexit 0
echo ems > /etc/rsync_client.password
Keepalived Config
! Configuration File for keepalivedglobal_defs {notification_email {salt_yang@puyacn.com}notification_email_from service@webackup.cnsmtp_server smtp.mxichina.cnsmtp_connect_timeout 30router_id NFS_MASTER}vrrp_script chk_nfs {script "/etc/keepalived/checknfs.sh check"interval 30}vrrp_instance NFS_MASTER {state MASTERinterface eth5virtual_router_id 54priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.1.178}track_script {chk_nfs}debugnopreemptnotify_master "/etc/keepalived/checknfs.sh master"notify_backup "/etc/keepalived/checknfs.sh backup"notify_fault "/etc/keepalived/checknfs.sh fault"notify_stop "/etc/keepalived/checknfs.sh fault"}
! Configuration File for keepalivedglobal_defs {notification_email {salt_yang@puyacn.com}notification_email_from service@webackup.cnsmtp_server smtp.mxichina.cnsmtp_connect_timeout 30router_id NFS_BACCKUP}vrrp_script chk_nfs {script "/etc/keepalived/checknfs2.sh check"interval 30}vrrp_instance NFS_BACKUP {state BACKUPinterface eth5virtual_router_id 54priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.1.178}track_script {chk_nfs}debugnopreemptnotify_master "/etc/keepalived/checknfs.sh master"notify_backup "/etc/keepalived/checknfs.sh backup"notify_fault "/etc/keepalived/checknfs.sh fault"notify_stop "/etc/keepalived/checknfs.sh fault"}
#!/bin/dash# Script to handle NFS from keepalived.## Usage: checknfs.sh action## Note: you can use $MAINTENANCE (/etc/keepalived/maintenance) to disable NFS checks# in case of short NFS maintenance## Usage func :[ "$1" = "--help" ] && { sed -n -e '/^# Usage:/,/^$/ s/^# \?//p' < $0; exit; }## CONFIG## KeepalivedKEEPALIVEDPID=/var/run/keepalived.pidKEEPALIVED=/etc/init.d/keepalived# NFSDNFS_FLAG="nfsd"# rync + inotifyRSYNC=/usr/bin/rsyncRSYNCDPID=/var/run/rsyncd.pidINOTIFY_SCRIPT=/usr/local/inotify.shINOTIFY_FLAG="inotify"RSYNC_FLAG="rsync"# local mount pointMOUNTPOINT="/data"# warmup delayMAXWAIT=240VIP=192.168.1.178# how to handle potential split-brain# 0: manual# 1: invalidate local data# 2: invalidate remote dataSPLIT_BRAIN_METHOD=1# maintenance flag: used to do maintenance on NFS without switch between nodesMAINTENANCE="/etc/keepalived/maintenance"## CONFIG LOGGER## tail -f /var/log/syslog | grep KeepLOG="logger -t KeepNFS[$$] -p syslog" # do not use -iLOGINFO="$LOG.info"LOGWARN="$LOG.warn"LOGERR="$LOG.err"check() {if ip addr | grep "${VIP}"then# if nfs server is down, need restart it.# Keepalive need switch after restart nfs failed$LOGWARN "This is Master."if check_nfsthen$LOGWARN "NFS is OK."elsestart_nfs$LOGWARN "Restart NFS service"if check_nfsthen$LOGWARN "After restart NFS service, but it's also down. Switch keepalive status"$KEEPALIVED restartfifi# if inotify server is down, need restart it.# nfs is also OK, keepalived needn't switch after restart nfs failedif check_inotifythen$LOGWARN "Inotify is OK."else$LOGWARN "Start Inotify script......"/bin/bash $INOTIFY_SCRIPT &if check_inotifythen$LOGWARN "After restart Inotify script, but it's also down and need man repair"fifielse$LOGWARN "This is Salve."if check_rsycn_daemonthen$LOGWARN "Rsync daemon is OK."else$LOGWARN "Start Rsync daemon....."start_rsync_daemonif check_rsycn_daemonthen$LOGWARN "After restart Rsync service, but it's also down and need man repair"fififireturn $?}set_fault() {set_backup}set_backup() {if check_nfsthen$LOGWARN "NFS Service is UP, now kill it"kill_nfsfiset_rsync_server}set_rsync_server() {# start rsync daemonif check_rsycn_daemonthen$LOGWARN "[Rsync daemon Service is Up.]"else$LOGWARN "[Begin to start daemon Service.]"start_rsync_daemonfor i in $( seq 1 $MAXWAIT )dosleep 1if check_rsycn_daemonthenbreakfidonefiif ! check_rsycn_daemonthen$LOGWARN "[After 240s, rsync daemon doesn't start, need adminstrator to repair]"fi# kill inotify serviceif check_inotifythenif kill_inotifythen$LOGWARN "[Kill inotify Service.]"else$LOGWARN "[Kill inotify Service failed, need adminstrator to repair]"fifi}# WARNING set_master is called at keepalived start# So if already in "good" state we must do nothing :)set_master() {# Starting NFSif [ $( pidof nfsd | wc -w ) -gt 0 ]then$LOGWARN "NFS already started ? What did I have to do ?"else$LOGWARN "Starting NFS ..."/sbin/service nfs restartfor i in $( seq 1 $MAXWAIT )dosleep 1if check_nfsthenbreakfidonefiif check_nfsthen$LOGWARN "NFS service has started"else$LOGWARN "NFS Service is broken and need a manual repair."fi# check inotify whether is not active. if not, start it.if check_inotifythen$LOGWARN "Inotify has started"else$LOGWARN "Started Inotify"start_inotifyfi# check rsync daemon service. if it's active, kill it.if check_rsync_daemonthenif kill_rsync_daemonthen$LOGWARN "[Kill Rsync Service successed!]"else$LOGWARN "[Kill Rsync Service failed, need adminstrator to repair]"fifi}# Check that NFS is responding# Return:# 0 if nfs is OK (or in maintenance mode)# 1 if nfs is downcheck_nfs() {if [ -e $MAINTENANCE ]thenreturn 0fiflag_exists=$(ps -ef | grep "${NFS_FLAG}" | grep -v grep | wc -l)if [ "${flag_exists}" -eq 0 ]then$LOGWARN "[NFS service is unavailable.]"return 1elsereturn 0fi}kill_nfs() {/sbin/service nfs stopreturn $?}start_nfs() {if [ $( pidof rpcbind | wc -w ) -eq 0 ]then/sbin/service rpcbind startfi/sbin/service nfs startreturn $?}# Check that inotify service is responding# Return:# 0 if inotify is OK (or in maintenance mode)# 1 if inotify is downcheck_inotify() {flag_exists=$(ps -ef | grep "${INOTIFY_FLAG}" | grep -v grep | wc -l)if [ "${flag_exists}" -eq 0 ]then$LOGWARN "[Inotify daemon service is unavailable.]"return 1elsereturn 0fi}start_inotify() {/bin/bash $INOTIFY_SCRIPT &return $?}kill_inotify() {ps -ef | grep "${INOTIFY_FLAG}" | grep -v grep | awk '{print $2 }' | xargs kill -9return $?}# Check that rsync daemon is responding# Return:# 0 if rsync daemon is OK (or in maintenance mode)# 1 if rsync daemon is downcheck_rsycn_daemon() {flag_exists=$(ps -ef | grep "${RSYNC_FLAG}" | grep -v grep | wc -l)if [ "${flag_exists}" -eq 0 ]then$LOGWARN "[Rsync daemon service is unavailable.]"return 1elsereturn 0fi}# Start Rsync daemonstart_rsync_daemon() {if [ -e $RSYNCDPID ]thenrm -rf $RSYNCDPIDfi$RSYNC --daemonreturn $?}# Kill Rsync daemonkill_rsync_daemon() {ps -ef | grep "${RSYNC_FLAG}" | grep -v grep | awk '{print $2 }' | xargs kill -9return $?}case "$1" incheck)checkexit $?;;backup)$LOGWARN "=> set to backup state <="set_backupexit $?;;fault)$LOGWARN "=> set to fault state <="set_faultexit $?;;master)$LOGWARN "=> set to master state <="set_masterexit $?;;esac
Creat share dir
Note: Use LVM to aviod nfs state file handle when switch nfs server
# check disk partitionfdisk -l# Creat partition and enter partition managementfdisk /dev/sdaEnter these choices: n -> p(partition type) -> +500M(size) -> t(change partition type) -> 4(partition num) -> 8e(LVM type) -> p(check partition) -> w (write partition)# make partition valid and not need restartpartprobe# Create PV and checkpvcreate /dev/sda4pvdisplay# Create vg and add pv intovgcreate VolGroup /dev/sda4# If VolGroup is existed, not need to create vgextendvg VolGroup /dev/sda4# Creat a LV and named lvData and checklvcreate -L 100M -n lvData VolGrouplvdisplay# Format and mountmkfs -t ext4 /dev/VolGroup/lvDatamount /dev/VolGroup/lvData /data# add fstab in /etc/fstab/dev/VolGroup/lvData /data ext4 defaults 1 2
Extend share dir
# Add a disk# Creat disk partitionfdisk /dev/sdbEnter these choices: n -> p(partition type) -> +20G(size) -> t(change partition type) -> 1(partition num) -> p(check partition) -> w (write partition)# make partition valid and not need restartpartprobe# Create PV and checkpvcreate /dev/sdb1mkfs –t ext4 /dev/sdb1# Extend vgvgextend VolGroup /dev/sdb1# Extend lvlvextend -L 20G /dev/VolGroup/lvDataresize2fs /dev/VolGroup/lvData