174540985.jpg

检查kernel是否已经支持LVS的IPVS模块modprobe -l|grep ipvs

[root@Cacti-nagios-05 ~]# modprobe -l|grep ipvs
/lib/modules/2.6.18-238.12.1.el5/kernel/net/ipv4/ipvs/ip_vs.ko
/lib/modules/2.6.18-238.12.1.el5/kernel/net/ipv4/ipvs/ip_vs_dh.ko
/lib/modules/2.6.18-238.12.1.el5/kernel/net/ipv4/ipvs/ip_vs_ftp.ko
/lib/modules/2.6.18-238.12.1.el5/kernel/net/ipv4/ipvs/ip_vs_lblc.ko
/lib/modules/2.6.18-238.12.1.el5/kernel/net/ipv4/ipvs/ip_vs_lblcr.ko
/lib/modules/2.6.18-238.12.1.el5/kernel/net/ipv4/ipvs/ip_vs_lc.ko
/lib/modules/2.6.18-238.12.1.el5/kernel/net/ipv4/ipvs/ip_vs_nq.ko
/lib/modules/2.6.18-238.12.1.el5/kernel/net/ipv4/ipvs/ip_vs_rr.ko
/lib/modules/2.6.18-238.12.1.el5/kernel/net/ipv4/ipvs/ip_vs_sed.ko
/lib/modules/2.6.18-238.12.1.el5/kernel/net/ipv4/ipvs/ip_vs_sh.ko
/lib/modules/2.6.18-238.12.1.el5/kernel/net/ipv4/ipvs/ip_vs_wlc.ko
/lib/modules/2.6.18-238.12.1.el5/kernel/net/ipv4/ipvs/ip_vs_wrr.ko

安装IPVS

wget \http://www.linuxvirtualserver.org/software/ipvs.html

tar -zxvf ipvsadm-1.24.tar.gz

cd ipvsadm-1.24

make

make install

注意:执行make时报错

libipvs.h:14:23 error: net/ip_vs.h No such file or directory

这是由于编译程序找不到对应内核造成的

解决:ln -s /usr/src/kernels/2.6.18-92.el5-i686 /usr/src/linux

也可通过yum安装ipvsadm 

yum install -y ipvsadm

然后执行 ipvsadm --help

如看到帮助提示,表明IPVS安装成功

查看路由:ipvsadm -ln , ip ad

lvs脚本RealServer脚本

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
VIP_1=192.168.188.1
sbin/ifconfig lo:1 $VIP_1 netmask 255.255.255.255 up
/sbin/route add -host $VIP_1 dev lo:1

安装Keepalived

tar zxvf keepalived-1.1.15.tar.gz

cd keepalived-1.1.15
./configure && make && make install

LVS主keepalived配置

! Configuration File for keepalived
global_defs {
 router_id LVS_DEVEL
}
vrrp_instance VI_1 {
 state MASTER
 interface eth0
 virtual_router_id 51
 priority 100
 advert_int 3
 authentication {
 auth_type PASS
 auth_pass 1
 }
 virtual_ipaddress {
 192.168.188.1
 }
}

virtual_server 192.168.188.1 80 {
 delay_loop 6
 lb_algo wlc
 lb_kind DR
 persistence_timeout 0
 protocol TCP

 real_server 192.168.188.2 80 {
 weight 200
 TCP_CHECK {
 connect_port 80
 connect_timeout 5
 nb_get_retry 3
 delay_before_retry 5
 }
 }

 real_server 192.168.188.3 80 {
 weight 200
 TCP_CHECK {
 connect_port 80
 connect_timeout 5
 nb_get_retry 3
 delay_before_retry 5
 }
 }
}

LVS从Keeplived配置

! Configuration File for keepalived
global_defs {
 router_id LVS_DEVEL
}
vrrp_instance VI_1 {
 state BACKUP
 interface eth0
 virtual_router_id 51
 priority 50
 advert_int 3
 authentication {
 auth_type PASS
 auth_pass 1  }
 virtual_ipaddress {
 192.168.188.1
 }
}

virtual_server 192.168.188.1 80 {
 delay_loop 6
 lb_algo wlc
 lb_kind DR
 persistence_timeout 0
 protocol TCP

 real_server 192.168.188.2  80 {
 weight 200
 TCP_CHECK {
 connect_port 80
 connect_timeout 5
 nb_get_retry 3
 delay_before_retry 5
 }
 }

 real_server 192.168.188.3 80 {
 weight 200
 TCP_CHECK {
 connect_port 80
 connect_timeout 5
 nb_get_retry 3
 delay_before_retry 5
 }
 }
}

