1. 程式人生 > >十五周五次課

十五周五次課

linux

十五周五次課

18.6 負載均衡集群介紹
18.7 LVS介紹
18.8 LVS調度算法
18.9/18.10 LVS NAT模式搭建

18.6 負載均衡集群介紹

負載均衡集群介紹

  • 主流開源軟件LVS、keepalived、haproxy、nginx等

    • 其中LVS屬於4層(網絡OSI 7層模型),nginx屬於7層,haproxy既可以認為是4層,也可以當做7層使用

    • keepalived的負載均衡功能其實就是lvs,lvs是keepalived內置的

    • lvs這種4層的負載均衡是可以分發TCP協議,web服務是80端口,除了分發80端口,還有其他的端口通信的,比如MySQL的負載均衡,就可以用LVS實現,而nginx僅僅支持http,https,mail,haproxy;haproxy也支持MySQL這種TCP負載均衡的

    • 7層有限制,不過有些更高級的功能,nginx可以通過站點目錄,去區分網站服務器之前,LVS4層的就不支持

  • 相比較來說,LVS這種4層的更穩定,能承受更多的請求,承載的並發量更高,而nginx這種7層的更加靈活,能實現更多的個性化需求

18.7 LVS介紹


LVS介紹

  • LVS是由國人章文嵩開發

  • 流行度不亞於apache的httpd,基於TCP/IP做的路由和轉發,穩定性和效率很高

  • LVS最新版本基於Linux內核2.6,有好多年不更新了

  • LVS有三種常見的模式:NAT、DR、IP Tunnel

  • LVS架構中有一個核心角色叫做分發器(Load balance),它用來分發用戶的請求,還有諸多處理用戶請求的服務器(Real Server,簡稱rs)

LVS NAT模式

  • LVS NAT模式,借助iptables的nat表來實現

  • 用戶的請求到分發器後,通過預設的iptables規則,把請求的數據包轉發到後端的rs上去

  • rs需要設定網關為分發器的內網ip

  • 用戶請求的數據包和返回給用戶的數據包全部經過分發器,所以分發器成為瓶頸

  • 在nat模式中,只需要分發器有公網ip即可,所以比較節省公網ip資源

技術分享圖片

  • 原理圖解釋:

    • Load Balancer,就是一個分發器;把用戶的請求,分發給後端的Real Server ,Real Server這些服務器接收到請求以後,處理好用戶請求以後,就重新丟回給Load Balancer;最後Load Balancer再返回給用戶;

    • 這個模式的弊端,當訪問量、請求量、反饋量大的時候,Load Balancer的壓力很大

    • LVS規模,一般規模最多支持10來臺服務器,超過10臺的話就會有力不從心;

    • nat模式這個結構,只需要有一個公網IP,其他real server服務器全部在內網就可以實現。優點,節省很多的資源

LVS IP Tunnel模式

  • LVS IP Tunnel模式,需要有一個公共的IP配置在分發器和所有rs上,我們把它叫做vip

  • 客戶端請求的目標IP為vip,分發器接收到請求數據包後,會對數據包做一個加工,會把目標IP改為rs的IP,這樣數據包就到了rs上

  • rs接收數據包後,會還原原始數據包,這樣目標IP為vip,因為所有rs上配置了這個vip,所以它會認為是它自己

技術分享圖片

  • 原理圖解釋:

    • 在load balancer與real server之間建立了虛擬通道,叫做 ip tunnel ;實際上是更改了數據包目的IP;請求過來通過load balancer,通過在real server上配置的VIP;用戶請求的時候,數據包裏面包好的目的IP,當數據包到達load balancer的時候,load balancer會進行一個數據包目的IP的更改,然後發送到具體的real server上,通過lvs的自己的算法,進行實現到底傳輸到哪個real server上;然後real server再解包處理,再通過一個VIP直接返回到用戶,這就省略數據回到load balancer分發器的過程,這樣就load balancer就沒有瓶頸

LVS DR模式

  • LVS DR模式,也需要有一個公共的IP配置在分發器和所有rs上,也就是vip

    • 和IP Tunnel不同的是,它會把數據包的MAC地址修改為rs的MAC地址

    • rs接收數據包後,會還原原始數據包,這樣目標IP為vip,因為所有rs上配置了這個vip,所以它會認為是它自己

技術分享圖片





18.8 LVS調度算法


