Lvs+keepalived实现负载均衡、故障剔除(DR模式)

简介: 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。 http://antsw.blog.51cto.com/936097/214975
Node1: 192.168.1.253
Node2: 192.168.1.254
VIP: 192.168.1.250
 
环境 centos5.2(我这里是用两台机实现的)
1、安装ipvsadm
yum -y install ipvsadm
 
2、安装keepalived
[root@node1 ~]# wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
[root@node1 ~]# tar zxvf keepalived-1.1.15.tar.gz
[root@node1 ~]# cd keepalived-1.1.15
[root@node1 keepalived-1.1.15]# ./configure --prefix=/ --mandir=/usr/local/share/man/ --with-kernel-dir=/usr/src/kernels/2.6.18-92.el5-i686/
configure后会输入这结果为正确
Keepalived configuration
------------------------
Keepalived version       : 1.1.15
Compiler                 : gcc
Compiler flags           : -g -O2
Extra Lib                : -lpopt -lssl -lcrypto
Use IPVS Framework       : Yes    #支持lvs
IPVS sync daemon support : Yes  
Use VRRP Framework       : Yes
Use LinkWatch            : No
Use Debug flags          : No
 
[root@node1 keepalived-1.1.15]# make && make install
到此,lvs+keepalived安装完成。但是还不能使用lvs功能,接下来以dr模式配置lvs+keepalived
 
3、[root@node1 ~]# cd /etc/keepalived/
[root@node1 keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
    shanswei@gmail.com
   }
   notification_email_from  shanswei@gmail.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
vrrp_instance VI_1 {
    state MASTER      ##lvs设置成master
    interface eth0
    virtual_router_id 51
    priority 102     ##lvs设置成 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       192.168.1.250 ## 如多个vip继续换行填写。
    }
}
virtual_server 192.168.1.250 80 {
    delay_loop 6
    lb_algo wlc
    lb_kind DR
    persistence_timeout 50
    protocol TCP
 
    real_server 192.168.1.253 80 {
        weight 3
        TCP_CHECK {
        connect_port 80
        connect_timeout 30
        }
    }
    real_server 192.168.1.254 80 {
        weight 1
        TCP_CHECK {
        connect_port 80
        connect_timeout 30
        }
    }
 
好了,keepalived配置完成了。
启动keepalived  
[root@node1 ~]# /etc/init.d/keepalived start
如果要随系统启动,执行chkconfig --add keepalived 
                                       chkconfig keepalived  on
 
4、接着配置真实服务器,这里通过脚本来实现吧(这个脚本只在WEB机上执行,我现在是用两台机。服务器既充当lvs、也充当web)
 [root@node1 keepalived]# vim realserver
#!/bin/bash
#description : start realserver
VIP=192.168.1.250
/etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of REALServer"
/sbin/ifconfig lo:0 $VIP broadcast $VIP 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
[root@node1 keepalived]# chmod +x realserver
[root@node1 keepalived]# ./realserver start
 
5、启动keepalived (附启动日志)
[root@node1 ~]# /etc/init.d/keepalived start  (LVS)
Oct 15 13:59:36 vmtest Keepalived: Starting Keepalived v1.1.15 (10/15,2009)
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Using MII-BMSR NIC polling thread...
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Netlink reflector reports IP 192.168.1.253 added
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.253 added
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Registering Kernel netlink reflector
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Registering Kernel netlink command channel
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Configuration is using : 11891 Bytes
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Activating healtchecker for service [192.168.1.253:80]
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Activating healtchecker for service [192.168.1.254:80]
Oct 15 13:59:36 vmtest Keepalived: Starting Healthcheck child process, pid=28419
Oct 15 13:59:36 vmtest Keepalived_vrrp: Using MII-BMSR NIC polling thread...
Oct 15 13:59:36 vmtest Keepalived_vrrp: Netlink reflector reports IP 192.168.1.253 added
Oct 15 13:59:36 vmtest Keepalived: Starting VRRP child process, pid=28420
Oct 15 13:59:36 vmtest Keepalived_vrrp: Netlink reflector reports IP 10.0.0.253 added
Oct 15 13:59:36 vmtest Keepalived_vrrp: Registering Kernel netlink reflector
Oct 15 13:59:36 vmtest Keepalived_vrrp: Registering Kernel netlink command channel
Oct 15 13:59:36 vmtest Keepalived_vrrp: Registering gratutious ARP shared channel
Oct 15 13:59:36 vmtest Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Oct 15 13:59:36 vmtest Keepalived_vrrp: Configuration is using : 36278 Bytes
Oct 15 13:59:36 vmtest Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(8,9)]
Oct 15 13:59:37 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Oct 15 13:59:38 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Oct 15 13:59:38 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Oct 15 13:59:38 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.250
Oct 15 13:59:38 vmtest Keepalived_healthcheckers: Netlink reflector reports IP 192.168.1.250 added
Oct 15 13:59:38 vmtest avahi-daemon[2580]: Registering new address record for 192.168.1.250 on eth0.
Oct 15 13:59:38 vmtest Keepalived_vrrp: Netlink reflector reports IP 192.168.1.250 added
Oct 15 13:59:43 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.250
 
