基于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 stop 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

posted on 2021-05-21 02:09  fxx013  阅读(65)  评论(0编辑  收藏  举报

导航