keepalive初窥及实现
Posted qingfengguoke
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalive初窥及实现相关的知识,希望对你有一定的参考价值。
实验目的:分别实现ipvs及nginx的keepalive高可用,在Master Down 状态下,Backup实现无缝对接,并实现双主双备模式;
1.实现Master与Backup之间的双vrrp虚拟地址;
2.分别实现对后端服务器的状态检测并处理机制;
3.测试负载均衡节点故障状态下的可用性;
4.测试后端服务器故障状态下的处理机制是否可用;
1. 两台 VIP设备之间vrrp 配置及后端服务器配置
1 !configuration File for keepalived 2 global_defs { (定义全局相关配置) 3 notification_email { (通告发送邮件地址) 4 root@localhost 5 } 6 notification_email_from keepalived@localhost (邮件发送者) 7 smtp_server 127.0.0.1 8 smtp_connect_timeout 30 9 router_id 192.168.20.240 (路由器id,可随意) 10 vrrp_mcast_group4 224.0.0.19 (指定VRRP设备之间通信的组播地址,一般单独定义一个) 11 } 12 vrrp_instance VI_1 { (vrrp实例配置) 13 status MASTER (该节点的vrrp角色) 14 interface ens192 (绑定的接口) 15 virtual_router_id 34 (***虚拟ID,非常重要,两台vrrp设备之间虚拟ID必须相同,有多个VRRP实例时,可以用多个不同的虚拟ID) 16 priority 100 (该VRRP设备的优先级,最高为100) 17 advert_int 1 (vrrp信息通告时间间隔) 18 authentication { (VRRP设备之间报文交互认证方式) 19 auth_type PASS 20 auth_pass 123456 (VRRP设置之间密钥需相同) 21 } 22 virtual_ipaddress { (指定虚拟IP地址) 23 192.168.20.240 dev ens192 lable ens192:1 24 } 25 notify_master "/etc/keepalived/notify.sh" (通知脚本) 26 notify_backup "/etc/keepalived/notify.sh" 27 notify_fault "/etc/keepalived/notify.sh“ 28 } 29 virtual_server 192.168.20.240 80 { (real_server全局配置) 30 delay_loop 3 (服务轮询的时间间隔) 31 lb_algo wrr (轮询方式) 32 lb_kind DR (调度算法) 33 protocol TCP (协议,仅进行TCP协议调度) 34 sorry_server 127.0.0.1 80 (错误页面返回主机) 35 real_server 192.168.20.244 80 { (定义real_server配置) 36 weight 1 (权重大小) 37 HTTP_GET { (监测方式为http_get) 38 url { 39 path / 40 status_code 200 (访问real_serverURL根路径,指定得到的返回结果为200) 41 } 42 connect_timeout 1 (连接超时时间) 43 nb_get_retry 3 (连接尝试次数) 44 delay_before_retry 1 (尝试连接之前等待时间) 45 } 46 real_server 192.168.20.245 80 { 47 weight 1 48 HTTP_GET { 49 url { 50 path / 51 status_code 200 52 } 53 connect_timeout 1 54 nb_get_retry 3 55 delay_before_retry 1 56 }
notify.sh 脚本配置
1 #!/bin/bash 2 # 3 contact=‘root@localhost‘ 4 5 notify() { 6 local mailsubject="$(hostname) to be $1 vip floating" 7 local mailbody="$(date +‘%F %T‘):VRRP transition,$(hostname) changed to be $1" 8 echo "$mailbody" | mail -s "$mailsubject" $contact 9 } 10 case $1 in 11 master) 12 notify master 13 ;; 14 backup) 15 notify backup 16 ;; 17 fault) 18 notify fault 19 ;; 20 *) 21 echo "Usage: $(basename $0) {master|backup|fault" 22 exit 1 23 ;; 24 esac
systemctl start keepalived 各VRRP节点重新启动服务
real_server 主机配置
1 echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore 2 echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore 3 echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce 4 echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce 5 ifconfig lo 192.168.20.240 netmask 255.255.255.255 broadcast 192.168.20.240 6 route add -host 192.168.20.240 dev lo
两台real_server之间配置相同, lo地址为VRRP虚拟地址;
real_server 上 http服务配置此处不做详解
测试: crul http://192.168.20.240 :得到返回结果为两台real_server轮询返回结果
停止某台主机keepalived服务,经测试,http服务正常轮询工作;
以上是关于keepalive初窥及实现的主要内容,如果未能解决你的问题,请参考以下文章