LVS調度算法

  • 輪詢 Round-Robin 簡稱:rr 最簡單的也是最容易理解

    • 用戶請求過來,均衡的分發到rs上

  • 加權輪詢 Weight Round-Robin 簡稱:wrr

    • 帶權重的輪詢,可以對機器單獨設置權重,對高權重的機器發送的請求會多一些

  • 最小連接 Least-Connection 簡稱: lc

    • 把請求發送到請求數量小的rs上

  • 加權最小連接 Weight Least-Connection 簡稱: wlc

    • 對請求數量小的rs,加上一個權重,使他優先

  • 基於局部性的最小連接 Locality-Based Least Connections 簡稱: lblc

  • 帶復制的基於局部性最小連接 Locality-Based Least Connections with Replication 簡稱: lblcr

  • 目標地址散列調度 Destination Hashing 簡稱:dh

  • 源地址散列調度 Source Hashing 簡稱: sh

18.9/18.10 LVS NAT模式搭建

LVS NAT模式搭建

  • NAT模式搭建 – 準備工作

    • 內網:133.133,設置網關為133.130

    • 內網:133.132,設置網關為133.130

    • 內網:133.130,外網:147.144(vmware僅主機模式)

    • 三臺機器

    • 分發器,也叫調度器(簡寫為dir)

    • rs1

    • rs2

    • 三臺機器上都執行執行

    • systemctl stop firewalld; systemc disable firewalld

    • systemctl start iptables-services; iptables -F; service iptables save

  • NAT模式搭建

    • 在dir上安裝ipvsadm

    • yum install -y ipvsdam

    • 在dir上編寫腳本,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_redirectsecho 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.133.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.133.132:80 -m -w 1
$IPVSADM -a -t 192.168.147.144:80 -r 192.168.133.133:80 -m -w 1
  • NAT模式效果測試

    • 兩臺rs上都安裝nginx

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

    • 瀏覽器裏訪問192.168.142.147,多訪問幾次看結果差異


LVS NAT模式搭建


NAT模式搭建-準備工作

  • NAT模式是通過iptables實現的,所以必須配置一些iptables規則

1.在配置前準備三臺機器,一臺作為分發器,也叫做調度器,簡稱 dir,另外兩臺就是real server,用來處理用戶請求的服務器,rs1、rs2(克隆虛擬機步驟)克隆或者重新裝一臺機器都可以

  • 克隆虛擬機的註意點

A機器IP為192.168.23.130、B機器IP為192.168.23.129,網關為23.130;C機器IP為192.168.23.128,網關為23.130

  • A機器增加一塊網卡,並啟動查看網段為192.168.38.0(根據自己機器來設置),設置新的網卡IP為192.168.38.147,並在物理機訪問這個IP地址,看是否正常通信

    • 增加新的網卡需要更改IP,然後重啟網卡,並重啟網絡服務。增加網卡註意點

[root@tianqi-01 ~]# cd /etc/sysconfig/network-scripts/

[root@tianqi-01 network-scripts]# ls

ifcfg-ens33 ifdown-bnep ifdown-isdn ifdown-sit ifup ifup-ippp ifup-plusb ifup-sit ifup-wireless

ifcfg-ens33:0 ifdown-eth ifdown-post ifdown-Team ifup-aliases ifup-ipv6 ifup-post ifup-Team init.ipv6-global

ifcfg-lo ifdown-ippp ifdown-ppp ifdown-TeamPort ifup-bnep ifup-isdn ifup-ppp ifup-TeamPort network-functions

ifdown ifdown-ipv6 ifdown-routes ifdown-tunnel ifup-eth ifup-plip ifup-routes ifup-tunnel network-functions-ipv6

[root@tianqi-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens37

[root@tianqi-01 network-scripts]# ls

ifcfg-ens33 ifdown-bnep ifdown-post ifdown-TeamPort ifup-eth ifup-plusb ifup-Team network-functions

ifcfg-ens33:0 ifdown-eth ifdown-ppp ifdown-tunnel ifup-ippp ifup-post ifup-TeamPort network-functions-ipv6

ifcfg-ens37 ifdown-ippp ifdown-routes ifup ifup-ipv6 ifup-ppp ifup-tunnel

ifcfg-lo ifdown-ipv6 ifdown-sit ifup-aliases ifup-isdn ifup-routes ifup-wireless

ifdown ifdown-isdn ifdown-Team ifup-bnep ifup-plip ifup-sit init.ipv6-global

[root@tianqi-01 network-scripts]# vim ifcfg-ens37


TYPE=Ethernet

PROXY_METHOD=none

BROWSER_ONLY=no

BOOTPROTO=static

DEFROUTE=yes

IPV4_FAILURE_FATAL=no

IPV6INIT=yes

IPV6_AUTOCONF=yes

IPV6_DEFROUTE=yes

IPV6_FAILURE_FATAL=no

IPV6_ADDR_GEN_MODE=stable-privacy

NAME=ens37

#UUID=bd1e1864-e75b-4f5f-bc63-9b1b0d64feff

DEVICE=ens37

ONBOOT=yes

IPADDR=192.168.38.147

#NETMASK=255.255.255.0

#GATEWAY=192.168.23.2

#DNS1=119.29.29.29

保存退出

  • 重啟ens37網卡

[root@tianqi-01 network-scripts]# ifdown ens37 && ifup ens37

Device 'ens37' successfully disconnected.

Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)

