基于keepalived的lvs负载均衡http集群

Posted fxx013

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于keepalived的lvs负载均衡http集群相关的知识,希望对你有一定的参考价值。

DR模式

4台

192.168.94.141,192.168.94.143:DR(keepalived主备)

192.168.94.129, 192.168.94.130 :RS

192.168.94.250:VIP

2台DR安装ipvsadm,keepalived ,postfix,mail

keepalive配置dr的vip为192.168.94.250,编辑脚本判断ipvsadm守护进程是否在运行,不在则判断进程挂了,关闭keep。

[root@node1 ~]# cat /script/ipvscheck.sh 
#!/bin/bash
systemctl status ipvsadm.service |grep dead
if [ $? -eq 0 ];then
    systemctl stop keepalived.service
fi
[root@node1 ~]# cat /script/notify.sh 
#!/bin/bash
VIP=$2
sendmail (){
	subject="${VIP}\'s server keepalived state is translate"
	content="`date +\'%F %T\'`: `hostname`\'s state change to master"
	echo $content | mail -s "$subject" 1252150147@qq.com
}
case "$1" in
  master)
	  systemctl status ipvsadm.service |grep dead
	  if [ $? -eq 0 ];then
	systemctl start ipvsadm.service
	fi
	sendmail
  ;;
  backup)
	  systemctl status ipvsadm.service |grep dead
	  if [ $? -ne 0 ];then
	systemctl start ipvsadm.service
	fi
  ;;
  *)
	  echo "Usage:$0 master|backup VIP"
  ;;
esac

配置dr规则,ipvsadm保存到/etc/sysconfig/ipvsadm

[root@node1 ~]# cat /etc/sysconfig/ipvsadm
-A -t 192.168.94.250:80 -s wrr
-a -t 192.168.94.250:80 -r 192.168.94.129:80 -g -w 1
-a -t 192.168.94.250:80 -r 192.168.94.130:80 -g -w 1

//keepalive会根据配置文件分配vip
[root@node1 ~]# cat /etc/keepalived/keepalived.conf 
...
vrrp_script ipvs_check {	
    script "/script/ipvscheck.sh"
    interval 5	
    weight -20
}
...
 track_script {
        ipvs_check
    }
    notify_master "/script/notify.sh master 192.168.94.141"
    notify_backup "/script/notify.sh backup 192.168.94.143"

RS端

http,net-tool安装,给lo网卡添加vip 192.168.94.250/32

//内核参数修改
[root@RS1 ~]# vim /etc/sysctl.conf
...
net.ipv4.conf.all.arp_ignore = 1 #只响应目的IP地址为接收网卡上的本地地址的arp请求
net.ipv4.conf.all.arp_announce = 2 #只向该网卡回应与该网段匹配的ARP报文
[root@RS1 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

[root@RS2 ~]# vim /etc/sysctl.conf
...
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@RS2 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
//路由指向vip
[root@RS1 ~]# route add -host 192.168.94.120/32 dev lo
[root@RS2 ~]# route add -host 192.168.94.120/32 dev lo

以上是关于基于keepalived的lvs负载均衡http集群的主要内容,如果未能解决你的问题,请参考以下文章

基于LVS负载均衡群集来实现Keepalived的部署 技术

haproxy+keepalived实现高可用负载均衡

HaProxy + Keepalived 实现高可用负载均衡

haproxy+keepalived实现高可用负载均衡

高可用负载均衡:LVS + Keepalived

基于keepalived实现lvs故障切换及mysql集群负载均衡初探