keepalived + lvs 实现简单负载均衡架构
jopen
11年前
LVS提 供负载均衡,keepalived提供健康检查,故障转移,提高系统的可用性!采用这样的架构以后 很容易对现有系统进行扩展,只要在后端添加或者减少realserver,只要更改lvs的 配置文件,并能实现无缝配置变更!
实验环境: rhel 6.3 iptables && selinux diabled
实验主机:
lvs-master: 192.168.0.2
lvs-backup: 192.168.0.3
lvs-vip: 192.168.0.100
rs: 192.168.0.44 && 192.168.0.57
主备机上软件包的安装与配置:
#yum install ipvsadm openssl-devel libnl-devel gcc make -y #tar zxf keepalived-1.2.12.tar.gz #cd keepalived-1.2.12 #./configure --prefix=/usr/local/keepalived Keepalived configuration ------------------------ Keepalived version: 1.2.5 Compiler: gcc Compiler flags: -g -O2 Extra Lib: -lpopt -lssl -lcrypto -lnl Use IPVS Framework: Yes IPVS sync daemon support : Yes IPVS use libnl: Yes Use VRRP Framework: Yes Use VRRP VMAC: No SNMP support: No Use Debug flags: No #make && make install拷贝配置文件以及启动脚本
#ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ #ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ #ln -s /usr/local/keepalived/etc/keepalived /etc/ #ln -s /usr/local/keepalived/sbin/keepalived /sbin修改配置文件
#vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { xxxxxx@gmail.com #接收警报的 email 地址,可以添加多个 } notification_email_from keepalived@server2.example.com #设置邮件的发送地址 smtp_server 127.0.0.1 #设置smtp server地址 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER #备机改为 BACKUP,此状态是由 priority 的值来决定的,当前priority 的值小于备机的值,那么将会失去 MASTER 状态 interface eth0 virtual_router_id 51 #主、备机的 virtual_router_id 必须相同,取值 0-255 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { #设置虚拟 IP 地址,可以设置多个虚拟 IP 地址,每行一个 192.168.0.100 } } virtual_server 192.168.0.100 80 { #定义虚拟服务器 delay_loop 6 lb_algo rr #lvs 轮叫算法 lb_kind DR #lvs DR模式 # nat_mask 255.255.255.0 # persistence_timeout 50 protocol TCP real_server 192.168.0.44 80 { #配置服务节点 weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.0.57 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
在realserver上:
#ifconfig eth0:0 192.168.0.100 netmask 255.255.255.0 up #echo `hostname` > /var/www/html/index.html #/etc/init.d.httpd start
测试:
1. 高可用测试:停止 master 上的 keepalived 服务,看 backup 是否接管。
2. 负载均衡测试:访问 http://192.168.0.100,看到页面在两个 realserver 上切换表示成功!
你也可以通过 ipvsadm -Ln 查看详细连接情况!
3. 故障切换测试:任意关闭 realserver 上的 httpd 服务,Keepalived 监控模块是否能及时发现,
然后屏蔽故障节点,同时将服务转移到正常节点来执行。