linux運維、架構之路-LVS負載均衡

1、LVS介紹html

一、介紹   linux

       LVS是Linux Virtual Server的簡寫,是linux虛擬的服務器集羣系統,能夠在unix/linux平臺下實現負載均衡集羣功能,由章文嵩博士組織成立,是國內出現最先的自由軟件之一。web

LVS項目介紹	
http://www.linuxvirtualserver.org/zh/lvs1.html
LVS集羣的體系結構	
http://www.linuxvirtualserver.org/zh/lvs2.html
LVS集羣中的IP負載均衡技術	
http://www.linuxvirtualserver.org/zh/lvs3.html
LVS集羣的負載調度	
http://www.linuxvirtualserver.org/zh/lvs4.html  

二、LVS特性算法

①真正實現負載調度的工具是IPVS,工做在linux內核層面。
②LVS自帶的IPVS管理工具是ipvsadm。
③keepalived實現管理IPVS及對負載均衡器的高可用。後端

三、LVS——DR模式工做原理服務器

四、LVS集羣其它模式併發

①DR直接路由模式(重點掌握)負載均衡

②NATcurl

③TUN隧道模式tcp

④FULLNAT

五、LVS應用場景

      日PV1000-2000W或者併發請求1W如下的均可以使用Nginx,超過的話使用LVS,大型門戶網站,電商網站須要用到

2、手工配置LVS

一、環境

[root@lb01 ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@lb01 ~]# uname -r
3.10.0-327.el7.x86_64
[root@lb01 ~]# getenforce 
Disabled
[root@lb01 ~]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
[root@lb01 ~]# hostname -I
10.0.0.5 172.16.1.5

二、安裝ipvsadm管理工具

①檢測是否安裝

[root@lb01 ~]# lsmod |grep ip_vs

②安裝ipvsadm

yum -y install ipvsadm

③檢查並激活lvs

[root@lb01 ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
[root@lb01 ~]# lsmod |grep ip_vs
ip_vs                 140944  0 
nf_conntrack          105745  1 ip_vs
libcrc32c              12644  2 xfs,ip_vs

三、配置LVS負載均衡(lb01操做)

ip addr add 10.0.0.3/24 dev eth0 #在eth0網卡綁定VIP地址
ipvsadm -C   #清除當前全部LVS規則                 
ipvsadm --set 30 5 60      #設置tcp、tcpfin、udp連接超時時間   
ipvsadm -A -t 10.0.0.3:80 -s wrr -p 20   #添加虛擬服務(-A)
ipvsadm -a -t 10.0.0.3:80 -r 10.0.0.7:80 -g -w 1 #將虛擬服務關聯到真實服務上(-a)
ipvsadm -a -t 10.0.0.3:80 -r 10.0.0.8:80 -g -w 1 #將虛擬服務關聯到真實服務上(-a)
ipvsadm -ln #查看配置結果
-C 清空整個表
-A 添加一個虛擬服務
-t 指定一個地址,必定是ip+端口
-s 調度算法
-a 添加一個real server
-m NAT模式
-g DR模式,默認
-d 刪除一個real server
-p 會話保持功能
-w 權重
-i tunnel模式

四、web服務器操做(web0一、web02)

①在lo網卡綁定VIP地址

ip addr add 10.0.0.3/32 dev lo

②修改內核參數抑制ARP響應

cat >>/etc/sysctl.conf<<EOF
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
EOF
sysctl -p

五、在lb02上面測試

[root@lb02 ~]# curl 10.0.0.3
web02
[root@lb02 ~]# curl 10.0.0.3
web02
[root@lb02 ~]# curl 10.0.0.3
web02
[root@lb02 ~]# curl 10.0.0.3
web02
[root@lb02 ~]# curl 10.0.0.3
web02
[root@lb02 ~]# curl 10.0.0.3
web01
[root@lb02 ~]# curl 10.0.0.3
web01

3、Keepalived配合LVS實現高可用負載均衡

一、安裝Keepalived

yum -y install keepalived

二、配置keepalived管理LVS

global_defs {

   router_id LVS_01

}

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 150

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

     10.0.0.3/24

    }

}

