欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

LVS负载均衡+DR模式+Keepalived

程序员文章站 2024-03-21 17:41:16
...

Keepalived原理剖析

  • Keepalived采用VRRP热备份协议实现Linux服务器的多机热备功能VRRP,虚拟路由冗余协议,是针对路由器的一种备份解决方案
  • 由多台路由器组成的一个热备组,通过共用的虚拟ip地址对外提供服务。
  • 每个热备组内同一时刻只有一台主路由器提供服务,其他路由处于冗余状态。
  • 若当前在线的路由器失效,则其他路由器会根据设置的优先级自动接替虚拟ip地址。

Keepalived配置文件介绍

  1. Keepalived配置目录位于/etc/keepalived中
  2. Keepalived.conf是主配置文件
  • global_defs{…}区段指定全局参数
  • vrrp_instance实例名称{…}区段指定VRRP热备参数
  • 注释文字”!”符号开头
  • 目录samples/,提供了许多配置样例作为参考
  1. 常用配置选项讲解(配置master)
  • router_id HA_TEST_R1:本路由器的名称
  • vrrp_instance VI_1:定义VRRP热备实例
  • interface en33:承载VIP地址的物理接口
  • virtual_router_id 1:虚拟路由器的ID号,每个热备组保持一致
  1. 配置slave服务器
  • router_id:设为自有名称
  • state:设为BACKUP
  • priority:值低于主服务器
  • 其它选项与master相同

实验操作

LVS(master)192.168.100.20
LVS(slave)192.168.100.30
Web1(Apache)192.168.100.100
Web2(Apache)192.168.100.200

LVS+DR配置

  1. LVS服务器上安装软件包
yum install ipvsadm keepalived -y
  1. 配置路由数据包转发功能,关闭重定向功能
vim /etc/sysctl.conf
#添加以下内容
net.ipv4.ip_forward=1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0

sysctl -p

LVS负载均衡+DR模式+Keepalived
3. 配置虚拟回环网卡功能

cd /etc/sysconfig/network-scripts/
cp -p ifcfg-lo ifcfg-lo:0
vim ifcfg-lo:0 
#添加以下内容
DEVICE=lo:0
IPADDR=192.168.100.10
NETMASK=255.255.255.0
ONBOOT=yes

ifup lo:0 #稍后启动
  1. 置ipvsadm启动脚本
vim /etc/init.d/dr.sh
#!/bin/bash
GW=192.168.100.1
VIP=192.168.100.10
RIP1=192.168.100.100
RIP2=192.168.100.200

case "$1" in
start)
     /sbin/ipvsadm --save > /etc/sysconfig/ipvsadm
     systemctl start ipvsadm
     /sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
     /sbin/route add -host $VIP dev ens33:0
     /sbin/ipvsadm -A -t $VIP:80 -s rr
     /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
     /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
     echo "ipvsadm starting---------------------[OK]"
     ;;
stop)
     /sbin/ipvsadm -C
     systemctl stop ipvsadm
     ifconfig ens33:0 down
     route del $VIP
     echo "ipvsadm stoped---------------------[OK]"
     ;;
status)
     if [ ! -e /var/lock/subsys/ipvsadm ]
     then
          echo "ipvsadm stoped--------------------"
     exit 1
     else
          echo "ipvsadm running--------------------" 
     fi
     ;;
*)
     echo "Usage:$0 {start|stop|status}"
     exit 1
     esac
     exit 0
chmod +x dr.sh
service dr.sh start

LVS负载均衡+DR模式+Keepalived

  1. slave与master的LVS+DR配置一样(通过scp拷贝脚本)
    LVS负载均衡+DR模式+Keepalived

  2. 安装Apache服务器

yum install httpd -y
  1. 配置站点文件
cd /var/www/html/
vim index.html
<h1>This is Alice web !</h1>    (web1中配置)
<h1>This is Bob web !</h1>      (web2中配置)
  1. 编写服务启动脚本
cd /etc/init.d/
vim web.sh
#!/bin/bash
VIP=192.168.100.10

case "$1" in
    start)
          ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
          /sbin/route add -host $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 $VIP /dev/null 2>&1
          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
          echo "RealServer Start OK"
          ;;
     *)
          echo "Usage:$0 {start|stop}"
          exit 1
     esac
     exit 0
chmod +x web.sh
service web.sh start

LVS负载均衡+DR模式+Keepalived

  1. 本机测试
    输入127.0.0.1
    LVS负载均衡+DR模式+Keepalived
    LVS负载均衡+DR模式+Keepalived

Keepalived配置

  1. 编写Keepalived主配置文件
vim /etc/keepalived/keepalived.conf
!删掉原来的配置文件,复制此段
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_01
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 10
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.100.10
    }
}

virtual_server 192.168.100.10 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.100.100 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.100.200 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
slave服务器需修改
router_id LVS_02
state BACKUP
priority 90
  1. 启动服务
systemctl start keepalived.service 
systemctl stop firewalld.service
setenforce 0
  1. 测试功能
    关闭master服务器的服务(使其宕机)
    输入VIP:192.168.100.10
    LVS负载均衡+DR模式+Keepalived
    LVS负载均衡+DR模式+Keepalived
    验证完成!