LVS+keepalive部署FTP高可用负载均衡
STEP 1 准备机器和环境
准备4台机器,这里以152-155四台机器为例(CENTOS 7.8)
-
173.1.3.152 (keepalived负载均衡 主机 MASTER)
-
173.1.3.153 (keepalived负载均衡 备机 BACKUP)
-
173.1.3.154 (FTP服务器1)
-
173.1.3.155 (FTP服务器2)
STEP 2 负载均衡主机安装
1、环境准备和关闭selinux
1)执行如下命令
2)永久关闭selinux
[root@Centos7 ~]# vim /etc/selinux/config 修改配置文件
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@Centos7 ~]# reboot 重启生效
3)查看内核版本,创建LN软链接,具体过程执行以下命令
-
[root@CentOS-7 ~]
-
[root@CentOS-7 ~]
-
(系统显示)2.6.32-358.el6.x86_64
-
[root@CentOS-7 ~]
2、安装ipvsadm
-
[root@CentOS-7 ~]
验证安装
-
[root@CentOS-7 ~]
-
显示如下:
-
***********************************************************************************
-
IP Virtual Server version 1.2.1 (size=4096)
-
Prot LocalAddress:Port Scheduler Flags
-
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
-
***********************************************************************************
-
[root@CentOS-7 ~]
-
显示如下
-
***********************************************************************************
-
ip_vs_rr 1420 0
-
ip_vs_wlc 1241 1
-
ip_vs 115643 5 ip_vs_rr,ip_vs_wlc
-
libcrc32c 1246 1 ip_vs
-
ipv6 321422 20 ip_vs,ip6t_REJECT,nf_conntrack_ipv6,nf_defrag_ipv6
3、安装keepalived
-
[root@CentOS-7 ~]
4、配置keepalived.conf
1)将keepalived.conf通过SFTP上传至/etc/keepalived
将check_ftpget.pl上传至/usr/local/bin
check_ftpget.pl文件内容如下:
[root@Centos7 ~]# cat /usr/local/bin/check_ftpget.pl
#!/usr/bin/perl -w
## Written 12/5/00 Jeremy Hanmer
# $Id: check_ftpget.pl,v 1.1 2000/12/07 14:40:44 karldebisschop Exp $
use strict;
use Net::FTP;
use Getopt::Std;
use vars qw($opt_H $opt_u $opt_p $opt_f);
getopts("H:u:p:f:");
my $host = $opt_H ||
die "usage: check_ftp.pl -h host [ ] ";
my $username = $opt_u || 'anonymous';
my $pass = $opt_p || "$ENV{'LOGNAME'}@$ENV{'HOSTNAME'}" ;
my $file = $opt_f;
my $status = 0;
my $problem;
my $output = "ftp ok";
my $ftp = Net::FTP->new("$host") ||
&crit("connect");
$ftp->login("$username", "$pass") ||
&crit("login");
$ftp->get($file) ||
&crit("get") if $file;
sub crit()
{undefined
$problem = $_[0];
$status = 2;
if ( $problem eq 'connect' ) {undefined
$output = "can't connect";
} elsif ( $problem eq 'login' ) {undefined
$output = "can't log in";
} elsif ( $problem eq 'get' ) {undefined
$output = "cant get $file";
}
}
print "$output ";
exit $status;
2)变更check_ftpget.pl文件权限(凡是遇到文件权限访问等问题,都需要变更文件权限,如本文没有提到的文件有权限访问问题,请参考这里)
3)keepalived.conf文件在这个安装手册上已经配置好了,不需要变动,但实际生产环境中可能由于IP地址不一样,需要变更一些配置。
以下我就这些配置跟大家做个说明
-
! Configuration File for keepalived
-
global_defs {
-
notification_email {
-
xxxxxx@sina.com
-
}
-
notification_email_from keepalived@localhost
-
smtp_server 127.0.0.1
-
smtp_connect_timeout 30
-
router_id LVS_DEVEL
-
}
-
-
vrrp_sync_group VGM {
-
group {
-
VI_CACHE
-
}
-
}
-
vrrp_instance VI_CACHE {
-
state MASTER
-
interface ens192
-
virtual_router_id 51
-
priority 100
-
advert_int 1
-
authentication {
-
auth_type PASS
-
auth_pass 1111
-
}
-
virtual_ipaddress {
-
173.1.3.156
-
}
-
}
-
-
virtual_server 173.1.3.156 21 {
-
delay_loop 6
-
lb_algo rr
-
lb_kind DR
-
persistence_timeout 50
-
protocol TCP
-
-
real_server 173.1.3.154 21 {
-
weight 1
-
MISC_CHECK {
-
misc_path "/usr/local/bin/check_ftpget.pl -H 173.1.3.154 -u admin -p 123457"
-
misc_timeout 10
-
}
-
}
-
real_server 173.1.3.155 21 {
-
weight 1
-
MISC_CHECK {
-
misc_path "/usr/local/bin/check_ftpget.pl -H 173.1.3.155 -u admin -p 123457"
-
misc_timeout 10
-
}
-
}
-
}
5、启动keepalived
1)直接启动:
2)添加开机自动启动
-
[root@CentOS-7 ~]
6、关闭防火墙
-
[root@CentOS-7 ~]
STEP 3 负载均衡备机安装
1、环境准备
参考STEP 2 主机配置
2、安装ipvsadm
参考STEP 2 主机配置
3、安装keepalived
参考STEP 2 主机配置
4、配置keepalived.conf
1)将keepalived_bak.conf通过SFTP上传至/etc/keepalived,并改名为keepalived.conf
将check_ftpget.pl上传至/usr/local/bin
其他步骤参考主机,备机注意keepalived.conf配置有两个地方和主机不同
state BACKUP
priority 99
5、启动keepalived
参考STEP 2 主机配置
6、关闭防火墙
参考STEP 2 主机配置
STEP 4 FTP服务器配置
1、环境准备
假设你已经安装了VSFTPD,此文中VSFTPD不是重点,安装方法在此不再赘述
2、FTP配置
1)将lvs_realserver.sh上传至FTP服务器/etc/rc.d/init.d
[root@nhmes-ftp1 ~]# cat /etc/rc.d/init.d/lvs_realserver.sh
#!/bin/bash
#description:start realserver
VIP1=173.1.3.156
/etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of RealServer"
/sbin/ifconfig lo:0 $VIP1 broadcast $VIP1 netmask 255.255.255.255 up
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
/sbin/ifconfig lo:0 down
echo "close LVS Directorserver"
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
# shell end
2)变更权限
3)启动配置脚本
马上启动:
开机自启动(后台方式):
在/etc/rc.d/rc.local中添加一行,并且给rc.local文件执行权限
-
/etc/rc.d/init.d/lvs_realserver.sh start&
-
chmod +x /etc/rc.d/rc.local
3)变更权限
STEP 5 测试
1、可以通过FTP客户端,直接连接173.1.3.156虚拟FTP服务
-
IP:173.1.3.156
-
PORT:21
-
USER:admin
-
PASS:123457
2、查看虚拟IP服务状态
在活动负载均衡机器(默认主机)上执行如下命令:
-
[root@CentOS-7 ~]
-
IP Virtual Server version 1.2.1 (size=4096)
-
Prot LocalAddress:Port Scheduler Flags
-
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
-
TCP 173.1.3.156:ftp wlc persistent 50
-
-> 173.1.3.154:ftp Route 1 1 0
-
-> 173.1.3.155:ftp Route 1 3 0
3、停止负载均衡主机服务
-
[root@CentOS-7 ~]
-
[root@CentOS-7 ~]
-
[root@CentOS-7 ~]
-
[root@CentOS-7 ~]
停止负载均衡主机,系统会自动唤醒备机,无需手工切换,自动化无人植守配置。切换时间目前配置的是1秒,但切换可能需要1-20秒不等的时间
主机恢复后,备机将自动进入休眠状态,如此反复。
4、停止负载均衡备机服务
命令参考上一条,对备机作任何变更,在主机存活期间无任何影响,但如果主机服务宕机,则会有影响,将导致虚拟FTP不可用。
5、停止FTP真机服务
停止任意一台真机服务,不会影响虚拟FTP服务,虚拟FTP服务会自动连上可以提供服务的FTP服务器,如果所有的FTP真机服务都宕机了,则虚拟FTP也将无法访问。
当真机FTP服务恢复正常后,虚拟FTP将自动添加真机服务至虚拟队列中,无人值守。
6、清除和手工添加一些虚拟IP规则
-
ipvsadm -C
-
ipvsadm -A -t 173.1.3.156:21 -s wlc
-
ipvsadm -a -t 173.1.3.156:21 -r 173.1.3.154 -g -w 1
-
ipvsadm -a -t 173.1.3.156:21 -r 173.1.3.155 -g -w 1
其他规则参考网络上ipvsadm命令用法