Lvs+Keepalived高可用负载均衡配置
环境介绍:
vip=192.168.3.80 (负载均衡虚拟ip)
lvs+keepalived_master eth0:172.20.27.81 eth1:192.168.3.81 (1核1G)
lvs+keepalived_back eth0:172.20.27.82 eth1:192.168.3.82 (1核1G)
web01 eth0:172.20.27.83 eth1:192.168.3.83 (1核1G)
web02 eth0:172.20.27.84 eth1:192.168.3.84 (1核1G)
一、在(lvs主备节点)安装
1.安装依赖包:
yum install -y gcc gcc-c++ makepcre pcre-devel kernel-devel openssl-devel libnl-devel popt-devel popt-static modprobe -l | grep ipvs //检查linux内核是否集成lvs模块: echo "1" >/proc/sys/net/ipv4/ip_forward //开启路由转发功能:
2.安装ipvsadm
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz tar zxvf ipvsadm-1.26.tar.gz cd ipvsadm-1.26 make && make install
3.安装keepalived
3.1安装配置keepalived
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz tar zxvf keepalived-1.2.15.tar.gz cd keepalived-1.2.15 ./configure --prefix=/usr/local/keepalived make && make install
3.2将keepalived配置成系统服务
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ mkdir /etc/keepalived/ cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
3.3修改keepalived配置文件
3.3.1修改lvs+keepalived_master的keepalived配置文件
echo '' >/etc/keepalived/keepalived.conf //清空配置文件,自己写 vim /etc/keepalived/keepalived.conf
! Configuration File forkeepalived global_defs { notification_email { [email protected] #故障接受联系人 } notification_email_from [email protected] #故障发送人 smtp_server 127.0.0.1 #本机发送邮件 smtp_connect_timeout 30 router_id LVS_MASTER #lvs+keepalived_back上修改为LVS_BACKUP } vrrp_instance VI_1 { state MASTER #lvs+keepalived_back上修改为BACKUP interface eth1 virtual_router_id 51 #虚拟路由标识,主从相同 priority 100 #lvs+keepalived_back上修改为90 advert_int 1 authentication { auth_type PASS auth_pass 1111 #主从认证密码必须一致 } virtual_ipaddress { #Web虚拟IP(VTP) 192.168.3.80 } } virtual_server 192.168.3.80 80 { #定义虚拟IP和端口 delay_loop 6 #检查真实服务器时间,单位秒 lb_algo wlc #设置负载调度算法,wlc为加权最少链接(Weighted Least Connections)比较多 lb_kind DR #设置LVS负载均衡DR模式 persistence_timeout 60 #同一IP的连接60秒内被分配到同一台真实服务器 protocol TCP #使用TCP协议检查realserver状态 real_server 192.168.3.83 80 { #第一个web服务器 weight 3 #节点权重值 TCP_CHECK { #健康检查方式 connect_timeout 3 #连接超时 nb_get_retry 3 #重试次数 delay_before_retry 3 #重试间隔/S } } real_server 192.168.3.84 80 { #第二个web服务器 weight 3 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
3.3.2修改lvs+keepalived_back的keepalived配置文件
echo '' >/etc/keepalived/keepalived.conf //清空配置文件,自己写 vim /etc/keepalived/keepalived.conf
! Configuration File forkeepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_BACKUP } vrrp_instance VI_1 { state BACKUP interface eth1 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress 192.168.3.80 } } virtual_server 192.168.3.80 80 { delay_loop 6 lb_algo wlc lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.3.83 80 { weight 3 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.3.84 80 { weight 3 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
3.4 启动keepalived(lvs主备节点都启动)
service keepalived restart echo 'service keepalived start' >>/etc/rc.local
二、分别在两台Web(web01和web02)服务器编写脚本并启动
vim /etc/init.d/real.sh #!/bin/bash #description : start realserver VIP=192.168.3.80 //keepalived虚拟ip . /etc/init.d/functions case "$1" in start) /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 echo "LVS RealServer Start OK" ;; stop) /sbin/ifconfig lo:0 down 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 "LVS RealServer Stoped OK" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac
chmod +x /etc/init.d/real.sh /etc/init.d/real.sh start echo "/etc/init.d/real.sh start" >> /etc/rc.local
三、测试及常用命令
常用命令:
ipvsadm -ln #显示集群中服务器ip信息 ip addr #显示VTP绑定在哪个服务器上 tail -f /var/log/messger (从日志中可知,主机出现故障后,备机立刻检测到,此时备机变为MASTER角色,并且接管了主机的虚拟IP资源,最后将虚拟IP绑定在eth1设备上)