LVS+Keepalived负载均衡操作手册
LVS+Keepalived负载均衡操作手册
一、LVS+Keepalived架构
信息列表:
资料下载(无法下载mailto:1013269096@qq.com):
二、LVS+Keepalived安装与配置
1、 配置环境:
操作系统:CentOS6.4 64位
软件:ipvsadm-1.26.tar.gz、keepalived-1.2.16.tar.gz
2、 安装LVS(在LVS-Master和LVS-Backup上执行)
安装前的准备:安装软件:yum install kernel-devel libnl* libpopt* popt*
新建目录:mkdir /usr/src/linux
[root@lvs-master ~]# ln -s /usr/src/kernels/2.6.32-504.16.2.el6.x86_64/ /usr/src/linux/
[root@lvs-master ~]# tar xf ipvsadm-1.26.tar.gz
[root@lvs-master ~]# cd ipvsadm-1.26
[root@lvs-master ipvsadm-1.26]# make && make install
3、安装Keepalived(在LVS-Master和LVS-Backup上执行)
[root@lvs-master ~]# tar xf keepalived-1.2.16.tar.gz
[root@lvs-master ~]# cd keepalived-1.2.16
[root@lvs-master keepalived-1.2.16]# ./configure --prefix=/usr/local/keepalived
[root@lvs-master keepalived-1.2.16]# make && make install
[root@lvs-master keepalived-1.2.16]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@lvs-master keepalived-1.2.16]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@lvs-master keepalived-1.2.16]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@lvs-master keepalived-1.2.16]#mkdir /etc/keepalived
创建服务脚本:[root@lvs-master ~]# vim /etc/rc.d/init.d/lvsdr.sh,内容如下:
#!/bin/bashVIP=192.168.0.2 #定义虚拟IP地址RIP1=192.168.0.5 #定义Real IP1地址RIP2=192.168.0.6 #定义Real IP2地址/etc/rc.d/init.d/functions #IP启动提供基础功能case "$1" instart)echo "start LVS of DirectorServer"#Set the Virtual IP Address/sbin/ifconfig eth1:0 $VIP broadcast $VIP netmask 255.255.255.255 up #在网卡eth1上绑定虚拟IP地址/sbin/route add -host $VIP dev eth1:0 #添加虚拟IP地址路由进路由表#Clear IPVS Table/sbin/ipvsadm –C #删除之前IPVS表中信息/sbin/ipvsadm -A -t $VIP:80 -s rr #配置使用RR调度算法进行负载调度/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g #配置使用DR负载均衡技术/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g#Run Lvs #启用LVS服务/sbin/ipvsadm ;;stop) echo "Close LVS Directorserver" #关闭LVS服务/sbin/ifconfig eth1:0 down/sbin/ipvsadm -C ;;*)echo "Usage: $0 {start|stop}"exit 1esac该脚本在LVS-Master和LVS-Backup上分别创建,并作如下设置:
给该文件赋予可执行权限:
[root@lvs-master ~]# chmod +x /etc/rc.d/init.d/lvsdr.sh
设置开机启动,在/etc/rc.local加入:
/etc/rc.d/init.d/lvsdr.sh start
5、配置Keepalived
创建服务脚本:[root@lvs-master ~]# vim /etc/rc.d/init.d/keepalived,内容如下:
global_defs {notification_email { 1013269096@qq.com #email 通知 } notification_email_from 1013269096@qq.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS-Master # 设置lvs的id,在一个网络内应该是唯一的,备用服务器设置为LVS-Backup}vrrp_sync_group test { #设置vrrp组group { loadbalance}}vrrp_instance loadbalance { state MASTER #设置lvs的状态,报错MASTER和BACKUP两种,必须大写,备用服务器设置为BACKUP interface eth1 #设置对外服务的接口 lvs_sync_daemon_inteface eth1 #设置lvs监听的接口 virtual_router_id 51 #设置虚拟路由表示 priority 180 #设置优先级,数值越大,优先级越高 advert_int 5 #设置同步时间间隔authentication { #设置验证类型和密码 auth_type PASS auth_pass 1111}virtual_ipaddress { #设置lvs vip 192.168.0.2}}virtual_server 192.168.0.2 80 { delay_loop 6 #健康检查时间间隔 lb_algo rr #负载均衡调度算法 lb_kind DR #负载均衡转发规则 #persistence_timeout 20 #设置会话保持时间,对bbs等很有用 protocol TCP #协议 real_server 192.168.0.5 80 { weight 3 #设置权重 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80}} real_server 192.168.0.6 80 { weight 3 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80}}}该脚本在LVS-Master和LVS-Backup上分别创建,并作如下设置:
给该文件赋予可执行权限:
[root@lvs-master ~]# chmod +x /etc/rc.d/init.d/keepalived
设置开机启动,在/etc/rc.local加入:
/etc/rc.d/init.d/keepalived start
防火墙设置(LVS-Master和LVS-Backup同样设置):
[root@lvs-master ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@lvs-master ~]# iptables -I INPUT 1 -p vrrp -j ACCEPT
[root@lvs-master ~]# service iptables save
6、RealServer服务器设置:
RealServer01设置:
[root@realserver01 ~]# yum install httpd –y
[root@realserver01 ~]# service httpd restart
[root@realserver01 ~]# echo "I am RealServer01" > /var/www/html/index.html
RealServer02设置:
[root@realserver02 ~]# yum install httpd –y
[root@realserver02 ~]# service httpd restart
[root@realserver02 ~]# echo "I am RealServer02" > /var/www/html/index.html
在两台RealServer服务器上建立脚本:
[root@realserver01 ~]# vim /etc/rc.d/init.d/realserver.sh
#!/bin/bash# description: Config realserver lo and apply noarpSNS_VIP=192.168.0.2/etc/rc.d/init.d/functionscase "$1" instart) 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 1esacexit 0
给该文件赋予可执行权限:
[root@realserver01 ~]# chmod +x /etc/rc.d/init.d/realserver.sh
设置开机启动,在/etc/rc.local加入:
/etc/rc.d/init.d/realserver.sh start
防火墙设置(RealServer01和RealServer02同样设置):
[root@realserver01 ~]# iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT
[root@realserver01 ~]# service iptables save
修改内核参数:
net.nf_conntrack_max=655360
net.netfilter.nf_conntrack_tcp_timeout_established=36000
一、在Win主机上新增环回虚拟网卡(Microsoft Loopback Adapter)
以管理员身份运行cmd后,在cmd命令窗口中执行:hdwwiz,弹出如下界面:
下一步后,勾选手动选择:
点选网络适配器,继续下一步:
左侧选择Microsoft,右侧找到如图硬件(Windows Server 2008/7中则叫:Microsoft Loopback Adapter)点击下一步开始安装,直至完成,则成功添加了环回虚拟网卡。 成功安装该适配器后,与其他任何适配器一样,您可以手动配置其选项。如果TCP/IP 属性配置为使用 DHCP,则该适配器最终将使用一个 autonet 地址 (169.254.x.x/16),因为该适配器并没有与任何物理媒体实际相连接。
注意:默认情况下,TCP/IP 属性配置为使用 DHCP。
二、设置环回网卡TCP/IP信息
打开Windows 7/8/Server 2008的网络和共享中心,左侧点开“更改适配器设置”,找到新增的环回连接,重命名为realserver(推荐重命名)。 右键realserver连接属性,开始设置TCP/IP:
和Linux中的lo:0一个样,将IP地址设置为VIP,将掩码设置为255.255.255.255,其余留空即可。
三、修改客户端网卡接口、环回接口连接模式
将以下代码保存为bat执行,或直接在CMD中依次执行2~5行命令即可(双引号中需根据实际连线名称修改)
netsh interface ipv4 set interface"realserver" weakhostreceive=enabled
netsh interface ipv4 set interface"realserver" weakhostsend=enabled
netsh interface ipv4 set interface "本地连接" weakhostreceive=enabled
netsh interface ipv4 set interface "本地连接" weakhostsend=enabled
“本地连接”和“realserver”在同一网段。