asfsd 发表于 2018-12-31 12:47:54

高可用载均衡之LVS+Keepalived

高可用负载均衡之LVS+Keepalived
  
  一、LVS+Keepalived介绍:
      LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器,通过LVS提供的负载均衡技术和Linux操作系统实现一个高性能、高可用的服务器群集,它具有良好可靠性、可扩展性和可操作性。从而以低廉的成本实现最优的服务性能。
  
      Keepalived就是一个类似于3~5层交换机制的软件,具有3~5层交换功能,其主要作用是检测web服务器的健康状态,如果某台web服务器故障,Keepalived将检测到并将其从系统中剔除,当该web服务器工作正常后Keepalived自动将其加入到服务器群中,这些工作全部自动完成,而不需要人工干预,只需要人工修复故障的web服务器即可。
  

  二、搭建LVS(DR模式)+ Keepalived高可用
  1. 服务器IP规划:
  Master DIP(192.168.126.1) VIP(192.168.126.5)
  Backup DIP(192.168.126.2) VIP(192.168.126.5)
  RIP (192.168.126.3) VIP(192.168.126.5)
  RIP (192.168.126.3) VIP(192.168.126.5)
  2.MASTER配置部分:
      编译安装LVS:Make && make install
      报错:make: *** Error 1
                        make: *** Error 2
      解决:yum installlibnl*
      编译安装Keepalived
      ./configure–prefix=/usr/local/keepalived
  cp/usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
  cp/usr/local/etc/sysconfig/keepalived /etc/sysconfig/
  cp/usr/local/keepalived/sbin/keepalived /usr/sbin/
     mkdir /etc/keepalived
     cp /usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/
  配置在主负载均衡服务器上配置keepalived.conf
    #vi /etc/keepalived/keepalived.conf (master)
    ! Configuration File for keepalived
    global_defs {
    notification_email {
    root@localhost
   }
   notification_email_from keepalived@localhost
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id LVS_DEVEL
}
    vrrp_instance VI_1 {
      state MASTER
      interface eth0
      virtual_router_id 51
      priority 101
      advert_int 1
      authentication {
            auth_type PASS
            auth_pass 1111
    }
      virtual_ipaddress {
            192.168.126.132/32 dev eth0 label eth0:0
    }
}
      virtual_server 192.168.126.132 80 {
        delay_loop 6    #(每隔6秒查询realserver状态)
        lb_algo rr      #(lvs 算法)
        lb_kind DR      #(Direct Route)
        nat_mask 255.255.255.0 #(同一IP的连接60秒内被分配到同一台realserver)
        protocol TCP    #(用TCP协议检查realserver状态)
  

      real_server 192.168.126.131 80 {
        weight 1      #(权重)
        HTTP_GET {
        url {
        path /
        status_code 200
      }
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
      }
  }
      real_server 192.168.126.149 80 {
        weight 1
        HTTP_GET {
        url {
        path /
        status_code 200
  }
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        }
      }
  }
  

      ipvsadm–C 清除所有规则
      ipvsadm-A -t 192.168.126.1132:80 -s wlc
      ipvsadm-a -t 192.168.126.132:80 -r 192.168.126.131 -g -w 2
      ipvsadm-a -t 192.168.126.132:80 -r 192.168.126.149 -g -w 1
      ipvsadm-L –n 查看规则
      ipvsadm–S 保存规则
  
     3.BACKUP配置部分:
      编译安装LVS:Make && make install
      报错:make: *** Error 1
                        make: *** Error 2
      解决:yum installlibnl*
      编译安装Keepalived
      ./configure–prefix=/usr/local/keepalived
  cp/usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
  cp/usr/local/etc/sysconfig/keepalived /etc/sysconfig/
  cp/usr/local/keepalived/sbin/keepalived /usr/sbin/
     mkdir /etc/keepalived
     cp /usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/
     配置在主负载均衡服务器上配置keepalived.conf
    #vi /etc/keepalived/keepalived.conf (备调度器Backup)
    ! Configuration File for keepalived
    global_defs {
    notification_email {
    root@localhost
       }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
    vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      192.168.2.170/32 eth0 label eth0:0
    }
}
      virtual_server 192.168.126.13280 {
        delay_loop 6
        lb_algo rr
        lb_kind DR
        nat_mask 255.255.255.0
        protocol TCP
  

      real_server 192.168.126.131 80 {
        weight 1
        HTTP_GET {
        url {
        path /
        status_code 200
      }
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
      }
  }
      real_server 192.168.126.149 80 {
        weight 1
        HTTP_GET {
        url {
        path /
        status_code 200
      }
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        }
      }
  }
  

      ipvsadm–C 清除所有规则
      ipvsadm -A -t 192.168.126.1132:80 -s wlc
      ipvsadm -a -t 192.168.126.132:80 -r 192.168.126.131 -g -w 2
      ipvsadm -a -t 192.168.126.132:80 -r 192.168.126.149 -g -w 1
      ipvsadm -L –n 查看规则
      ipvsadm–S 保存规则
  
   4.Real-Server1配置部分:
        Echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
        Echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        Echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_arp_announce
        Echo 2 > /proc/sys/net/ipv4/conf/all/arp_arp_announce
      限定linux主机对arp广播请求的响应级别,以及向外通告自己的IP地址的,通告级别!
      配置虚拟IP:
ifconfiglo:0 192.168.100.1(VIP) netmask 255.255.255.255 broadcast 192.168.1.255(广播地址) up
      Routeadd –host ip(vip) dev eth0:0
        RS上要做路由,要是不做路由响应客户端时会用eth0响应,而不是VIP
        Service httpdrestart
        Echo “this isreal-server1” > /var/www/html/index.html
      开启路由转发
      5.Real-Server2配置部分:
      Echo 1 >/proc/sys/net/ipv4/conf/eth0/arp_ignore
      Echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore
      Echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_arp_announce
      Echo 2 > /proc/sys/net/ipv4/conf/all/arp_arp_announce
      限定linux主机对arp广播请求的响应级别,以及向外通告自己的IP地址的,通告级别!
      配置虚拟IP:
ifconfiglo:0 192.168.100.1(VIP) netmask 255.255.255.255 broadcast 192.168.1.255(广播地址) up
      Routeadd –host ip(vip) dev eth0:0
      RS上要做路由,要是不做路由响应客户端时会用eth0响应,而不是VIP
        Service httpdrestart
        Echo “this isreal-server1” > /var/www/html/index.html
      开启路由转发
  
  
     注意:Rlear-Server限定linux主机对arp广播请求的响应级别后在配置VIP
     Keepalived.conf配置文件定要copy到/etc/目录下
  




页: [1]
查看完整版本: 高可用载均衡之LVS+Keepalived