一、环境准备
1 安装必要软件
1.1.1 安装Keepalived和IPVSAdm
下载软件
wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
安装Keepalived
tar -xzf keepalived-1.2.7.tar.gz cd keepalived-1.2.7 ./configure make && make install
配置Keepalived
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ mkdir /etc/keepalived cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/sbin/keepalived /usr/sbin
启动Keepalived
service keepalived start
安装IPVSAdm
tar -xzf ipvsadm-1.24.tar.gz cd ipvsadm-1.24 make && make install
验证安装
ipvsadm --help
2 配置FTP服务器
安装vsftpd
yum install vsftpd -y
配置vsftpd
# 编辑配置文件 vim /etc/vsftpd/vsftpd.conf # 修改以下内容 anonymous_enable=NO local_enable=YES write_enable=YES chroot_local_user=YES allow_writeable_chroot=YES
启动并设置开机自启
service vsftpd start chkconfig vsftpd on
二、负载均衡配置
2.1 Keepalived配置文件(/etc/keepalived/keepalived.conf)
global_defs { router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.99.0.145 } track_script { chk_ftp } } virtual_server 10.99.0.145 21 { delay_loop 6 lb_algo wlc lb_kind DR persistence_timeout 50 protocol TCP real_server 10.99.0.246 21 { weight 1 MISC_CHECK { misc_path "/usr/local/bin/check_ftpget.pl -H 10.99.0.246 -u ftpuser -p password" misc_timeout 10 } } real_server 10.99.0.247 21 { weight 1 MISC_CHECK { misc_path "/usr/local/bin/check_ftpget.pl -H 10.99.0.247 -u ftpuser -p password" misc_timeout 10 } } }
>创建健康检查脚本 (/usr/local/bin/check_ftpget.pl)
#!/usr/bin/perl use strict; use Net::FTP; my $host = shift @ARGV; my $user = shift @ARGV; my $pass = shift @ARGV; my $ftp = Net::FTP->new($host) or die "Cannot connect to $host: $@"; $ftp->login($user, $pass) or die "Cannot login ", $ftp->message; my $ret = $ftp->list; $ftp->quit; if ($ret) { exit(0); # Success } else { exit(1); # Failure }
2 IPVSAdm配置及验证
添加FTP服务到LVS中 ipvsadm -A -t 10.99.0.145:21 -s rr ipvsadm -a -t 10.99.0.145:21 -r 10.99.0.246:21 -m wlc -w 1 ipvsadm -a -t 10.99.0.145:21 -r 10.99.0.247:21 -m wlc -w 1
三、FTP备份脚本配置
3.1 创建备份脚本 (/usr/local/bin/ftpbackup.sh)
#!/bin/bash backup 4738 web site; TARFILE="/var/www/4738.com_web_$(date +%Y%m%d).tar.gz" ftpuser="ftpuser" ftppassword="ftppassword" ftpserver="8.8.8.8" #FTP服务器的IP地址 ftpdir="/4738.com" #备份到FTP服务器的目录; tar -czvf $TARFILE --exclude=uploads /var/www/4738 ftp -n <<EOF open $ftpserver user $ftpuser $ftppassword binary cd $ftpdir lcd /var/www prompt put $TARFILE close bye EOF
3.2 添加脚本到计划任务 (crontab -e)
每天凌晨2点执行备份脚本 0 2 * * * /usr/local/bin/ftpbackup.sh
四、测试与监控
1 测试备份脚本手动执行
/usr/local/bin/ftpbackup.sh
4.2 查看Keepalived状态和日志
service keepalived status tail -f /var/log/keepalived.log
到此,以上就是小编对于“服务器负载均衡ftp备份脚本”的问题就介绍到这了,希望介绍的几点解答对大家有用,有任何问题和不懂的,欢迎各位朋友在评论区讨论,给我留言。
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/681606.html