keepalived配置文件详解

global_defs {
notification_email { #指定keepalived在发生切换时需要发送email到的对象,一行一个
sysadmin@fire.loc
}
notification_email_from Alexandre.Cassen@firewall.loc #指定发件人
smtp_server localhost #指定smtp服务器地址
smtp_connect_timeout 30 #指定smtp连接超时时间
router_id LVS_DEVEL #运行keepalived机器的一个标识
}
vrrp_sync_group VG_1{ #监控多个网段的实例
group {
inside_network #实例名
outside_network
}
notify_master /path/xx.sh #指定当切换到master时,执行的脚本
netify_backup /path/xx.sh #指定当切换到backup时,执行的脚本
notify_fault "path/xx.sh VG_1" #故障时执行的脚本
notify /path/xx.sh
smtp_alert #使用global_defs中提供的邮件地址和smtp服务器发送邮件通知
}
vrrp_instance inside_network {
state BACKUP #指定那个为master,那个为backup,如果设置了nopreempt这个值不起作用,主备考priority决


interface eth0 #设置实例绑定的网卡
dont_track_primary #忽略vrrp的interface错误(默认不设置)
track_interface{ #设置额外的监控,里面那个网卡出现问题都会切换
eth0
eth1
}
mcast_src_ip #发送多播包的地址,如果不设置默认使用绑定网卡的primary ip
garp_master_delay #在切换到master状态后,延迟进行gratuitous ARP请求
virtual_router_id 50 #VPID标记
priority 99 #优先级,高优先级竞选为master
advert_int 1 #检查间隔,默认1秒
nopreempt #设置为不抢占 注:这个配置只能设置在backup主机上,而且这个主机优先级要比另外一台高
preempt_delay #抢占延时,默认5分钟
debug #debug级别
authentication { #设置认证
auth_type PASS #认证方式
auth_pass 111111 #认证密码
}
virtual_ipaddress { #设置vip
192.168.202.200
}
}
virtual_server 192.168.202.200 23 {
delay_loop 6 #健康检查时间间隔
lb_algo rr #lvs调度算法rr|wrr|lc|wlc|lblc|sh|dh
lb_kind DR #负载均衡转发规则NAT|DR|RUN
persistence_timeout 5 #会话保持时间
protocol TCP #使用的协议
persistence_granularity <NETMASK> #lvs会话保持粒度
virtualhost <string> #检查的web服务器的虚拟主机(host:头)
sorry_server<IPADDR> <port> #备用机,所有realserver失效后启用
real_server 192.168.200.5 23 {
weight 1 #默认为1,0为失效
inhibit_on_failure #在服务器健康检查失效时,将其设为0,而不是直接从ipvs中删除
notify_up <string> | <quoted-string> #在检测到server up后执行脚本
notify_down <string> | <quoted-string> #在检测到server down后执行脚本

TCP_CHECK {
connect_timeout 3 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 23 健康检查的端口的端口
bindto <ip>
}
HTTP_GET | SSL_GET{
url{ #检查url,可以指定多个
path /
digest <string> #检查后的摘要信息
status_code 200 #检查的返回状态码
}
connect_port <port>
bindto <IPADD>
connect_timeout 5
nb_get_retry 3
delay_before_retry 2
}

SMTP_CHECK{
host{
connect_ip <IP ADDRESS>
connect_port <port> #默认检查25端口
bindto <IP ADDRESS>
}
connect_timeout 5
retry 3
delay_before_retry 2
helo_name <string> | <quoted-string> #smtp helo请求命令参数,可选
}
MISC_CHECK{
misc_path <string> | <quoted-string> #外部脚本路径
misc_timeout #脚本执行超时时间
misc_dynamic #如设置该项,则退出状态码会用来动态调整服务器的权重,返回0 正常,不修改;返回1,

检查失败,权重改为0;返回2-255,正常,权重设置为:返回状态码-2
}
}

启动Keepalived

/usr/local/sbin/keepalived -D -f /usr/local/etc/keepalived/keepalived.conf

高可用性能测试

停掉主LVS上的Keepalived 备LVS接管VIP

查看备LVS日志

tail -f /var/log/messages

停掉其中一台RealServer LVS检测到该节点不可用故剔除