負載均衡集羣介紹、LVS介紹、LVS的調度算法、LVS NAT模式搭建

負載均衡集羣介紹

  • 主流開源軟件LVS、keepalived、haproxy、nginx等;
  • 其中LVS屬於4層(網絡OSI 7層模型),nginx屬於7層,haproxy既可以認爲是4層,也可以當做7層使用;
  • keepalived的負載均衡功能其實就是lvs;
  • lvs這種4層的負載均衡是可以分發除80外的其他端口通信的,比如MySQL的,而nginx僅僅支持http,https,mail,haproxy也支持MySQL這種;
  • 相比較來說,LVS這種4層的更穩定,能承受更多的請求,而nginx這種7層的更加靈活,能實現更多的個性化需求;

LVS介紹

  • LVS是由國人章文嵩開發;
  • 流行度不亞於apache的httpd,基於TCP/IP做的路由和轉發,穩定性和效率很高;
  • LVS最新版本基於Linux內核2.6,有好多年不更新了;
  • LVS有三種常見的模式:NAT、DR、IP Tunnel;
  • LVS架構中有一個核心角色叫做分發器(Load balance),它用來分發用戶的請求,還有諸多處理用戶請求的服務器(Real Server,簡稱rs);

LVS NAT模式

負載均衡集羣介紹、LVS介紹、LVS的調度算法、LVS NAT模式搭建

  • 這種模式藉助iptables的nat表來實現;
  • 用戶的請求到分發器後,通過預設的iptables規則,把請求的數據包轉發到後端的rs上去;
  • rs需要設定網關爲分發器的內網ip;
  • 用戶請求的數據包和返回給用戶的數據包全部經過分發器,所以分發器成爲瓶頸;
  • 在nat模式中,只需要分發器有公網ip即可,所以比較節省公網ip資源;

LVS IP Tunnel模式

負載均衡集羣介紹、LVS介紹、LVS的調度算法、LVS NAT模式搭建

  • 這種模式,需要有一個公共的IP配置在分發器和所有rs上,我們把它叫做vip;
  • 客戶端請求的目標IP爲vip,分發器接收到請求數據包後,會對數據包做一個加工,會把目標IP改爲rs的IP,這樣數據包就到了rs上;
  • rs接收數據包後,會還原原始數據包,這樣目標IP爲vip,因爲所有rs上配置了這個vip,所以它會認爲是它自己;

LVS DR模式

負載均衡集羣介紹、LVS介紹、LVS的調度算法、LVS NAT模式搭建

  • 這種模式,也需要有一個公共的IP配置在分發器和所有rs上,也就是vip
  • 和IP Tunnel不同的是,它會把數據包的MAC地址修改爲rs的MAC地址
  • rs接收數據包後,會還原原始數據包,這樣目標IP爲vip,因爲所有rs上配置了這個vip,所以它會認爲是它自己

LVS的調度算法

1. 輪詢 Round-Robin rr (重點)

非常簡單的一種高度算法,就是按順序把請求依次發送給後端的服務器,它不管後端服務器的處理速度和響應時間怎樣。當後端服務器性能不一致時,用這種調度算法就不合適過了。

2. 加權輪詢 Weight Round-Robin wrr (重點)

比第一種算法多了一個權重的設置,權重越高的服務器被分配到的請求就越多,這樣後端服務器被分配到的請求就越多,這樣後端服務器性能不一致時,就可以給配置低的服務器較小的權重。

3. 最小連接 Least-Connection lc (重點)

這種算法會根據各真實服務器上的連接數來決定把新的請求分配給誰,連接數少說明服務器是空閒的,這樣把新的請求分配到空閒服務器上才更加合理。

4. 加權最小連接 Weight Least-Connection wlc (重點)

在最小連接高度的基礎上再增加一個權重設置,這樣就可以人爲地去控制哪些服務上多分配請求,哪些少分配請求。

5. 基於局部性的最小連接 Locality-Based Least Connections lblc

這種算法簡稱LBLC,是針對請求報文的目標IP地址的負載均衡調度,目前主要用於Cache集羣系統,因爲在Cache集羣客戶請求報文的目標IP地址是變化的。算法的設計目標是在服務器的負載基本平衡的情況下,將相同目標IP地址的請求調度到同一臺服務器,來提高各臺服務器的訪問局部性和主存Cache命中率。

6. 帶複製的基於局部性最小連接 Locality-Based Least Connections with Replication lblcr

