一、方案图
该方案可以解决nfs服务器的单点故障问题,也可以解决数据的备份难题,可以作为公司从nfs文件系统-->分布式存储系统的过渡解决方案。由于我公司从初创到至今,用户的产生数据量与日俱增,在探讨了现有nfs文件系统不能解决现有问题以及现阶段部署分布式存储系统的大量财力、人力的基础上,提出这么一个可以过度的方案。使用LVM磁盘管理技术可以很方便的在每个主机上进行磁盘的扩容。辅助监控监本智能监测服务状态,节省了运维成本!
二、配置步骤
1、LVM管理(主机、备机)
创建分区:fdisk /dev/xvde
p 查看分区表
n 创建分区
t 修改分区类型 8e LVM
w 保存分区表并退出(写入分区表)
使分区生效: partprobe
创建pv:pvcreate /dev/xvde1 #创建物理卷
pvdisplay #查看已经产生的pv
创建VG:vgcreate VGdata /dev/xvde1 #创建卷组,名称为VGdata
vgdisplay #查看卷组表
创建LV:lvcreate -L 20G -n LVdata VGdata #-L lv的大小 -n 指定lv的名称
lvdisplay #显示LV信息
格式化LV并挂载文件系统
mkfs.ext4 /dev/VGdata/LVdata
mount /dev/VGdata/LVdata /data
管理--> 增加lv大小
首先创建一块新的分区并格式化分区
mkfs.ext4 /dev/xvde2
创建pv,扩容VG,LV
vgextend VGdata /dev/xvde2 #扩容VG
lvextend -L 50G /dev/VGdata/LVdata #扩容LV到50G +50G是对LV直接增加50G
扩容文件系统:
resize2fs /dev/VGdata/LVdata #执行该命令重设大小,对于当前正在使用的LVdata有效
查看扩容情况:df -h
============================================================
如果希望创建一个使用全部卷组的逻辑卷,则需要首先察看该卷组的PE数,然后在创
建逻辑卷时指定:
# vgdisplay web_document| grep “Total PE”
Total PE 45230
# lvcreate -l 45230 web_document -n www1
一般建议最好将文件系统卸载,调整大小,然后再加载:
# umount /dev/web_document/www1
# resize_reiserfs /dev/web_document/www1
# mount -treiserfs /dev/web_document/www1 /data/wwwroot
==============================================================
2.inotify+rsync安装与配置(主机、备机)
安装rsync
cd /usr/local/usr/
wget http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz
tar xf rsync-3.1.1.tar.gz
cd rsync-3.1.1
./configure --prefix=/usr/local/rsync && make && make install
建立密码认证文件:
echo "yzkj1234" >/usr/local/rsync/rsync.passwd
chmod 600 /usr/local/rsync/rsync.passwd
安装inotify
cd /usr/local/src/
wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar xf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify && make && make install
===========================================
使用脚本进行后台运行监测文件的状态(主机)
sh /opt/shell/rsync.sh &
#!/bin/bash
host=10.124.x.x # 备机IP地址
src=/data/ # 主机数据源路径
des=DataBack # 备机/usr/local/rsync/rsyncd.conf文件设置的认证模块名
user=cloud # 与备机进行数据传输的认证用户,备机/usr/local/rsync/rsyncd.passwd 文件记录验证用户名:密码
#yestoday=`date -d"1 day ago" +"%F"` #定义昨天的日期
#todayTime=`date +%H%M%S` #120255 时分秒
#使用inotifywait命令对源数据路径文件进行列表遍历,并定义了日志的输入格式: 17/04/15 16:32 /data/10ATTRIB was rsynced.
/usr/local/inotify/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f%e‘ -e modify,delete,create,attrib $src | while read files
do
#使用rsync命令对新产生的数据进行同步,其中--delete将在传输数据的过程中将主机中不存在而备机上存在的数据进行删除,如果需要保持备机的数据不被删除,则将--delete参数去掉即可
/usr/local/rsync/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsyncd.passwd $src [email protected]$host::$des
echo "${files} was rsynced." >>/usr/local/rsync/logs/rsync.log 2>&1
#使用crontab计划任务实现日志的轮转 crontab -e 0 0 * * * mv /usr/local/rsync/logs/rsync.log /usr/local/rsync/logs/rsync-$yestoday.log
# if [[ ${todayTime} -ge 000000 ]]
# then
# mv /usr/local/rsync/logs/rsync.log /usr/local/rsync/logs/rsync-$yestoday.log
# touch /usr/local/rsync/logs/rsync.log
# fi
done
配置rsyncd.conf文件并启动rsyncd服务(备机)
vim /usr/local/rsync/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 10
pid file = /usr/local/rsync/rsyncd.pid
lock file = /usr/local/rsync/rsync.lock
log file = /usr/local/rsync/logs/rsyncd.log
[DataBack]
path=/data/
comment = update
ignore errors
read only = no
write only = no
list = flase
hosts allow = 10.124.X.X
auth users = cloud
uid = root
gid = root
rsync服务启动脚本vim /etc/init.d/rsyncd chmod +x /etc/init.d/rsyncd
=======================================
#!/bin/bash
#
# rsyncd This shell script takes care of starting and stopping
# standalone rsync.
#
# chkconfig: - 99 50
# description: rsync is a file transport daemon
# processname: rsync
# config: /usr/local/rsync/rsyncd.conf
# Source function library
. /etc/rc.d/init.d/functions
RETVAL=0
rsync="/usr/local/rsync/bin/rsync"
prog="rsync"
CFILE="/usr/local/rsync/rsyncd.conf"
start() {
# Start daemons.
[ -x $rsync ] || \
{ echo "FATAL: No such programme";exit 4; }
[ -f $CFILE ] || \
{ echo "FATAL: config file does not exist";exit 6; }
echo -n $"Starting $prog: "
daemon $rsync --daemon --config=$CFILE
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
echo
return $RETVAL
}
stop() {
# Stop daemons.
echo -n $"Stopping $prog: "
killproc $prog -QUIT
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
return $RETVAL
}
# call the function we defined
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|status}"
exit 2
esac
exit $RETVAL
==============================================
使用脚本进行后台运行监测文件的状态(备机)
sh /opt/shell/rsync.sh &
#!/bin/bash
host=10.124.x.x # 备机IP地址
src=/data/ # 主机数据源路径
des=DataMain # 备机/usr/local/rsync/rsyncd.conf文件设置的认证模块名
user=cloud # 与备机进行数据传输的认证用户,备机/usr/local/rsync/rsyncd.passwd 文件记录验证用户名:密码
#yestoday=`date -d"1 day ago" +"%F"` #定义昨天的日期
#todayTime=`date +%H%M%S`
#使用inotifywait命令对源数据路径文件进行列表遍历,并定义了日志的输入格式: 17/04/15 16:32 /data/10ATTRIB was rsynced.
/usr/local/inotify/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f%e‘ -e modify,delete,create,attrib $src | while read files
do
#使用rsync命令对新产生的数据进行同步,其中--delete将在传输数据的过程中将主机中不存在而备机上存在的数据进行删除,如果需要保持备机的数据不被删除,则将--delete参数去掉即可
/usr/local/rsync/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsyncd.passwd $src [email protected]$host::$des
echo "${files} was rsynced." >>/usr/local/rsync/logs/rsync.log 2>&1
done
配置rsyncd.conf文件并启动rsyncd服务(主机)
vim /usr/local/rsync/rsyncd.conf 启动服务:/usr/local/bin/rsync --daemon --config=/usr/local/rsync/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 10
pid file = /usr/local/rsync/rsyncd.pid
lock file = /usr/local/rsync/rsync.lock
log file = /usr/local/rsync/logs/rsyncd.log
[DataMain]
path=/data/
comment = update
ignore errors
read only = no
write only = no
list = flase
hosts allow = 10.124.X.X
auth users = cloud
uid = root
gid = root
service rsyncd start
==================================================================
3.nginx安装与配置(主机、备机)
user nginx;
group nginx;
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 5000;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
#access_log logs/access.log main;
server_names_hash_bucket_size 64;
fastcgi_intercept_errors on;
server_tokens off;
tcp_nodelay on;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;#端口
server_name localhost;#服务名
root /data/; #显示的根索引目录
autoindex on;#开启索引功能
autoindex_exact_size off;#关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; # 显示本机时间而非 GMT 时间
charset utf-8;
#access_log logs/host.access.log main;
error_page 404 = http://www.apicloud.com/404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/html;
}
}
}
=================================================================
监控lvs状态的脚本(主机)
/usr/local/nagios/libexec/check_lvs_rsync.sh
#!/bin/bash -x
##################################
#Author:qingbo.song #
#Date:2015-4-17 #
#E-mail:[email protected] #
#Comment:Lvs_status #
##################################
#0 正常
#1 告警
#2 紧急
#3 未知
keepalived_process=`ps aux|grep [k]eepalived|grep -v ‘check_lvs_rsync.sh‘|wc -l`
processNum=`ps -ef|grep [r]sync.sh|grep -v ‘check_rsync_process.sh‘|wc -l`
#sed -n ‘9p‘ file 获取某一行的数据
ip=`ip addr|sed -n ‘9p‘|awk ‘{print $2}‘` #获取vip地址
file=/opt/shell/rsync.sh
if [ "x${keepalived_process}" = "x3" ] #判断keepalived进程数是否正常
then
if [ "${ip}" = "10.124.x.x/32" ] #keepalived进程数正常,判断VIP是否正常
then
if [ ${processNum} = 2 ] #keepalived进程数正常,VIP正常,判断rsync实时备份进程数是否正常
then
echo "Keepalived is OK!数据传输到主机!Rsync实时备份脚本运行正常!"
exit 0 #keepalived进程数正常,VIP正常,rsync实时备份进程数正常,返回信号0--正常
else
if [ ! -f "$file" ] #keepalived进程数正常,VIP正常,rsync实时备份进程数不正常,判断rsync.sh脚本是否存在
then
echo "Keepalived is OK!数据传输到主机!Rsync实时备份进程&&rsync.sh脚本不存在,请联系管理员!"
exit 2 #keepalived进程数正常,VIP正常,rsync实时备份进程数不正常,判断rsync.sh脚本不存在,返回信号量2请管理员处理
else
/usr/bin/sh /opt/shell/rsync.sh & #执行rsync.sh脚本并后台运行
if [ ${processNum} = 2 ] #keepalived进程数正常,VIP正常,rsync实时备份进程数不正常,判断rsync.sh脚本存在,重新执行之后再次判断rsync进程数
then
echo "Keepalived is OK!数据传输到主机!Rsync实时备份进程已重新启动!"
exit 0 #rsync.sh正常执行,返回信号量0--正常
else
echo "Keepalived is OK!数据传输到主机!Rsync实时备份进程未能重新启动,请联系管理员!"
exit 2 #rsync.sh不能正常执行,返回信号量2--紧急,请求管理员处理
fi
fi
fi
else
if [ ${processNum} = 2 ] #keepalived进程数正常,VIP不正常,判断rsync实时备份进程数是否正常
then
/usr/bin/pkill rsync.sh #杀死rsync.sh进程,数据写到备机,主机这边应该将rsync.sh脚本暂停
if [ ${processNum} = 2 ] #再次判断rsync.sh进程数
then
echo "Keepalived is OK!数据传输到备机!Rsync实时备份进程未能正常关闭,请联系管理员!"
exit 2 #rsync.sh进程不能杀死,返回信号2--紧急,请求管理员处理
else
echo "Keepalived is OK!数据传输到备机!Rsync实时备份进程正常关闭!"
exit 1 #rsync.sh进程关闭成功,返回信号量1--警告,但数据应该是传输到备机上的,所以这块儿管理员需要进行检查
fi
else #keepalived进程数正常,VIP不正常,rsync实时备份进程数不正常,则是rsync.sh脚本已经停止执行
echo "Keepalived is OK!数据传输到备机!Rsync实时备份进程正常关闭!"
exit 1
fi
fi
else
if [ ${processNum} = 2 ] #keepalived进程数异常,判断rsync实时备份进程数是否正常
then
/usr/bin/pkill rsync.sh #rsync实时备份进程数正常的话,我们需要kill掉
if [ ${processNum} = 2 ]
then
echo "Keepalived is ERR!Rsync实时备份进程未能正常关闭,请联系管理员!"
exit 2 #Rsync实时备份进程未能关掉,返回信号量2--紧急,请联系管理员
else
echo "Keepalived is ERR!Rsync实时备份进程正常关闭!"
exit 1 #Rsync实时备份进程正常关掉,返回信号量1--警告
fi
else
echo "Keepalived is ERR!Rsync实时备份进程正常关闭!"
exit 1
fi
fi
===================================================================================
监控lvs状态的脚本(备机)
/usr/local/nagios/libexec/check_lvs_rsync.sh
#!/bin/bash -x
##################################
#Author:qingbo.song #
#Date:2015-4-17 #
#E-mail:[email protected] #
#Comment:Lvs_status #
##################################
#/usr/local/nagios/libexec/check_lvs_rsync.sh
#0 正常
#1 告警
#2 紧急
#3 未知
keepalived_process=`ps aux|grep [k]eepalived|grep -v ‘check_lvs_rsync.sh‘|wc -l`
processNum=`ps -ef|grep rsync.sh|grep -v grep|wc -l`
#sed -n ‘9p‘ file 获取某一行的数据
ip=`ip addr|sed -n ‘9p‘|awk ‘{print $2}‘` #获取vip地址
file=/opt/shell/rsync.sh
if [ "x${keepalived_process}" = "x3" ] #判断keepalived进程数是否正常
then
if [ "${ip}" = "10.124.151.4/32" ] #keepalived进程数正常,判断VIP是否正常
then
if [ ${processNum} = 2 ] #keepalived进程数正常,VIP正常,判断rsync实时备份进程数是否正常
then
echo "Keepalived is OK!数据传输到备机!Rsync实时备份脚本运行正常!"
exit 0 #返回信号量0表示正常
else
if [ ! -f "$file" ] #keepalived进程数正常,VIP正常,rsync实时备份进程数不正常,判断rsync.sh脚本是否存在
then
echo "Keepalived is OK!数据传输到备机!Rsync实时备份进程&&rsync.sh脚本不存在,请联系管理员!"
exit 2 #keepalived进程数正常,VIP正常,rsync实时备份进程数不正常,判断rsync.sh脚本不存在,返回信号量2请管理员处理
else
/usr/bin/sh /opt/shell/rsync.sh & #执行rsync.sh脚本并后台运行
if [ ${processNum} = 2 ] #再次判断rsync进程数
then
echo "Keepalived is OK!数据传输到备机!Rsync实时备份进程已重新启动!"
exit 0 #rsync.sh正常执行,返回信号量0--正常
else
echo "Keepalived is OK!数据传输到备机!Rsync实时备份进程未能重新启动,请联系管理员!"
exit 2 #rsync.sh不能正常执行,返回信号量2--紧急,请求管理员处理
fi
fi
fi
else
if [ ${processNum} = 2 ]
then
/usr/bin/pkill rsync.sh #杀死rsync.sh进程,数据写到主机,备机这边应该将rsync.sh脚本暂停
if [ ${processNum} = 2 ] #再次判断rsync.sh进程数
then
echo "Keepalived is OK!数据传输到主机!Rsync实时备份进程未能正常关闭,请联系管理员!"
exit 2
else
echo "Keepalived is OK!数据传输到主机!Rsync实时备份进程正常关闭!"
exit 0
fi
else
echo "Keepalived is OK!数据传输到主机!Rsync实时备份进程关闭正常!"
exit 0
fi
fi
else
if [ ${processNum} = 2 ] #keepalived进程数异常,判断rsync实时备份进程数是否正常
then
/usr/bin/pkill rsync.sh #rsync实时备份进程数正常的话,我们需要kill掉
if [ ${processNum} = 2 ]
then
echo "Keepalived is ERR!Rsync实时备份进程未能正常关闭,请联系管理员!"
exit 2 #Rsync实时备份进程未能关掉,返回信号量2--紧急,请联系管理员
else
echo "Keepalived is ERR!Rsync实时备份进程正常关闭!"
exit 1 #Rsync实时备份进程正常关掉,返回信号量1--警告
fi
else
echo "Keepalived is ERR!Rsync实时备份进程正常关闭!"
exit 1
fi
fi
=====================================================================
4.keepalived安装与配置
1)Keepalived的编译安装
cd /usr/local/src/
wget http://www.keepalived.org/software/keepalived-1.1.20.tar.gz
tar xf keepalived-1.2.15.tar.gz
cd keepalived-1.1.20
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-279.el6.x86_64/
make && make install
将Keepalived做成服务:
cd /usr/local/keepalived/ && cp etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ && cp etc/sysconfig/keepalived /etc/sysconfig/ && mkdir /etc/keepalived && cp etc/keepalived/keepalived.conf /etc/keepalived/ && cp sbin/keepalived /usr/sbin/
2)修改防火墙
-A INPUT -p vrrp -j ACCEPT #基于DR模式,当用户发出请求后,只有DR响应ARP广播包,允许vrrp虚拟路由器冗余协议
重启防火墙:service iptables restart
3)keepalived配置文件修改
vim /etc/keepalived/keepalived (主)
==================================================
! Configuration File for keepalived
global_defs {
notification_email { //定义邮件服务的
[email protected] //定义收件人,这里改为本机,只是测试使用
}
notification_email_from [email protected] //定义发件人,
smtp_server 127.0.0.1 //定义邮件服务器,一定不能使用外部地址
smtp_connect_timeout 30 //超时时间
router_id LVS_DOWNLOAD
}
vrrp_instance VI_1 { //定义虚拟路由,VI_1 为虚拟路由的标示符,自己定义名称
state MASTER //开启后,该节点的优先级比另一节点的优先级高,所以转化为MASTER状态
interface eth0 //所有的通告等信息都从eth0这个接口出去
virtual_router_id 7 //虚拟路由的ID,而且这个ID也是虚拟MAC最后一段的来源,这个ID号一般不能大于255,且这个ID一定不能有冲突
priority 100 //初始优先级
advert_int 1 //通告的个数
authentication { //认证机制
auth_type yzkj //认证类型
auth_pass yzkj1234 //密码,应该为随机的字符串
}
virtual_ipaddress { //虚拟地址,即VIP
10.124.151.4
}
}
==========================================================
vim /etc/keepalived/keepalived (备)
==========================================================
! Configuration File for keepalived
global_defs {
notification_email { //定义邮件服务的
[email protected] //定义收件人,这里改为本机,只是测试使用
}
notification_email_from [email protected] //定义发件人,
smtp_server 127.0.0.1 //定义邮件服务器,一定不能使用外部地址
smtp_connect_timeout 30 //超时时间
router_id LVS_DOWNLOAD
}
vrrp_instance VI_1 { //定义虚拟路由,VI_1 为虚拟路由的标示符,自己定义名称
state BACKUP //修改从节点的状态,主节点为MASTER,从节点就为BACKUP
interface eth0 //所有的通告等信息都从eth0这个接口出去
virtual_router_id 7 //虚拟路由的ID,而且这个ID也是虚拟MAC最后一段的来源,这个ID号一般不能大于255,且这个ID一定不能有冲突
priority 99 //修改优先级,注意从节点的优先级一定要小于主节点
advert_int 1 //通告的个数
authentication { //认证机制
auth_type yzkj //认证类型
auth_pass yzkj1234 //密码,应该为随机的字符串
}
virtual_ipaddress { //虚拟地址,即VIP
10.124.151.4
}
}
至此服务器配置完毕!
附注:参考链接
LVM磁盘管理 http://www.cnblogs.com/gaojun/archive/2012/08/22/2650229.html
基于keepalived实现VIP转移 lvs nginx的高可用 http://www.it165.net/admin/html/201309/1865.html
rsync启动脚本 http://my.oschina.net/u/236631/blog/113472
inotifyrsync -- 用inotify和rsync实现多机文件实时同步 http://gaopenghigh.iteye.com/blog/1746741
文件共享存储&&主备实时热备实现方案