keepalived+lvs高可用框架双机热备加负载均衡

发布于 2020-09-16  478 次阅读


首先准备4台虚拟主机:
1.192.168.232.203 keepalived(master)+lvs
2.192.168.232.204 keepalived(backup)+lvs
3.192.168.232.205 web+realserver
4.192.168.232.206 web+realserver
vip:192.168.232.200

一.配置web段机器

首先在192.168.232.205,192.168.232.206上安装 nginx
关闭防火墙(iptables firewalld),和selinux 如果nginx 可以访问就行下一步

2.在192.168.232.205,192.168.232.206运行realserver,
新建 vi /etc/ini.d/realserver 添加如下内容

#!/bin/bash
# description: Config realserver lo and apply noarp
#Written by :NetSeek http://www.linuxtone.org
SNS_VIP=192.168.232.200
. /etc/rc.d/init.d/functions
case "$1" in
start)
       ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
       /sbin/route add -host $SNS_VIP dev lo:0
      echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
       sysctl -p >/dev/null 2>&1
       echo "RealServer Start OK"
       ;;
stop)
       ifconfig lo:0 down
       route del $SNS_VIP >/dev/null 2>&1
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       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/all/arp_announce
       echo "RealServer Stoped"
       ;;
*)
       echo "Usage: $0 {start|stop}"
       exit 1
esac
exit 0

chmod 755 /etc/init.d/realserver
service realserver start 以上realserver 的配置需要在205,206都添加
以上web机器就配置完了,我们需要检查ip addr或者ifconfig,出现如图的显示就说明配置正确了

二.配置keepalived

在192.168.232.203,192.168.232.204上安装 keepalived+lvs
一样需要关闭防火墙(iptables firewalld),yum -y install keepalived ipvsadm
配置keepalived 现在都是用keepalived来管理lvs(负载均衡)的。如下是keepalived 的配置