[root@tianqi-01 network-scripts]# ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.23.130 netmask 255.255.255.0 broadcast 192.168.23.255

inet6 fe80::1eb9:8f9e:264a:7159 prefixlen 64 scopeid 0x20<link>

ether 00:0c:29:25:28:af txqueuelen 1000 (Ethernet)

RX packets 584 bytes 48043 (46.9 KiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 334 bytes 59127 (57.7 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.11.139 netmask 255.255.255.0 broadcast 192.168.11.255

ether 00:0c:29:25:28:af txqueuelen 1000 (Ethernet)


ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.38.147 netmask 255.255.255.0 broadcast 192.168.38.255

inet6 fe80::8926:30a4:c44a:4412 prefixlen 64 scopeid 0x20<link>

ether 00:0c:29:25:28:b9 txqueuelen 1000 (Ethernet)

RX packets 4 bytes 618 (618.0 B)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 25 bytes 2338 (2.2 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536

inet 127.0.0.1 netmask 255.0.0.0

inet6 ::1 prefixlen 128 scopeid 0x10<host>

loop txqueuelen 1 (Local Loopback)

RX packets 2 bytes 196 (196.0 B)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 2 bytes 196 (196.0 B)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


[root@tianqi-01 network-scripts]#


PS:網關最後設置,否則包無法下載

技術分享圖片

  • B機器和C機器的網關必須設置成分發器(即A機器)的內網IP,若不設置成它的網關,是沒法通信的

    • 這時B、C機器就無法上網了

2.三臺機器設置完成後,關閉三臺機器的防火墻

關閉firewalld服務

systemctl stop firewalld

使firewalld服務不再開機啟動

systemctl disable firewalld

3.機器B、機器C 下載安裝iptables-services 包

[root@tianqi-02 ~]# yum install -y iptables-services

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

  • 有時下載包的時候特別慢,就是epel.repo源的原因導致的,這裏可以臨時關閉,就是直接更改名字即可(因為epel.repo源是國外的資源,所以很慢)

4.機器B、機器C 啟動iptables服務

  • systemctl start iptables

機器B

[root@tianqi-02 ~]# systemctl start iptables

[root@tianqi-02 ~]#

機器C

[root@localhost ~]# systemctl start iptables

[root@localhost ~]#

5.機器B、機器C 設置開機啟動

  • systemctl enable iptables

  • systemctl enable iptables

  • iptables -F

  • service iptables save

機器B

[root@tianqi-02 ~]# systemctl start iptables

[root@tianqi-02 ~]# systemctl enable iptables

Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.

[root@tianqi-02 ~]# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

72 5204 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED

0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0

0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0

0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22

0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited


Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited


Chain OUTPUT (policy ACCEPT 49 packets, 4508 bytes)

pkts bytes target prot opt in out source destination

[root@tianqi-02 ~]# iptables -F //清空表的規則,以便後續實驗

[root@tianqi-02 ~]# service iptables save

iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]

[root@tianqi-02 ~]#

機器C同上

6.清空並查看機器A的規則

[root@tianqi-01 network-scripts]# iptables -nvL

Chain INPUT (policy ACCEPT 63 packets, 4614 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 40 packets, 3560 bytes)

pkts bytes target prot opt in out source destination

[root@tianqi-01 network-scripts]#

7.關閉機器機器A,機器B,機器C,三臺機器的selinux

setenforce 0 //臨時關閉selinux

getenforce //查看selinux是否關閉

為了保險起見,在配置文件中永久關閉selinux

vi /etc/selinux/config

SELINUX=enforcing更改為SELINUX=disabled

準備工作結束


NAT模式搭建

1.首先在分發器dir上(即A機器),安裝ipvsadm ,這個是實現 lvs 的一個重要的工具,缺少這個工具,將沒有辦法實現 lvs 的功能

[root@tianqi-01 ~]# yum install -y ipvsadm

2.在分發器(A機器)上編寫一個腳本(LVS全都是以腳本的方式去執行的,這樣比較方便進行維護不用一條命令一條命令的進行操作)

[root@tianqi-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 //偽裝操作,不然不能轉發rs的數據

echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects //偽裝操作,不然不能轉發rs的數據

# 註意區分網卡名字,阿銘的兩個網卡分別為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.23.0/24 -j MASQUERADE //MASQUERADE實現同網段的機器去上網,路由器使用的就是這個功能

# director設置ipvsadm

IPVSADM='/usr/sbin/ipvsadm' //設置一個變量,方便下面命令引用

$IPVSADM -C //清空規則

$IPVSADM -A -t 192.168.38.147:80 -s rr //用來定義lvs 的模式;wlc為算法,可以按需求選擇 lvs 裏面適合的算法

$IPVSADM -a -t 192.168.38.147:80 -r 192.168.23.128:80 -m -w 1 //小規則,-r 指定dir機器IP,-m 指定nat模式,-w指定rs權重

$IPVSADM -a -t 192.168.38.147:80 -r 192.168.23.129:80 -m -w 1 //小規則,-r 指定dir機器IP,-m 指定nat模式,-w指定rs權重

保存退出

  • IPVSADM -A -t 192.168.38.147:80 -s lc -p 3 : -A增加一個規則,-t 制定lvs 模式,之後IP 就是dir的IP,-s 指定算法;-p 指定超時時間(數據包轉發超時時間)

  • 超時時間解釋:

    • 用戶1訪問的是a機器,-p 的意思就是在同一個時間,一直在同一臺機器上進行請求

3.執行腳本,若是沒輸出,表示腳本沒有錯誤

[root@tianqi-01 ~]# sh /usr/local/sbin/lvs_nat.sh

[root@tianqi-01 ~]#

NAT模式效果測試

  • NAT模式效果測試

    • 兩臺rs上都安裝nginx

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

    • 瀏覽器裏訪問192.168.142.147,多訪問幾次看結果差異

1.首先查看B機器和C機器上的nginx服務是否開啟

B機器

[root@tianqi-02 ~]# ps aux |grep nginx

root 1318 0.0 0.0 112660 976 pts/0 S+ 01:13 0:00 grep --color=auto nginx

[root@tianqi-02 ~]# systemctl start nginx

[root@tianqi-02 ~]# ps aux |grep nginx

root 1334 0.0 0.1 122908 2104 ? Ss 01:14 0:00 nginx: master process /usr/sbin/nginx

nginx 1335 0.0 0.1 123292 3136 ? S 01:14 0:00 nginx: worker process

nginx 1336 0.0 0.1 123292 3136 ? S 01:14 0:00 nginx: worker process

root 1338 0.0 0.0 112660 976 pts/0 S+ 01:14 0:00 grep --color=auto nginx

[root@tianqi-02 ~]#

C機器

[root@localhost ~]# ps aux |grep nginx

root 2154 0.0 0.0 112644 952 pts/0 R+ 17:05 0:00 grep --color=auto nginx

[root@localhost ~]# systemctl start nginx

[root@localhost ~]# ps aux |grep nginx

root 2167 0.0 0.2 122864 2104 ? Ss 17:06 0:00 nginx: master process /usr/sbin/nginx

nginx 2168 0.0 0.3 123248 3120 ? S 17:06 0:00 nginx: worker process

root 2170 0.0 0.0 112644 948 pts/0 R+ 17:06 0:00 grep --color=auto nginx

[root@localhost ~]#

2設置兩臺rs的主頁,做一個區分,也就是說直接curl兩臺rs的ip時,得到不同的結果

  • 編輯B機器的索引頁

[root@localhost ~]# curl localhost

backup backup.

[root@localhost ~]# vim /usr/share/nginx/html/index.html


aming02.

保存退出

[root@localhost ~]# curl localhost

aming02.

[root@localhost ~]#

  • 編輯C機器的索引頁

[root@tianqi-02 ~]# vim /usr/share/nginx/html/index.html


aming03.

保存退出

[root@tianqi-02 ~]# curl localhost

aming03.

[root@tianqi-02 ~]#

3.這時瀏覽器訪問模擬的公網IP,即192.168.204.1,若是瀏覽器訪問不成功,可用curl測試

[root@tianqi-01 ~]# curl 192.168.38.147

aming02.

[root@tianqi-01 ~]# curl 192.168.38.147

aming02.

4.查看A機器上的nat規則

[root@tianqi-01 ~]# iptables -t nat -nvL

Chain PREROUTING (policy ACCEPT 29 packets, 1560 bytes)

pkts bytes target prot opt in out source destination


Chain INPUT (policy ACCEPT 2 packets, 156 bytes)

pkts bytes target prot opt in out source destination


Chain OUTPUT (policy ACCEPT 4 packets, 240 bytes)

pkts bytes target prot opt in out source destination


Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

0 0 MASQUERADE all -- * * 192.168.23.0/24 0.0.0.0/0

[root@tianqi-01 ~]#

5.查看 ipvsadm 規則

[root@tianqi-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.38.147:80 lc persistent 3

-> 192.168.23.128:80 Masq 1 0 0

-> 192.168.23.129:80 Masq 1 0 0

[root@tianqi-01 ~]#

友情鏈接:http://www.apelearn.com阿銘linux

十五周五次課