[root@node1 ~]# /etc/init.d/keepalived start  (LVS)
Oct 15 15:40:26 node1 Keepalived: Starting Keepalived v1.1.15 (10/12,2009)
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Using MII-BMSR NIC polling thread...
Oct 15 15:40:26 node1 Keepalived: Starting Healthcheck child process, pid=11157
Oct 15 15:40:26 node1 Keepalived: Starting VRRP child process, pid=11159
Oct 15 15:40:26 node1 Keepalived_vrrp: Using MII-BMSR NIC polling thread...
Oct 15 15:40:26 node1 Keepalived_vrrp: Registering Kernel netlink reflector
Oct 15 15:40:26 node1 Keepalived_vrrp: Registering Kernel netlink command channel
Oct 15 15:40:26 node1 Keepalived_vrrp: Registering gratutious ARP shared channel
Oct 15 15:40:26 node1 Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Oct 15 15:40:26 node1 Keepalived_vrrp: Configuration is using : 36712 Bytes
Oct 15 15:40:26 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Oct 15 15:40:26 node1 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Registering Kernel netlink reflector
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Registering Kernel netlink command channel
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Configuration is using : 12417 Bytes
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Activating healtchecker for service [192.168.1.253:80]
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Activating healtchecker for service [192.168.1.254:80]
绿色字体表示启动成功
 
6、测试,我现在停掉主lvs上面的keepalived,从lvs会出现以下日志,表示从lvs能正常接管主的lvs,从而实现lvs主从互备功能,当主lvskeepalived启动后。会自动切换回来
[root@node1 ~]# tali -f /var/log/messages
Oct 15 15:43:18 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Oct 15 15:43:19 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Oct 15 15:43:19 node1 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Oct 15 15:43:19 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.250
Oct 15 15:43:19 node1 avahi-daemon[3087]: Registering new address record for 192.168.1.250 on eth0.
Oct 15 15:43:24 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.250
 
7、测试down机,比如我这机跑的是web服务,我现在停止apache,出现以下现像
绿线画的是lvs正常,当我停止httpd的时候,再看lvs时,192.168.1.253这个IP已不在lvs表中(画红线处),因为我停掉了httpd服务。
我再启动httpd服务时,再查看lvs192.168.1.253又自动加进表中(画蓝线处),此时已实现了负载均衡、故障剔除功能。
 
8、心得
这个配置只需要两个软件就可以了,ipvsadm、keepalived,再加上一个脚本,这个脚本只在
web机上执行
还有keepalived的配置文件,在一台机上配置好了之后,直接拷到另一台机去,拷到另一台
机只需要改动两个地方
vrrp_instanceVI_1{
    stateMASTER       ##主lvs设置成master
    interfaceeth0
    virtual_router_id51
    priority102   ##从lvs设置成 99
    stateMASTER 如果是从lvs,则改成BACKUP,priority102 改为小于100.

本文出自 “山捻子” 博客,请务必保留此出处http://antsw.blog.51cto.com/936097/214975

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
6月前
|
负载均衡 应用服务中间件 Linux
企业实战(13)LVS负载均衡NAT(网络地址转换)模式实战详解(一)
企业实战(13)LVS负载均衡NAT(网络地址转换)模式实战详解(一)
|
5月前
|
负载均衡 应用服务中间件 Linux
Nginx系列教程(14) - LVS+KeepAlived+Nginx实现高性能负载均衡集群
Nginx系列教程(14) - LVS+KeepAlived+Nginx实现高性能负载均衡集群
175 0
|
3月前
|
缓存 负载均衡 应用服务中间件
【分布式技术专题】「分析Web服务器架构」Tomcat服务器的运行架构和LVS负载均衡的运行机制(修订版)
在本章内容中,我们将深入探讨 Tomcat 服务器的运行架构、LVS 负载均衡的运行机制以及 Cache 缓存机制,并提供相应的解决方案和指导。通过理解这些关键概念和机制,您将能够优化您的系统架构,提高性能和可扩展性。
205 4
【分布式技术专题】「分析Web服务器架构」Tomcat服务器的运行架构和LVS负载均衡的运行机制(修订版)
|
4月前
|
负载均衡 算法 Nacos
【Ribbon实现客户端负载均衡和故障转移】—— 每天一点小知识
【Ribbon实现客户端负载均衡和故障转移】—— 每天一点小知识
154 0
|
4月前
|
负载均衡 监控 调度
Keepalived+Lvs(dr)调度器主备配置小实验
Keepalived和LVS(Linux Virtual Server)是两个常用的开源软件,通常结合使用以提供高可用性和负载均衡的解决方案。 Keepalived是一种用于实现高可用性的软件,它可以监控服务器的健康状态,并在主服务器出现故障时自动切换到备份服务器。
95 2
|
4月前
|
负载均衡 网络协议 Linux
小白带你学习linux的keepalived+lvs和keepalived双机热备(三十七)
小白带你学习linux的keepalived+lvs和keepalived双机热备(三十七)
75 0
|
5月前
|
负载均衡 算法 网络协议
Keepalived+LVS搭建高可用负载均衡
Keepalived+LVS搭建高可用负载均衡
180 1
|
5月前
|
负载均衡 前端开发 网络协议
Keepalived+HAProxy 搭建高可用负载均衡(二)
Keepalived+HAProxy 搭建高可用负载均衡
|
5月前
|
负载均衡 算法 调度
Keepalived+HAProxy 搭建高可用负载均衡
Keepalived+HAProxy 搭建高可用负载均衡
216 0
|
6月前
|
负载均衡 应用服务中间件 nginx
71分布式电商项目 - nginx高可用以及lvs+nginx负载均衡(资料)
71分布式电商项目 - nginx高可用以及lvs+nginx负载均衡(资料)
41 0