标签:keepalived lvs
背景:lvs_dr模式dr无法主动识别rs上的web服务器是否停止工作,为解决这一问题,用到keepalived,集成高可
用集群和负载均衡为一体(keepalived-->HA+LB)
环境:
DIR:172.16.115.100(主keepalived)
RS1:172.16.115.157(nginx)
RS2:172.16.115.202(nginx、备keepalived)
Cilent:172.16.115.*
DIR/RS2主机(keepalived主、备)
1. DIR/RS2上分别安装keepalived和ipvsadm
命令 yum install -y keepalived ipvsadm
2. DIR/RS2上分别置空并重新编辑keepalived配置文件vim /etc/keepalived/keepalived.conf
内容:
vrrp_instance VI_1 { state MASTER # 状态:DIR上用MASTER,RS2备用服务器上用BACKUP interface eth0 virtual_router_id 51 priority 100 # 优先级别:DIR上用100,RS2用90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.115.200 } } virtual_server 172.16.115.200 80 { delay_loop 6 # 每隔6秒查询real server的状态 lb_algo rr # lvs算法 lb_kind DR # Driect Route persistence_timeout 0 # 同一ip的连接60秒内被分配到同一台real server protocol TCP # 用tcp协议查询real server状态 real_server 172.16.115.157 80 { weight 100 # 权重为100 TCP_CHECK { connect_timeout 10 # 10秒无反应超时 nb_get_retry 3 delay_before_retry 3 connet_port 80 } } real_server 172.16.115.202 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connet_port 80 } } }
RS1/RS2(Web服务器)
1. RS1和RS2编写脚本vim /usr/local/sbin/lvs_rs.sh
内容:
#! /bin/bash #指定vip是在回环地址地址上;子网掩码全设255,表明只发不回 vip=172.16.115.200 ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up #添加路由 route add -host $vip lo:0 #调整内核参数 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
2. RS1和RS2分别启动脚本/usr/local/sbin/lvs_rs.sh,并启动niginx
说明:执行成功之后,会在回环地址网卡上生产一个虚拟ip 172.16.115.200
命令 sh /usr/local/sbin/lvs_rs.sh
命令 /etc/init.d/nginx start
3. dir和rs2上启动keepalived服务;dir上查看ipvsadm规则;ip addr查看ip(ifconfig看不到)
说明:dir和rs2(主备)都产生转发规则,主启动了虚拟ip:172.16.115.200,备没有启动(当主停掉时,备才启动)
dir上: /etc/init.d/keepalived start
rs2上: /etc/init.d/keepalived start
注:echo 1 > /proc/sys/net/ipv4/ip_forward(如无法转发,则在keepalived主、备上开启)
keepalived 主
keepalived 备
测试:
1. 停止rs1的nginx服务,客户机访问虚拟IP
命令 /etc/init.d/nginx stop
命令 curl 172.16.115.200
结果:成功跳过不能访问的rs1,rs1的转发规则也被清掉(重启nginx,又会加回来)
2. 停掉dir上的主keepalived,查看主备状态
说明:备用keepalived接管,重新启动主keepalived时,主重启接管
命令 /etc/init.d/keepalived stop
keepalived 主
keepalived 备
dir上的主keepalived停止状态下,再次停用rs1的Nginx时,客户端也成功跳过不能访问的rs1;重启rs1的Nginx,恢复正常
本文出自 “一马踏平川” 博客,请务必保留此出处http://huangzp.blog.51cto.com/12434999/1901568
标签:keepalived lvs
原文地址:http://huangzp.blog.51cto.com/12434999/1901568