【lvs负载均衡原理】lvs负载均衡实现机制/ipvs调度算法

更新时间:2019-02-08    来源:ling    手机版     字体:

【www.bbyears.com--ling】

八种调度算法:  
  :rr|wrr|lc|wlc|lblc|dh|sh|
  轮叫调度(Round-Robin Scheduling
  加权轮叫调度(Weighted Round-Robin Scheduling
  最小连接调度(Least-Connection Scheduling)
  加权最小连接调度(Weighted Least-Connection Scheduling)
  基于局部性的最少链接(Locality-Based Least Connections Scheduling)
  带复制的基于局部性最少链接(Locality-Based Least Connections with Replication Scheduling)
  目标地址散列调度(Destination Hashing Scheduling)
  源地址散列调度(Source Hashing Scheduling)

 

三种均衡机制:

  IP负载均衡技术是在负载调度器的实现技术中效率最高的。

三种机制分别是:

Virtual Server via Network Address Translation (VS/NAT): 也有说端口映射的,其目标是将一组服务器构成一个高性能的、高可用的虚拟服务器。因为这种技术容易形成单点故障,从而造成网路无法访问,并且存在带宽瓶颈。所以LVS又提供了下面两种实现。

Virtual Server via IP Tunneling (VS/TUN ): 通过IP隧道实现虚拟服务。

Virtual Server via Direct Routing (VS/DR) :直接路由实现虚拟服务器的方法(负载能力最强),本文搭建的就是DR模式。

 

LVS

使用Keepalived做LVS时,真实服务器(Real-Server)需要为ipvsadm单独写脚本,Windows 需环回网络接口,ipvsadm 启用并工作后,keepalived才会接管控制ipvsadm。

脚本:

 代码如下 #!/bin/bash
VIP=192.168.2.130
/etc/rc.d/init.d/functions
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 "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

 

ipvsadm

ipvsadm -l

ipvsadm -ln

ipvsadm -lnc

 

vrrp_script 配置

 代码如下

vrrp_script chk_sshd {
script "killall -0 sshd" # cheaper than pidof
interval 2 # check every 2 seconds
weight -4 # default prio: -4 if KO

# weight -40 # if failed, decrease 40 of the priority
fall 1 # require 2 failures for failures
rise 1 # require 1 sucesses for ok
}

vrrp_script chk_haproxy {
script "killall -0 haproxy" # cheaper than pidof
interval 2 # check every 2 seconds
}

vrrp_script chk_http_port {
#script "/tcp/127.0.0.1/80" # connects and exits

#script "nc -z 127.0.0.1 80" # connects and exits

script " interval 1 # check every second
weight -2 # default prio: -2 if connect fails
}

vrrp_script chk_https_port {
script "/tcp/127.0.0.1/443"
interval 1
weight -2
}

vrrp_script chk_smtp_port {
script "/tcp/127.0.0.1/25"
interval 1
weight -2
}

vrrp_instance VI_1 {
interface eth0
state MASTER
virtual_router_id 51
priority 100

virtual_ipaddress {
192.168.200.18/25
}

track_interface {
eth1 weight 2 # prio = +2 if UP
eth2 weight -2 # prio = -2 if DOWN
eth3 # no weight, fault if down
}

track_script {
chk_sshd # use default weight from the script
chk_haproxy weight 2 # +2 if process is present
chk_http_port
chk_https_port
chk_smtp_port
}
}

vrrp_instance VI_2 {
interface eth1
state MASTER
virtual_router_id 52
priority 100

virtual_ipaddress {
192.168.201.18/26
}

track_interface {
eth0 weight 2 # prio = +2 if UP
eth2 weight -2 # prio = -2 if DOWN
eth3 # no weight, fault if down
}

track_script {
chk_haproxy weight 2
chk_http_port
chk_https_port
chk_smtp_port
}
}
 

一例配置:

 代码如下

! Configuration File for keepalived

global_defs {
   notification_email {
 notify@aooshi.org
   }
   notification_email_from servername@aooshi.org
   smtp_server 192.168.2.111
   smtp_connect_timeout 30
   router_id webcache253-keepalived
}

vrrp_script check_http {
 script "        interval 5
        weight -10
}

vrrp_instance VI_158 {
    state BACKUP
    interface eth0
    virtual_router_id 62
    priority 99
    advert_int 1
    smtp_alert
    authentication {
        auth_type PASS
        auth_pass webcache
    }
    virtual_ipaddress {
        192.168.2.158 brd 192.168.2.255 label eth0:158
    }
 track_script {
  check_http
 }
}

vrrp_instance VI_159 {
    state MASTER
    interface eth0
    virtual_router_id 63
    priority 100
    advert_int 1
    smtp_alert
    authentication {
        auth_type PASS
        auth_pass webcache
    }
    virtual_ipaddress {
        192.168.2.159 brd 192.168.2.255 label eth0:159
    }
 track_script {
  check_http
 }
}

本文来源:http://www.bbyears.com/shujuku/47875.html

热门标签

更多>>

本类排行