使用lvs实现ftp的负载均衡
操作系统:CentOS6.5_x64
问题描述
使用lvs实现ftp的负载均衡
为了使模型足够简单,这里只实现了loadblance,HA并未实现,可以借助keepalived实现。
具体实现
hostA : 192.168.1.21
hostB : 192.168.1.22
hostC : 192.168.1.23
虚拟ip地址: 192.168.1.20
hostA为负载均衡器
hostB和hostC为ftp服务器
转发模式:DR
调度算法:rr
hostA配置
安装ipvsadm:
yum install ipvsadm -y
从源码安装:
yum install -y gcc gcc-c++ make pcre pcre-devel kernel-devel openssl-devel yum install libnl* popt* wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz tar zxvf ipvsadm-1.26.tar.gz cd ipvsadm-1.26 make && make install
开启ip转发功能:
vim /etc/sysctl.conf net.ipv4.ip_forward = 1 sysctl -p
关闭防火墙:
/etc/init.d/iptables stop
配置ipvs(start.sh) :
#! /bin/sh # DR Mode ipvsadm -C ipvsadm -A -t 192.168.1.20:21 -s rr -p ipvsadm -a -t 192.168.1.20:21 -r 192.168.1.22:21 -g ipvsadm -a -t 192.168.1.20:21 -r 192.168.1.23:21 -g ipvsadm save ipvsadm -ln ifconfig eth0:0 192.168.1.20 netmask 255.255.255.0
hostB配置
配置虚拟ip:
[root@host22 test]# cat /etc/init.d/realserver.sh #!/bin/bash SNS_VIP=192.168.1.20 . /etc/rc.d/init.d/functions case "$1" in start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_VIP dev lo:0 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 sysctl -p >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) ifconfig lo:0 down route del $SNS_VIP >/dev/null 2>&1 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 "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0 [root@host22 test]# sh /etc/init.d/realserver.sh start SIOCADDRT: File exists RealServer Start OK
开启ftp服务
安装vsftpd :
yum install vsftpd
修改配置:
为了方便开启匿名用户操作ftp的各种权限。
chmod a+w /var/ftp/pub/ # 开启文件夹写权限 vim /etc/vsftpd/vsftpd.conf anon_upload_enable=YES anon_mkdir_write_enable=YES anon_other_write_enable=YES # 开启匿名用户删除功能
配置selinux:
getsebool -a | grep ftp setsebool -P allow_ftpd_anon_write 1 setsebool -P allow_ftpd_full_access 1
启动ftp:
/etc/init.d/vsftpd start
配置开机启动:
chkconfig vsftpd on
关闭防火墙:
/etc/init.d/iptables stop
chkconfig iptables off
hostC配置
配置同hostB
讨论
这里只是用lvs实现了ftp负载均衡的模型,其它具体问题请参考lvs相关文档。
这里附上测试脚本,ftp写文件脚本:
#! /usr/bin/env python #-*- coding:utf-8 -*- import ftplib,os,time ftp = ftplib.FTP("192.168.1.20") ftp.login() ftp.cwd("/pub") i = 0 while True : filename = "ftptest1_%d.txt" % i print filename i += 1 with open(filename,"w") as fout : fout.write(str(time.time())) myfile = open(filename, 'r') try : ftp.storlines('STOR ' + filename, myfile) except : ftp.login() ftp.cwd("/pub") myfile.close() os.remove(filename) time.sleep(10)
好,就这些了,希望对你有帮助。
本文github地址:
https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/20170623_使用lvs实现ftp的负载均衡.rst
欢迎补充