本文为大家介绍lvs+keepalived配置高可用的负载均衡,供大家学习参考。文章写的不错,很有实用价值。
昨天在chinaunix的论坛上看到一提问:为什么ipvsadm -L看不到后端的realsrver,我想应该是他的realserver上没有启动脚本吧!也或许是他realserver上的80端口没有开启,这里我也写下我的环境的lvs+keepalived架构的实现!目的就是用虚拟IP转发80端口和1099端口请求到后端的真实服务器。
四台机器环境:
IP地址 lvs版本 keepalived版本 系统版本 用途
192.168.15.67 1.2.4 1.1.15 redhat4.8(64位) MASTER
192.168.15.91 1.2.4 1.1.15 redhat4.8(64位) BACKUP
192.168.15.93 无需安装 无需安装 redhat4.8(64位) Realserver
192.168.15.94 无需安装 无需安装 redhat4.8(64位) Realserver
192.168.15.120 VIP
拓扑图:
1、lvs软件ipvsadm下载安装(主机和备机上面都必须安装)
2、keepalived下载安装(主机和备机上面都必须安装)
//注释:如果系统rhel版本低于5.0以下安装报错,这里要修改系统配置
//vi /usr/src/linux/include/linux/types.h 将下面2行红色字体用/* */注释掉
/*
typedef __u16 __bitwise __sum16;
typedef __u32 __bitwise __wsum;
*/
./configure
make
make install
3、将keepalived做成服务启动(主机和备机上面都必须安装)
4、主机上配置keeplived(配置前备份好keepalived.conf文件然后再清空即可)
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
echo “” > /etc/keepalived/keepalived.conf
增加如下内容:
! Configuration File for keepalived
global_defs {
notification_email {
zengzhunzhun@ninetowns.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER 状态是MASTER
interface eth0 监听网卡切换
virtual_router_id 51
priority 100 优先级100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.15.120 虚拟IP地址,即VIP
}
}
virtual_server 192.168.15.120 80{
delay_loop 6
lb_algo wlc
lb_kind DR DR模式
persistence_timeout 1
protocol TCP
real_server 192.168.15.94 80{
weight 1 权重,权重越高处理的请求越多
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.15.93 80{
weight 1
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
virtual_server 192.168.15.120 1099{
delay_loop 6
lb_algo wlc
lb_kind DR
persistence_timeout 1
protocol TCP
real_server 192.168.15.94 1099{
weight 1
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 1099
}
}
real_server 192.168.15.93 1099{
weight 1
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 1099
}
}
}
5、备机上配置keeplived(配置前备份好keepalived.conf文件然后再清空即可)
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
echo “” > /etc/keepalived/keepalived.conf
增加如下内容:(只有两处和主机上的不一样,下面已用红色字体标出)
! Configuration File for keepalived
global_defs {
notification_email {
zengzhunzhun@ninetowns.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP 注意状态是BACKUP
interface eth0
virtual_router_id 51
priority 99 优先级是99,比主机低
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.15.120
}
}
virtual_server 192.168.15.120 80{
delay_loop 6
lb_algo wlc
lb_kind DR
persistence_timeout 1
protocol TCP
real_server 192.168.15.94 80{
weight 1
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.15.93 80{
weight 1
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
virtual_server 192.168.15.120 1099{
delay_loop 6
lb_algo wlc
lb_kind DR
persistence_timeout 1
protocol TCP
real_server 192.168.15.94 1099{
weight 1
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 1099
}
}
real_server 192.168.15.93 1099{
weight 1
TCP_CHECK {
connect_timeout 1
nb_get_retry 3
delay_before_retry 3
connect_port 1099
}
}
}
6、两台realserver上启动脚本,脚本内容如下:
7、测试
1)启动主机和备机的keepalived服务,ip a确认是否有vip地址?
2)输入ipvsadm -Ln查看是否有如下内容输出?
ipvsadm -Ln
IP Virtual Server version 1.2.0 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 124.42.15.120:80 wlc persistent 1
-> 192.168.15.94:80 Route 1 122 4
-> 192.168.15.93:80 Route 1 124 6
TCP 124.42.15.120:1099 wlc persistent 1
-> 192.168.15.94:1099 Route 1 0 0
-> 192.168.15.93:1099 Route 1 0 0
3)停止主机的keepalived服务,备机上确认是否接管vip地址?
4)备机上输入ipvsadm -Ln查看ActiveConn和InActConn对应的值不为0?主机上反而为0了。
5)启动主机的keepalived服务,确认主机再次接管vip地址继续服务?
这个请大家在配置时自行测试吧。有问题可以进脚本学堂QQ群:161228069 进行交流。