該算法簡稱LBLCR,也是針對目標IP地址的負載均衡,它與LBLC算法的不同之處是:它要維護從一個目標IP地址到一組服務器的映射,而LBLC算法是維護從一個目標IP地址到一臺服務器的映射。LBLCR算法先根據請求的目標IP地址找出該目標IP地址對應的服務器組,按“最小連接”原則從該服務器組中選出一臺服務器,若服務器沒有超載,則將請求發送到該服務器;若服務器超載,則按“最小連接”原則從整個集羣中選出一臺服務器,將該服務器加入到服務器組中,將請求發送到該服務器。同時,當該服務器組有一段時間沒有被修改,將最忙的服務器從服務器組中刪除,以降低複製的程度。

7. 目標地址散列調度 Destination Hashing dh

該算法也是針對目標IP地址的負載均衡的,但它是一種靜態映射算法,通過一個散列(hash)函數將一個目標IP地址映射到一臺服務器。目標地址散列調度算法先根據請求的目標IP地址,作爲散列鍵(hash key)從靜態分配的散列表找出對應的服務器,若該服務器是可用的且未超載,將請求發送到該服務器,否則返回空。

8. 源地址散列調度 Source Hashing sh

該算法正好與目標地址散列調度算法相反,它根據請求的源IP地址,作爲散列鍵從靜態分配的散列表找出對應的服務器,若該服務器是可用的且未超載,將請求發送到該服務器,否則返回空。它的算法流程與目標地址散列調度算法的基本相似,只不過將請求的目標IP地址換成請求的源IP地址。

LVS NAT模式搭建

1.準備工作

1. 準備三臺機器,按需求配置好IP。
  • 分發器,也叫調度器(簡寫爲dir)兩張網卡,一個內網IP:172.16.111.100/24,一個外網IP:172.16.13.129/24(vmware僅主機模式)
  • 真實服務器rs1,內網IP:172.16.111.110/24,設置網關爲172.16.111.100(網關設置成分發器的內網ip,做最後一步準備工作在改)
  • 真實服務器rs2,內網IP:172.16.111.120/24,設置網關爲172.16.111.100(網關設置成分發器的內網ip,做最後一步準備工作在改)
  • 爲了好區分實驗效果更改計算機名
[root@localhost ~]# hostnamectl set-hostname gary-tao-03
[root@localhost ~]# bash
##進入子shell,更改成功
[root@gary-tao-03 ~]# 
2. 三臺機器上都要關閉防火牆
[root@localhost ~]# iptables -nvL   //查看
[root@localhost ~]# systemctl stop firewalld  //關閉
[root@gary-tao ~]# systemctl disable firewalld  //關閉開機啓動
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
3. 操作在真實服務器rs1與rs2上,調用一個空的iptables規則,在配置一個新的iptables規則開啓,以免舊的規則影響到實驗效果(使用centos6裏面的iptables形式)

[root@gary ~]# yum install -y iptables-services
#由於要調用epel國外資源安裝會很慢,可以臨時取消epel源的調用方式安裝,操作如下
#把/etc/yum.repos.d/目錄下epel.repo 改一下名字
[root@gary ~]# cd /etc/yum.repos.d/
[root@gary yum.repos.d]# ls
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo  nginx.repo
[root@gary yum.repos.d]# mv epel.repo epel.repo.1

#查看安裝包
[root@gary yum.repos.d]# yum list |grep iptables-service
iptables-services.x86_64                   1.4.21-18.2.el7_4           @updates 
[root@gary ~]# yum install -y iptables-services

#查看一個包都安裝了哪些文件
[root@gary ~]# rpm -ql iptables-services  
/etc/sysconfig/ip6tables
/etc/sysconfig/iptables
/usr/lib/systemd/system/ip6tables.service
/usr/lib/systemd/system/iptables.service
/usr/libexec/initscripts/legacy-actions/ip6tables
/usr/libexec/initscripts/legacy-actions/ip6tables/panic
/usr/libexec/initscripts/legacy-actions/ip6tables/save
/usr/libexec/initscripts/legacy-actions/iptables
/usr/libexec/initscripts/legacy-actions/iptables/panic
/usr/libexec/initscripts/legacy-actions/iptables/save
/usr/libexec/iptables
/usr/libexec/iptables/ip6tables.init
/usr/libexec/iptables/iptables.init

