本文主要是介绍lvs + keepalived高可用负载均衡集群双主实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目说明
环境拓扑
环境介绍
| ||
环境配置
后端 WEB 服务器配置
1 2 | [root@WEB-01 ~] # echo "web-01" >/var/www/html/index.html [root@WEB-01 ~] # service httpd start |
1 2 | [root@WEB-02 ~] # echo "web-02" >/var/www/html/index.html [root@WEB-02 ~] # service httpd start |
1 2 3 4 | [root@LK-01 ~] # curl 172.16.4.102 web-01 [root@LK-01 ~] # curl 172.16.4.103 web-02 |
#!/bin/bash
#
vip1=172.16.4.1
vip2=172.16.4.2interface1="lo:0"
interface2="lo:1"case $1 in
start)echo 1> /proc/sys/net/ipv4/conf/all/arp_ignoreecho 1> /proc/sys/net/ipv4/conf/lo/arp_ignoreecho 2> /proc/sys/net/ipv4/conf/all/arp_announceecho 2> /proc/sys/net/ipv4/conf/lo/arp_announceifconfig$interface1 $vip1 broadcast $vip1 netmask 255.255.255.255 upifconfig$interface2 $vip2 broadcast $vip2 netmask 255.255.255.255 uproute add-host $vip1 dev $interface1route add-host $vip2 dev $interface2;;stop)echo 0 >/proc/sys/net/ipv4/conf/all/arp_ignoreecho 0 >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho 0 >/proc/sys/net/ipv4/conf/all/arp_announceecho 0 >/proc/sys/net/ipv4/conf/lo/arp_announceifconfig$interface1 downifconfig$interface2 down;;status)if ifconfiglo:0 |grep $vip1 &> /dev/null; thenecho"ipvs1 is running."elseecho"ipvs1 is stopped."fiif ifconfiglo:1 |grep $vip2 &> /dev/null; thenecho"ipvs2 is running."else echo"ipvs2 is stopped."fi ;;*)echo"Usage: `basename $0` {start|stop|status}"exit 1
esac
[root@LK-01 ~]# yum -y install keepalived
[root@LK-02 ~]# yum -y install keepalived
[root@LK-01 ~]# vim /etc/keepalived/keepalived.conf
global_defs {router_idLVS_DEVEL
}vrrp_script chk_mt_down {script"[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" #定义一个脚本,判断down文件是否存在,存在就退出执行,不存在继续执行interval 1 #1秒检测一次weight -5 #如果down文件存在,VRRP实例优先级减5
}vrrp_instance VI_1 { #定义VRRP热备实例state MASTER #MASTER表示主节点interfaceeth 0 #承载VIP的物理接口virtual_router_id 51 #虚拟路由器的ID号priority 100 #优先级advert_int 1 #通知间隔秒数(心跳频率)authentication { #设置主备节点直接的认证信息auth_type PASS #auth_pass asdfgh #}virtual_ipaddress { #设置集群中的VIP地址172.16.4.1/32 brd 172.16.4.1 dev eth0 label eth0:0}track_script {chk_mt_down #调用上面定义的脚本,如果这里没有调用,那么上面定义的脚本是无法生效的}
}vrrp_instance VI_2 { #定义实例为HK-02的备份节点state BACKUPinterfaceeth 0virtual_router_id 52 priority 99 #设置优先级,低于主服务器 advert_int 1authentication {auth_type PASSauth_pass qwerty}virtual_ipaddress {172.16.4.2}track_script {chk_mt_down}
}virtual_server 172.16.4.1 80 { #定义一个VIP地址和端口delay_loop 6lb_algo rr #轮训算法lb_kind DR #调度类型 nat_mask 255.255.0.0
# persistence_timeout 50 #设置长连接时长,单位秒protocolTCPreal_server 172.16.4.102 80 { #定义后端RS服务器的相关信息weight 1 #设置权重HTTP_GET { #设置健康状态检测方式url { #健康检查判断后端服务器首页是否访问正常(返回200状态码),如果正常则认为服务器正常path /index.html status 200}connect_timeout 3 #连接超时nb_get_retry 3 #重试次数delay_before_retry 1 #重试时间间隔}}real_server 172.16.4.103 80 {weight 1HTTP_GET {url {path /index.htmlstatus_code 200}connect_timeout 3nb_get_retry 3 delay_before_retry 1}
}
}virtual_server 172.16.4.2 80 {delay_loop 6lb_algo rr lb_kind DRnat_mask 255.255.0.0
# persistence_timeout 50protocol TCPreal_server 172.16.4.102 80 {weight 1HTTP_GET {url {path /index.htmlstatus 200}connect_timeout 3nb_get_retry 3delay_before_retry 1}}real_server 172.16.4.103 80 {weight 1HTTP_GET {url {path /index.htmlstatus_code 200}connect_timeout 3nb_get_retry 3delay_before_retry 1}
}
}
[root@LK-02 ~]# vim /etc/keepalived/keepalived.conf
global_defs {router_idLVS_DEVEL
}vrrp_script chk_mt_down {script"[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"interval 1weight -5
}vrrp_instance VI_1 {state BACKUPinterfaceeth 0virtual_router_id 51priority 99advert_int 1authentication {auth_type PASSauth_pass asdfgh}virtual_ipaddress {172.16.4.1/32 brd 172.16.4.1 dev eth0 label eth0:0}track_script {chk_mt_down}
}
vrrp_instance VI_2 {state MASTERinterfaceeth 0virtual_router_id 52priority 100advert_int 1authentication {auth_type PASSauth_pass qwerty}virtual_ipaddress {172.16.4.2}track_script{chk_mt_down}
}virtual_server 172.16.4.1 80 {delay_loop 6lb_algo rrlb_kind DRnat_mask 255.255.0.0
# persistence_timeout 50protocol TCPreal_server 172.16.4.102 80 {weight 1HTTP_GET{url {path /index.htmlstatus_code 200}connect_timeout 3nb_get_retry 3delay_before_retry 1}
}
real_server 172.16.4.103 80 {weight 1HTTP_GET{url {path /index.htmlstatus 200}connect_timeout 3nb_get_retry 3delay_before_retry 1}}
}virtual_server 172.16.4.2 80 {delay_loop 6lb_algo rrlb_kind DRnat_mask 255.255.0.0
# persistence_timeout 50protocol TCPreal_server 172.16.4.102 80 {weight 1HTTP_GET{url {path /index.htmlstatus_code 200}connect_timeout 3 nb_get_retry 3 delay_before_retry 1 }
}
real_server 172.16.4.103 80 {weight 1HTTP_GET{url {path /index.htmlstatus 200}connect_timeout 3 nb_get_retry 3 delay_before_retry 1 }}
}
[root@LK-01 ~]# service keepalived start
[root@LK-02 ~]# service keepalived start
验证
Vip 地址和集群规则验证
[root@LK-01 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 00:0c:29:22:c5:c2 brd ff:ff:ff:ff:ff:ffinet172.16.4.100/16 brd 172.16.255.255 scope global eth0inet172.16.4.1/32 brd 172.16.4.1 scope global eth0:0 inet6fe80::20c:29ff:fe22:c5c2/64 scope linkvalid_lft forever preferred_lft forever
[root@LK-01 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags->RemoteAddress:Port ForwardWeight ActiveConn InActConn
TCP 172.16.4.1:80 rr->172.16.4.102:80 Route 1 0 0 ->172.16.4.103:80 Route 1 0 0
TCP 172.16.4.2:80 rr->172.16.4.102:80 Route 1 0 0 ->172.16.4.103:80 Route 1 0 0
[root@LK-02 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 00:0c:29:f1:dd:b2 brd ff:ff:ff:ff:ff:ffinet172.16.4.101/16 brd 172.16.255.255 scope global eth0inet172.16.4.2/32 scope global eth0inet6fe80::20c:29ff:fef1:ddb2/64 scope linkvalid_lft forever preferred_lft forever
[root@LK-02 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags->RemoteAddress:Port ForwardWeight ActiveConn InActConn
TCP 172.16.4.1:80 rr->172.16.4.102:80 Route 1 0 0 ->172.16.4.103:80 Route 1 0 0
TCP 172.16.4.2:80 rr->172.16.4.102:80 Route 1 0 0 ->172.16.4.103:80 Route 1 0 0
负载均衡验证
[root@localhost ~]# curl 172.16.4.1
web-02
[root@localhost ~]# curl 172.16.4.1
web-01
[root@localhost ~]# curl 172.16.4.1
web-02
[root@localhost ~]# curl 172.16.4.1
web-01
[root@localhost ~]# curl 172.16.4.2
web-02
[root@localhost ~]# curl 172.16.4.2
web-01
[root@localhost ~]# curl 172.16.4.2
web-02
[root@localhost ~]# curl 172.16.4.2
web-01
高可用验证
[root@LK-02 ~]# touch /etc/keepalived/down
[root@LK-01 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 00:0c:29:22:c5:c2 brd ff:ff:ff:ff:ff:ffinet172.16.4.100/16 brd 172.16.255.255 scope global eth0inet172.16.4.1/32 brd 172.16.4.1 scope global eth0:0inet172.16.4.2/32 scope global eth0inet6fe80::20c:29ff:fe22:c5c2/64 scope linkvalid_lft forever preferred_lft forever
[root@localhost ~]# curl 172.16.4.1
web-02
[root@localhost ~]# curl 172.16.4.1
web-01
[root@localhost ~]# curl 172.16.4.2
web-02
[root@localhost ~]# curl 172.16.4.2
web-01
健康检测验证
[root@WEB-02 ~]# service httpd stop
[root@LK-01 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags->RemoteAddress:Port ForwardWeight ActiveConn InActConn
TCP 172.16.4.1:80 rr->172.16.4.102:80 Route 1 0 1
TCP 172.16.4.2:80 rr->172.16.4.102:80 Route 1 0 1
[root@LK-02 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags->RemoteAddress:Port ForwardWeight ActiveConn InActConn
TCP 172.16.4.1:80 rr->172.16.4.102:80 Route 1 0 0
TCP 172.16.4.2:80 rr->172.16.4.102:80 Route 1 0 0
这篇关于lvs + keepalived高可用负载均衡集群双主实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!