virtual_server 10.0.0.3 80 {

    delay_loop 6                  

    lb_algo wrr               

    lb_kind DR               

    nat_mask 255.255.255.0

    persistence_timeout 50    

    protocol TCP               

    real_server 10.0.0.7 80 {

        weight 1             

        TCP_CHECK {

        connect_timeout 8      

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

        }

    }

 

    real_server 10.0.0.8 80 {

        weight 1             

        TCP_CHECK {

        connect_timeout 8      

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

        }

    }

}

global_defs {

   router_id LVS_02

}

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 {

     10.0.0.3/24

    }

}

virtual_server 10.0.0.3 80 {

    delay_loop 6         

    lb_algo wrr               

    lb_kind DR               

    nat_mask 255.255.255.0

    persistence_timeout 50    

    protocol TCP               

    real_server 10.0.0.7 80 {

        weight 1             

        TCP_CHECK {

        connect_timeout 8      

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

        }

    }

 

    real_server 10.0.0.8 80 {

        weight 1             

        TCP_CHECK {

        connect_timeout 8      

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

        }

    }

}

 三、測試keepalived高可用,故障轉移

①停掉lb01的keepalvied

[root@lb01 ~]# systemctl stop keepalived.service
[root@lb01 ~]# curl 10.0.0.3
web01

停掉lb02的keepalvied

[root@lb02 ~]# systemctl stop keepalived.service
[root@lb02 ~]# curl 10.0.0.3
web01

四、測試Keepalived對後端節點的健康檢查功能

①web後端正常時狀態

[root@lb01 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.3:80 wrr persistent 50
  -> 10.0.0.7:80                  Route   1      0          0         
  -> 10.0.0.8:80                  Route   1      0          0 

②web後端節點宕機或者服務關閉時(此處關閉了web01)

[root@lb01 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.3:80 wrr persistent 50
  -> 10.0.0.7:80                  Route   1      0          0 

五、Keepalived+LVS多實例配置

①lb01

global_defs {
   router_id LVS_01
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
     10.0.0.3/24
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2222
    }
    virtual_ipaddress {
     10.0.0.4/24
    }
}

virtual_server 10.0.0.3 80 {
    delay_loop 6              
    lb_algo wrr                
    lb_kind DR                
    nat_mask 255.255.255.0
    persistence_timeout 50     
    protocol TCP                

    real_server 10.0.0.7 80 {
        weight 1              
        TCP_CHECK {
        connect_timeout 8       
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }

    real_server 10.0.0.8 80 {
        weight 1              
        TCP_CHECK {
        connect_timeout 8       
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
}

virtual_server 10.0.0.4 80 {
    delay_loop 6          
    lb_algo wrr                
    lb_kind DR                
    nat_mask 255.255.255.0
    persistence_timeout 50     
    protocol TCP                

    real_server 10.0.0.7 80 {
        weight 1              
        TCP_CHECK {
        connect_timeout 8       
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }

    real_server 10.0.0.8 80 {
        weight 1              
        TCP_CHECK {
        connect_timeout 8       
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
}

②lb02

global_defs {
   router_id LVS_02
}

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 {
     10.0.0.3/24
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2222
    }
    virtual_ipaddress {
     10.0.0.4/24
    }
}

virtual_server 10.0.0.3 80 {
    delay_loop 6          
    lb_algo wrr                
    lb_kind DR                
    nat_mask 255.255.255.0
    persistence_timeout 50     
    protocol TCP                

    real_server 10.0.0.7 80 {
        weight 1              
        TCP_CHECK {
        connect_timeout 8       
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }

    real_server 10.0.0.8 80 {
        weight 1              
        TCP_CHECK {
        connect_timeout 8       
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
}

virtual_server 10.0.0.4 80 {
    delay_loop 6          
    lb_algo wrr                
    lb_kind DR                
    nat_mask 255.255.255.0
    persistence_timeout 50     
    protocol TCP                

    real_server 10.0.0.7 80 {
        weight 1              
        TCP_CHECK {
        connect_timeout 8       
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }

    real_server 10.0.0.8 80 {
        weight 1              
        TCP_CHECK {
        connect_timeout 8       
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
}
相關文章
相關標籤/搜索