keepalived+lvs-dr实现高可用负载均衡
实验拓扑
实验要求
- RS1与RS2地址为172.18.27.201/202
- VS1和VS2地址为172.18.27.103/200,VIP为172.18.27.254
- VS1和VS2实现lvs-dr负载均衡及高可用性,且vs为sorry sever。
-
keepalived是单主模式。
实验步骤
-
各个主机安装必备软件
##RS1与RS2安装httpd yum install -y httpd ##VS1与VS2安装keeplived和httpd yum instll keepalived httpd -y ##所有主机同步时间 ntpdate 2.cn.pool.ntp.org
-
配置RS1与RS2
RS1配置,RS1的系统为centos7.2. ##配置测试页 vim /var/www/html/index.html <h1/>R1:172.18.27.201</h1> ##启动httpd服务 systemctl restart httpd.service ##配置VIP,并关闭内核中的arp_ignore,建议写脚本 [root@localhost bin]#vi setparam.sh #!/bin/bash vip="172.18.27.254" start (){ ifconfig lo:0 $vip netmask 255.255.255.255 broadcast $vip up echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore route add -host $vip dev lo:0 } stop (){ echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce 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/lo/arp_ignore ifconfig lo:0 down } case $1 in start) start echo "arp_announce set ok" echo "arp_ignore set ok" echo "lo:0 up" ;; stop) stop echo "arp_announce set ok" echo "arp_ignore set ok" echo "lo:0 down" ;; *) echo "$0 stop|start" ;; esac
RS2配置,RS1的系统为centos7.2。 ##配置测试页 vim /var/www/html/index.html <h1/>R2:172.18.27.202</h1> ##启动httpd服务 systemctl restart httpd.service ##用RS1的脚本配置RS2
-
配置VS1和VS2
##VS1配置,VS1系统是centos6.8。 #配置sorry_server yum install -y httpd vim /var/www/html/index.html <h1/>sorry server 1:172.18.27.102</h1> 启动服务 srvice httpd start #配置keepalived vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { ##报警邮箱设置 root@localhost ##报警邮箱 } notification_email_from keepalived@localhost ##报警有哪个用户发送 smtp_server 127.0.0.1 ##报警邮箱服务器 smtp_connect_timeout 30 ##报警超时时间 vrrp_mcast_group4 224.100.27.1 ##通过组播地址,通告状态和优先级等信息 } vrrp_instance VI_1 { ##设置vrrp state MASTER ##设置主从 interface eth1 ##vrrp实例工作的网络接口 virtual_router_id 51 ##设置vrrp的id标识符,0-255 priority 100 ##设定优先级 advert_int 1 ##对外的通知时间间隔 authentication { ##设定认证方式 auth_type PASS auth_pass 1111 } track_interface { ##设定监听网络接口 eth1 } nopreempt ##设定是否为抢占和非抢占模式 virtual_ipaddress { ##定义VIP 172.18.27.254/16 dev eth1 label eth1:0 ##设定VIP在那个网络接口上 } } virtual_server 172.18.27.254 80 { delay_loop 6 ##定义服务轮询间隔,多久检查RS的健康状态 lb_algo wrr ##设定VS的调用方法 lb_kind DR ##设定lvs的调用类型 protocol TCP ##设定工作的协议 sorry_server 127.0.0.1 80 ##设定sorry_server real_server 172.18.27.201 80 { ##设定RS weight 1 ##设定权重 HTTP_GET { ##设定RS健康状态查询方法 url { 根据urlde状态码来确定RS的状态 path / status_code 200 } connect_timeout 1 ##设定检查的超时时长 nb_get_retry 3 ##设定检查的重试次数 delay_before_retry 1 ##设定检查延迟时间 } } real_server 172.18.27.202 80 { weight 2 HTTP_GET { url { path / status_code 200 } connect_timeout 1 nb_get_retry 3 delay_before_retry 1 } } } #启动服务 service keepalived start
##配置VS2,VS2的系统是centOS7.2. #配置sorry_server yum install -y httpd vim /var/www/html/index.html <h1/>sorry server 2:172.18.27.200</h1> 启动服务 systemctl start httpd #配置keepalived ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 vrrp_mcast_group4 224.100.27.1 } vrrp_instance VI_1 { state BACKUP interface eno16777736 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_interface { eno16777736 } nopreempt virtual_ipaddress { 172.18.27.254/16 dev eno16777736 label eno16777736:0 } } virtual_server 172.18.27.254 80 { delay_loop 6 lb_algo wrr lb_kind DR net_mask 255.255.0.0 protocol TCP sorry_server 127.0.0.1 80 real_server 172.18.27.201 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 1 nb_get_retry 3 delay_before_retry 1 } } real_server 172.18.27.202 80 { weight 2 HTTP_GET { url { path / status_code 200 } connect_timeout 1 nb_get_retry 3 delay_before_retry 1 } } } #启动服务 systemctl start keeplived
测试实验效果
在client测试实验效果 for i in {1..10};do curl 172.18.27.254 ;done
正常状态的测试效果
一台RS挂掉之后
两台RS都挂掉
原创文章,作者:oranix,如若转载,请注明出处:http://www.178linux.com/75627