#啓動iptables.service
[root@gary ~]# systemctl start iptables
[root@gary ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.

#開啓的目的是爲了調用一個空的規則
[root@localhost ~]# iptables -F
[root@localhost ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  確定  ]

#關閉SELinux
[root@localhost ~]# getenforce  #查看selinux
Enforcing
[root@localhost ~]# setenforce 0  #臨時關閉
[root@gary-tao ~]# getenforce   #已關閉狀態
Disabled
[root@gary ~]# getenforce   #臨時關閉狀態
Permissive
[root@localhost ~]# vi /etc/selinux/config 
#永久關閉,修改配置文件,把Enforcing改成Disabled,重啓生效。

#rs1與rs2的網關都要是dir機器的內網ip
[root@garytao-03 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.16.111.100  0.0.0.0         UG    100    0        0 ens33
172.16.111.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33

2.配置分發器dir

安裝LVS核心工具ipvsdam

[root@garytao-01 ~]# yum install -y ipvsadm
//安裝LVS的核心工具,只需要在dir機器安裝

#假設如果報錯沒有可用軟件包 ipvsadm,按如下方式安裝:

wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz 
tar zxf ipvsadm-1.26.tar.gz 
cd ipvsadm-1.26 
rpm -qa | grep kernel-devel(默認已經安裝) 
(安裝所需依賴包 
yum install popt-static kernel-devel make gcc openssl-devel lftplibnl* popt* openssl-devel lftplibnl* popt* libnl* libpopt* gcc*)
make && make install 

檢測是否安裝成功:

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

在dir上編寫一個腳本:

[root@gary-tao ~]# vim /usr/local/sbin/lvs_nat.sh

增加如下配置內容:

#! /bin/bash
# director 服務器上開啓路由轉發功能
echo 1 > /proc/sys/net/ipv4/ip_forward
# 關閉icmp的重定向
echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
# 注意區分網卡名字,這裏兩個網卡分別爲ens33和ens37
echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/ens38/send_redirects
# director 設置nat防火牆
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 172.16.111.0/24  -j MASQUERADE
# director設置ipvsadm
IPVSADM='/usr/sbin/ipvsadm'
$IPVSADM -C
$IPVSADM -A -t 172.16.13.129:80 -s wlc -p 3
$IPVSADM -a -t 172.16.13.129:80 -r 172.16.111.110:80 -m -w 1
$IPVSADM -a -t 172.16.13.129:80 -r 172.16.111.120:80 -m -w 1

[root@gary-tao ~]# sh /usr/local/sbin/lvs_nat.sh  //執行腳本

3.NAT模式效果測試

  • 兩臺rs上都安裝nginx;
  • 設置兩臺rs的主頁,做一個區分,也就是說直接curl兩臺rs的ip時,得到不同的結果

這裏改了下第二臺rs的nginx配置,第三臺沒有改。

[root@gary ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@gary ~]# vi /usr/share/nginx/html/index.html 
[root@gary ~]# curl localhost
garytao02.
  • 使用curl訪問172.16.13.129,多訪問幾次看測試結果
[root@garytao-01 ~]# curl 172.16.13.129
garytao02.
[root@garytao-01 ~]# curl 172.16.13.129
garytao02.
[root@garytao-01 ~]# curl 172.16.13.129
garytao02.
[root@garytao-01 ~]# curl 172.16.13.129
garytao02.
[root@garytao-01 ~]# curl 172.16.13.129
garytao02.

#連接訪問多次,一直請求到rs1上。這是因爲在前面的腳本中有設置有-p參數,理論上有300秒內會一直請求到rs1上。

#更改腳本的時間值,把wlc -p 3參數刪除了,改成rr
[root@garytao-01 ~]# cat /usr/local/sbin/lvs_nat.sh
#! /bin/bash
# director 服務器上開啓路由轉發功能
echo 1 > /proc/sys/net/ipv4/ip_forward
# 關閉icmp的重定向
echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
# 注意區分網卡名字,這裏兩個網卡分別爲ens33和ens37
echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/ens38/send_redirects
# director 設置nat防火牆
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 172.16.111.0/24  -j MASQUERADE
# director設置ipvsadm
IPVSADM='/usr/sbin/ipvsadm'
$IPVSADM -C
$IPVSADM -A -t 172.16.13.129:80 -s rr 
$IPVSADM -a -t 172.16.13.129:80 -r 172.16.111.110:80 -m -w 1
$IPVSADM -a -t 172.16.13.129:80 -r 172.16.111.120:80 -m -w 1

#再次測試,做到了均衡訪問
[root@garytao-01 ~]# curl 172.16.13.129
garytao02.
[root@garytao-01 ~]# curl 172.16.13.129
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@garytao-01 ~]# curl 172.16.13.129
garytao02.
[root@garytao-01 ~]# curl 172.16.13.129
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章