1. 程式人生 > >負載均衡集群介紹、LVS介紹、LVS調度算法、LVS NAT模式搭建

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

外網 ext root 設置ip 調度算法 tun http isa ip配置

18.6 負載均衡集群介紹
  • 主流開源軟件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層的更加靈活,能實現更多的個性化需求

18.7 LVS介紹

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

LVS NAT模式

技術分享圖片

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

LVS IP Tunnel模式

技術分享圖片

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

LVS DR模式

技術分享圖片

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

18.8 LVS調度算法

  • 輪詢 Round-Robin rr
  • 加權輪詢 Weight Round-Robin wrr
  • 最小連接 Least-Connection lc
  • 加權最小連接 Weight Least-Connection wlc
  • 基於局部性的最小連接 Locality-Based Least Connections lblc
  • 帶復制的基於局部性最小連接 Locality-Based Least Connections with Replication lblcr
  • 目標地址散列調度 Destination Hashing dh
  • 源地址散列調度 Source Hashing sh

18.9-18.10 LVS NAT模式搭建

NAT模式搭建-準備工作

  • 三臺機器
  • 分發器,也叫調度器(簡寫為dir)
  • 內網:192.168.0.130/24 ,外網:192.168.147.144 (VMware僅主機模式)
    技術分享圖片
[root@taoyuan ~]# hostnamectl set-hostname qingyun-01
#進入個子shell
[root@taoyuan ~]# bash
[root@qingyun-01 ~]# 
  • rs1
    • 內網:192.168.0.132/24,設置網關為192.168.0.130
[root@qingyun-02 html]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.130   0.0.0.0         UG    100    0        0 ens33
192.168.0.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33
  • rs2
    • 內網:192.168.0.133/24,設置網關為192.168.0.130
[root@qingyun-03 html]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.130   0.0.0.0         UG    100    0        0 ens33
192.168.0.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33
  • 三臺機器上都執行,關閉防火墻
  • systemctl stop firewalld;systemctl disable firewalld

#由於3臺是重新,都關閉防火墻
[root@qingyun-01 ~]# systemctl stop firewalld
[root@qingyun-01 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@qingyun-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

[root@qingyun-03 ~]# yum install -y iptables-services

#如果安裝很慢,可以取消epel
#把/etc/yum.repos.d/目錄下epel.repo 改一下名字

#查看包安裝的文件
[root@qingyun-01 yum.repos.d]# rpm -ql iptables-services
/etc/sysconfig/ip6tables
/etc/sysconfig/iptables
/usr/lib/systemd/system/ip6tables.service
/usr/lib/systemd/system/iptables.service

#啟動iptables.service
[root@qingyun-01 yum.repos.d]# systemctl start iptables
[root@qingyun-01 yum.repos.d]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
  • systemctl start iptables-services;iptables -F; service iptables save
#開啟的目的是為了調用一個空的規則

[root@qingyun-03 ~]# iptables -F
[root@qingyun-03 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  確定  ]

#關閉selinux
[root@qingyun-01 yum.repos.d]# setenforce 0
[root@qingyun-01 yum.repos.d]# vi /etc/selinux/config 
#SELINUX=disabled 

#查看網關
[root@qingyun-03 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    100    0        0 ens33
192.168.0.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33

NAT模式搭建

  • 在dir上安裝ipvsadm
[root@qingyun-01 yum.repos.d]# yum install -y ipvsadm
  • 在dir上編寫腳本,vim /usr/local/sbin/lvs_nat.sh //內容如下
[root@qingyun-01 ~]# 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
# dirrector 設置nat防火墻
iptables -t nat -F 
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE
# director設置ipvsadm
IPVSADM=‘/usr/sbin/ipvsadm‘
$IPVSADM -C
$IPVSADM -A -t 192.168.147.144:80 -s wlc -p 3
$IPVSADM -a -t 192.168.147.144:80 -r 192.168.0.132:80 -m -w 1
$IPVSADM -a -t 192.168.147.144:80 -r 192.168.0.133:80 -m -w 1
  • 執行腳本
[root@qingyun-01 ~]# sh /usr/local/sbin/lvs_nat.sh
#沒有輸出,說明沒有錯誤

NAT模式效果測試

  • 兩臺rs上都安裝nginx

  • 設置兩臺rs的主頁,做一個區分,也就是說直接curl兩臺rs的ip時,得到不同的結果
[root@qingyun-01 ~]# curl 192.168.147.144
qingyun03-132
[root@qingyun-01 ~]# curl 192.168.147.144
qingyun03-133
[root@qingyun-01 ~]# curl 192.168.147.144
qingyun03-132
[root@qingyun-01 ~]# curl 192.168.147.144
qingyun03-133
[root@qingyun-01 ~]# cat /usr/local/sbin/lvs_nat.sh 
...........
$IPVSADM -C
$IPVSADM -A -t 192.168.147.144:80 -s wlc
.............
  • 瀏覽器裏訪問192.168.147.144,多訪問幾次查看結果差異

技術分享圖片

[root@qingyun-01 ~]# 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.147.144:80 wlc
  -> 192.168.0.132:80             Masq    1      0          0         
  -> 192.168.0.133:80             Masq    1      0          4        

技術分享圖片

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