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

擴展html

lvs 三種模式詳解 :linux

http://www.it165.net/admin/html/201401/2248.htmlnginx

lvs幾種算法 :算法

http://www.aminglinux.com/bbs/thread-7407-1-1.htmlapache

關於arp_ignore和 arp_announce :vim

http://www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.htmlwindows

lvs原理相關的 :centos

http://blog.csdn.net/pi9nc/article/details/23380589bash

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)

clipboard.png

clipboard.png

clipboard.png

18.8 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

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(僅主機模式)

clipboard.png

2. 編輯ens37網卡配置文件 :

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

設定ip爲(僅主機網段): 192.168.145.128

添加內容(不用設置網關) :

NAME=ens37

DEVICE=ens37

ONBOOT=no

IPADDR= 192.168.145.128

PREFIX=24

clipboard.png

3. 重啓網絡服務命令:(重啓network.service網絡服務)

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

4. 激活ens37網卡

[root@hao-01 ~]# ifup ens37

5. windows系統,pingens37(僅主機)外網ip

clipboard.png

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

1. 關閉firewalld防火牆 :

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

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

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

查看firewalld防火牆是否關閉

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

clipboard.png

2. 安裝centos 6系統中的iptables防火牆工具:

[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

查看firewalld防火牆是否關閉

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

clipboard.png

3. 臨時關閉getenforce防火牆

[root@hao-01 ~]# setenforce 0

永久關閉getenforce防火牆:

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

更改內容:

SELINUX=disabled

clipboard.png

hao2   hao3機器上操做:

ens33網卡網關改爲ip段內網段iphao1內網ip(128)

1. 更改hao2機器ens33網卡配置文件 :

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

clipboard.png

重啓網卡

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

2. 改hao3機器ens33網卡配置文件:

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

clipboard.png

重啓網卡

[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 > /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.223.0/24  -j MASQUERADE

# director設置ipvsadm

IPVSADM='/usr/sbin/ipvsadm'

$IPVSADM -C

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

$IPVSADM -a -t 192.168.145.128:80 -r 192.168.223.143:80 -m -w 1

$IPVSADM -a -t 192.168.145.128:80 -r 192.168.223.144:80 -m -w 1

clipboard.png

3. 執行lvs_nat.sh腳本:

[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 ~]# > /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

clipboard.png

hao3機器(r)上操做:

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

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

2. 搜索nginx是否啓動?

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

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

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

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

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

添加內容(便於和hao2區分):

hao3

5. 查看 :

[root@hao-03 ~]# curl localhost

clipboard.png

hao1機器(dir)上操做測試

1. curl訪問hao1機器(dir) ens37外網ip

[root@hao-01 ~]# curl 192.168.145.128

clipboard.png

相關文章
相關標籤/搜索