天天看點

18.6 負載均衡叢集介紹;18.7 LVS介紹;18.9 LVS NAT模式搭建(上);18.10 LVS NAT模式搭建(下)

擴充:

lvs 三種模式詳解  

<a href="http://www.it165.net/admin/html/201401/2248.html" target="_blank">http://www.it165.net/admin/html/201401/2248.html</a>

lvs幾種算法 

<a href="http://www.aminglinux.com/bbs/thread-7407-1-1.html" target="_blank">http://www.aminglinux.com/bbs/thread-7407-1-1.html</a>

關于arp_ignore和 arp_announce 

<a href="http://www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html" target="_blank">http://www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html</a>

18.6 負載均衡叢集介紹

1. 主流開源軟體LVS、keepalived、haproxy、nginx等

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

3. keepalived的負載均衡功能其實就是lvs

4. lvs這種4層的負載均衡是可以分發除80外的其他端口通信的,比如MySQL的,而nginx僅僅支援http,https,mail,haproxy也支援MySQL這種

5. 相比較來說,LVS這種4層的更穩定,能承受更多的請求,而nginx這種7層的更加靈活,能實作更多的個性化需求

18.7 LVS介紹;

1. LVS是由國人章文嵩開發

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

3. LVS最新版本基于Linux核心2.6,有好多年不更新了

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

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

前四條算法較為重要!!!

1. 輪詢 Round-Robin  rr

2. 權重輪詢 Weight Round-Robin wrr

3. 最小連接配接 Least-Connection lc

4. 權重最小連接配接 Weight Least-Connection wlc

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

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

7. 目标位址散列排程 Destination Hashing dh

8. 源位址散列排程 Source Hashing  sh

18.9 LVS NAT模式搭建(上)

NAT模式搭建 – 準備工作

準備三台機器:hao1  hao2  hao3

hao1機器操作:

hao1機器作為:分發器,也叫排程器(簡寫為dir)

1. 添加一個新網卡,選擇僅主機模式

ens33網卡内網:192.168.211.128(ANT模式)

ens37網卡外網:192.168.47.128(僅主機模式)

[root@hao-01 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens37

設定ip為(僅主機網段): 192.168.47.128

添加内容(不用設定網關):

NAME=ens37

DEVICE=ens37

ONBOOT=no

IPADDR=192.168.47.128

PREFIX=24

<a href="https://s4.51cto.com/wyfs02/M01/05/F0/wKiom1muo6KR5_pKAABhSTjRBrA805.png" target="_blank"></a>

3. 重新開機網絡服務指令:(重新開機network.service網絡服務)

[root@hao-01 ~]# systemctl restart network.service

4. 激活ens37網卡

[root@hao-01 ~]# ifup ens37

5. 在windows系統,ping下ens37(僅主機)外網ip:

<a href="https://s1.51cto.com/wyfs02/M02/05/F0/wKiom1muo7jhN34pAAA6qt7s0ks065.png" target="_blank"></a>

hao1   hao2   hao3機器都要執行下面關閉防火牆指令:

1. 關閉firewalld防火牆:

[root@hao-01 ~]# systemctl stop firewalld

設定開機不啟動firewalld防火牆:

[root@hao-01 ~]# systemctl disable firewalld

檢視firewalld防火牆是否關閉?

[root@hao-01 ~]# iptables -nvL

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

如果上面安裝慢,臨時重命名epel.repo,再yum 安裝

(記得改回重命名!):

mv /etc/yum.repos.d/epel.repo  /etc/yum.repos.d/epel.repo1

啟用iptables:

[root@hao-01 ~]# systemctl enable iptables

開啟iptables:

[root@hao-01 ~]# systemctl start iptables

清除防火牆規則:

[root@hao-01 ~]# iptables -F

關閉iptables:

[root@hao-01 ~]# service iptables save

[root@hao-01 ~]# setenforce 0

永久關閉getenforce防火牆:

[root@hao-01 ~]# vi /etc/selinux/config

更改内容:

SELINUX=disabled

ens33網卡網關改成ip段為内網段,ip為hao1内網ip(128)

1. 更改hao2機器ens33網卡配置檔案:

[root@hao-02 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33

<a href="https://s3.51cto.com/wyfs02/M02/A4/A1/wKioL1muo_qDr4auAAAQwVpCgIQ597.png" target="_blank"></a>

重新開機網卡:

[root@hao-02 ~]# systemctl restart network

2. 更改hao3機器ens33網卡配置檔案:

[root@hao-03 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33

<a href="https://s2.51cto.com/wyfs02/M00/05/F0/wKiom1mupCyRO-ihAAAQ-z04_lU641.png" target="_blank"></a>

[root@hao-03 ~]# systemctl restart network

18.10 LVS NAT模式搭建(下)

hao1機器(dir)上操作:

1. 在hao1機器(dir)上,安裝 ipvsadm:

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

2. 在hao1機器(dir)上,編寫lvs_nat.sh腳本:

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

添加内容:

#! /bin/bash

# director 伺服器上開啟路由轉發功能

echo 1 &gt; /proc/sys/net/ipv4/ip_forward

# 關閉icmp的重定向

echo 0 &gt; /proc/sys/net/ipv4/conf/all/send_redirects

echo 0 &gt; /proc/sys/net/ipv4/conf/default/send_redirects

# 注意區分網卡名字,阿銘的兩個網卡分别為ens33和ens37

echo 0 &gt; /proc/sys/net/ipv4/conf/ens33/send_redirects

echo 0 &gt; /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.211.0/24  -j MASQUERADE

# director設定ipvsadm

IPVSADM='/usr/sbin/ipvsadm'

$IPVSADM -C

$IPVSADM -A -t 192.168.47.128:80 -s rr

$IPVSADM -a -t 192.168.47.128:80 -r 192.168.211.129:80 -m -w 1

$IPVSADM -a -t 192.168.47.128:80 -r 192.168.211.130:80 -m -w 1

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

hao2機器(r)上操作:

1. 啟動nginx(yum安裝的nginx):

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

2. 搜尋nginx是否啟動?

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

3. 清空index.html内容(yum安裝的nginx):

[root@hao-02 ~]# &gt; /usr/share/nginx/html/index.html

4. 編輯index.html(yum安裝的nginx):

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

添加内容(便于和hao3區分):

hao2

5. 檢視:

[root@hao-02 ~]# curl localhost

hao3機器(r)上操作:

[root@hao-03 ~]# systemctl start nginx

[root@hao-03 ~]# ps aux |grep nginx

[root@hao-03 ~]# &gt; /usr/share/nginx/html/index.html

[root@hao-03 ~]# vim /usr/share/nginx/html/index.html

添加内容(便于和hao2區分):

hao3

[root@hao-03 ~]# curl localhost

hao1機器(dir)上操作測試:

1. curl通路hao1機器(dir) ens37外網ip:

[root@hao-01 ~]# curl 192.168.47.128

本文轉自 主内安詳 51CTO部落格,原文連結:http://blog.51cto.com/zhuneianxiang/1962940,如需轉載請自行聯系原作者

繼續閱讀