負載均衡集群介紹、LVS介紹及調度算法、LVS NAT模式搭建
-
實現負載均衡集群的軟件有:LVS、keepalived、Nginx、haproxy等。其中LVS屬於四層(網絡OSI模型);Nginx 屬於七層;haproxy既可以認為是四層,也可以當作是七層使用。
-
keepalived 的負載均衡功能其實就是lvs
-
LVS、haproxy這種四層負載均衡可以分發除80端口以外的通信,如mysql-3306;而nginx 僅僅支持http,https,mail。
- 相對來說,LVS 這種四層的更加穩定,能承受更多的請求,而nginx 這種七層的更加靈活,能實現更多的個性化需求。
LVS 介紹
- LVS 是由中國人章文嵩開發的
- 流行度不亞於apache的httpd,基於TCP/IP做的路由和轉發,穩定性和效率很高
- LVS 最新版本基於Linux 內核2.6,有好多年不更新了
- LVS 有三種常見的模式:NAT、DR、IP Tunnel
- LVS 架構中有一個核心角色叫做分發器(Load balancer),它用來分發用戶的請求,還有諸多處理用戶請求的服務器(Real Sever,簡稱rs)
LVS NAT 模式
-
這種模式借助iptables的nat表來實現
-
用戶的請求到分發器後,通過預設的iptables規則,把請求的數據包轉發到後端的rs上去
-
rs需要設定網關為分發器的內網ip
-
用戶請i去的數據包和返回給用戶的數據包全部經過分發器,所以分發器成為瓶頸
- 在nat模式中,只需要分發器有公網ip即可,所以比較節省公網ip資源
註意: 該模式中,load balancer 是限制該架構處理用戶請求的一個瓶頸,所以該模式比較適合小模式的集群(服務器rs在10臺以內);該模式的優勢是節省公網資源。
LVS IP Tunnel模式
-
這種模式,需要有一個公共的IP配置在分發器和所有rs上,我們把它叫做vip
-
客戶端請求的目標IP為vip,分發器接收到請求數據包後,會對數據包做一個加工,會把目標ip改為rs的IP,這樣數據包就到了rs上
- rs接收數據包後,會還原原始數據包,這樣目標IP為vip,因為所有rs上配置了這個vip,所以它會認為是它自己
註意: real server通過一個公網IP直接返回到用戶,這就省略數據回到load balancer分發器的過程,這樣就load balancer就沒有瓶頸。
LVS DR 模式
-
這種模式,也需要有一個公共的IP配置在分發器和所有rs上,也就是vip
-
和IP Tunnel不同的是,它會把數據包的MAC地址修改為rs的MAC地址
- rs接收數據包後,會還原原始數據包,這樣目標IP 為vip,因為所有rs上配置了這個vip,所以它認為是它自己
LVS 調度算法
-
輪詢 Round-Robin rr
- 用戶的請求過來,它均衡的將請求分發到rs上,沒有優劣之分
-
加權輪詢 Weight Round-Robin wrr
- 帶權重的輪詢,可以對機器單獨設置權重,對權重高的機器發送的請求會多一些
-
最小連接 Least-Connection lc
- 把請求發送到請i去數量少的rs上
-
加權最小連接 Weight Least-Connection wlc
- 帶權重的最小連接,權重高的優先
-
基於局部性的最小連接 Locality-Based Least Connection lblc
-
帶復制的基於局部性最小連接 Locality-Based Least Connections with Replication lblcr
-
目標地址散列調度 Destination Hashing dh
- 源地址散列調度 Source Hashing sh
常用的是前四中算法
LVS NAT模式搭建
是通過iptables 實現
準備工作
-
準備三臺虛擬機
-
分發器 (調度器director)
內網:192.168.159.133,外網:192.168.64.151
-
real server1 (real 1)
內網:192.168.159.131,網關:192.168.159.130
-
real server2 (real 2)
內網:192.168.159.132,網關:192.168.159.130
-
-
關閉三臺機器的防火墻
建議使用iptables:
[root@centos04 ~]# systemctl stop firewalld
// 關閉firewalld 防火墻[root@centos04 ~]# systemctl disable firewalld
// 禁止firewalld防火墻開機啟動// 此處建議使用iptables
[root@centos04 ~]# yum install -y iptables.service
// 安裝iptables.servcie[root@centos04 ~]# systemctl enable iptables
// 設置iptables 開機啟動[root@centos04 ~]# systemctl start iptables
// 開啟iptables 服務[root@centos04 ~]# iptables -F
// 清空防火墻規則[root@centos04 ~]# service iptables save
// 保存當前規則// 進行該設置的原因是NAT模式是基於防火墻nat表的一種模式,所以會使用iptables規則。
臨時關閉selinux
[root@centos04 ~]# getenforce
// 查看selinux狀態
// Enforcing 為開啟, Permissive 為關閉
[root@centos04 ~]# setenforce 0/1
// 開啟、關閉防火墻
// 0為關閉、1為開啟永久關閉selinux
[root@centos04 ~]# vim /etc/selinux/config
// 將selinux 由enforcing 改成disabled
配置分發器
-
在dir(分發器)上安裝ipvsadm
[root@centos04 ~]# yum install -y ipvsadm
// 這個工具和iptables 有點像
// 如果下載很慢,則進入/etc/yum.repos.d/
// 將epel.repo 重命名,下在完之後改回來 -
在dir(分發器)上編寫腳本
[root@centos04 ~]# 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/ens37/send_redirects
// director 設置nat防火墻
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 192.168.64.0/24 -j MASQUERADE
// director設置ipvsadm規則
IPVSADM=‘/usr/sbin/ipvsadm‘
$IPVSADM -C
// -C=clear,清除規則
$IPVSADM -A -t 192.168.64.151:80 -s rr
// -A:=add,添加規則,定義算法的規則;
// -t:分發器ip(外網ip);
// -s 指定算法;
// -p:指定超時時間(解決session問題:保證同一請求被分發到同一rs上)單位為秒。
// 因為添加-p選項後會影響測試效果,所以在此不加該參數(註:時間不能設置為0)
$IPVSADM -a -t 192.168.64.151:80 -r 192.168.159.131:80 -m -w 1
$IPVSADM -a -t 192.168.64.151:80 -r 192.168.159.132:80 -m -w 1
// -a:=add,增加nat架構中的rs;
// -r:指定rs的IP;
// -m:指定LVS模式為NAT(masquerade)
// -w:=weight,指定權重執行腳本:
[root@centos04 ~]# sh /usr/local/sbin/lvs_nat.sh
// 執行該腳本時無錯誤輸出說明沒問題。查看ipvsadm規則:
[root@centos04 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.64.151:80 rr
-> 192.168.159.131:80 Masq 1 0 0
-> 192.168.159.132:80 Masq 1 0 0
配置rs
-
在兩臺rs上安裝Nginx,並分別設置主頁來區分兩臺機器。
[root@localhost ~]# yum install -y nginx
[root@localhost ~]# systemctl start nginx// 如果nginx 是yum安裝,其主頁目錄為 /usr/share/nginx/html/index.html
測試
-
訪問外網ip:192.168.64.151
[root@centos04 ~]# curl 192.168.64.151
This is real server 2
[root@centos04 ~]# curl 192.168.64.151
this is real server 1
[root@centos04 ~]# curl 192.168.64.151
This is real server 2
[root@centos04 ~]# curl 192.168.64.151
this is real server 1
負載均衡集群介紹、LVS介紹及調度算法、LVS NAT模式搭建