溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

搭建LVS負載均衡群集(理論+實戰(zhàn),理解更輕松)

發(fā)布時間:2020-02-26 15:22:43 來源:網(wǎng)絡 閱讀:5127 作者:JarryZ 欄目:MySQL數(shù)據(jù)庫

LVS負載均衡群集

理解負載均衡群集的原理

掌握LVS-NAT的部署

企業(yè)群集應用概述

群集的含義:

1.Cluster,集群、群集
2.由多臺主機構成,但對外只表現(xiàn)為一個整體

在互聯(lián)網(wǎng)應用中,隨著站點對硬件性能、響應速度、服務穩(wěn)定性、數(shù)據(jù)可靠性等要求越來越高,單臺服務器力不從心
解決方法:

1.使用價格昂貴的小型機、大型機
2.使用普通服務器構建服務群集

企業(yè)群集分類

根據(jù)群集所針對的目標差異,可分為三種類型:

1.負載均衡群集(輪詢,最小連接的加權重)
2.高可用群集(訪問的速度,可靠性)
3.高性能運算群集(并發(fā)處理任務)

負載均衡群集(Load Balance Cluster):

1.以提高應用系統(tǒng)的響應能力、盡可能處理更多的訪問請求、減少延遲為目標,獲得高并發(fā)、負載(LB) 的整體性能
2.LB的負載分配依賴于主節(jié)點的分流算法

高可用群集(High Availability Cluster):

1.以提高應用系統(tǒng)的可靠性、盡可能地減少中斷時間為目標,確保服務的連續(xù)性,達到高可用(HA) 的容錯效果
2.HA的工作方式包括雙工和主從兩種模式

高性能運算群集(High Performance Computer Cluster):

1.以提高應用系統(tǒng)的CPU運算速度、擴展硬件資源和分析能力為目標,獲得相當于大型、超級計算機的高性能運算(HPC)能力
2.高性能運算群集的高性能依賴于"分布式運算”、“并行計算” ,通過專用硬件和軟件將多個服務器的CPU、內(nèi)存等資源整合在一起,實現(xiàn)只有大型、超級計算機才具備的計算能力

負載均衡群集工作模式分析

負載均衡群集是目前企業(yè)用得最多的群集類型
群集的負載調(diào)度技術有三種工作模式:

1.地址轉(zhuǎn)換
2.IP隧道
3.直接路由(DR)

NAT模式

地址轉(zhuǎn)換(Network Address Translation):

1.簡稱NAT模式,類似于防火墻的私有網(wǎng)絡結(jié)構,負載調(diào)度器作為所有服務器節(jié)點的網(wǎng)關,即作為客戶機的訪問入口,也是各節(jié)點回應客戶機的訪問出口
2.服務器節(jié)點使用私有IP地址,與負載調(diào)度器位于同一個物理網(wǎng)絡,安全性要優(yōu)于其他兩種方式

搭建LVS負載均衡群集(理論+實戰(zhàn),理解更輕松)

TUN模式

IP隧道(IP Tunnel):

1.簡稱TUN模式,采用開放式的網(wǎng)絡結(jié)構,負載調(diào)度器僅作為客戶機的訪問入口,各節(jié)點通過各自的Internet連接直接回應客戶機,而不再經(jīng)過負載調(diào)度器
2.服務器節(jié)點分散在互聯(lián)網(wǎng)中的不同位置,具有獨立的公網(wǎng)IP地址,通過專用IP隧道與負載調(diào)度器相互通信

搭建LVS負載均衡群集(理論+實戰(zhàn),理解更輕松)

DR模式

直接路由(Direct Routing):

1.簡稱DR模式,采用半開放式的網(wǎng)絡結(jié)構,與TUN模式的結(jié)構類似,但各節(jié)點并不是分散在各地,而是與調(diào)度器位于同一個物理網(wǎng)絡
2.負載調(diào)度器與各節(jié)點服務器通過本地網(wǎng)絡連接,不需要建立專用的IP隧道

搭建LVS負載均衡群集(理論+實戰(zhàn),理解更輕松)

負載均衡群集架構

####負載均衡的結(jié)構:

1.第一層,負載調(diào)度器(Load Balancer或Director)
2.第二層,服務器池(Server Pool)
3.第三層,共享存儲(Share Storage)

搭建LVS負載均衡群集(理論+實戰(zhàn),理解更輕松)

關于LVS虛擬服務器

Linux Virtual Server:

1.針對Linux內(nèi)核的負載均衡解決方案
2.1998年5月,由我國的章文嵩博士創(chuàng)建
3.官方網(wǎng)站: http://www.linuxvirtualserver.org/

LVS的負載調(diào)度算法
1.輪詢(Round Robin):

①將收到的訪問請求按照順序輪流分配給群集中的各節(jié)點(真實服務器)
②均等地對待每一臺服務器, 而不管服務器實際的連接數(shù)和系統(tǒng)負載

2.加權輪詢(Weighted Round Robin):

①根據(jù)真實服務器的處理能力輪流分配收到的訪問請求,調(diào)度器可以自動查詢各節(jié)點的負載情況,并動態(tài)調(diào)整其權重
②保證處理能力強的服務器承擔更多的訪問流量

3.最少連接(Least Connections)

①根據(jù)真實服務器已建立的連接數(shù)進行分配,將收到的訪問請求優(yōu)先分配給連接數(shù)最少的節(jié)點

4.加權最少連接(Weighted Least Connections)

①在服務器節(jié)點的性能差異較大的情況下,可以為真實服務器自動調(diào)整權重
②權重較高的節(jié)點將承擔更大比例的活動連接負載

使用ipvsadm工具

VS群集創(chuàng)建與管理

搭建LVS負載均衡群集(理論+實戰(zhàn),理解更輕松)

NFS共享存儲服務

Network File System,網(wǎng)絡文件系統(tǒng)

1.依賴于RPC (遠端過程調(diào)用)
2.需安裝nfs-utils、rpcbind軟件包
3.系統(tǒng)服務: nfs、 rpcbind
4.共享配置文件: /etc/exports

在客戶機中訪問NFS共享資源

1.安裝rpcbind軟件包,并啟動rpcbind服務
2.手動掛載NFS共享目錄
3.fstab自動掛載設置

Demo:LVS負載均衡群集

環(huán)境準備:

CentOS 7-1:調(diào)度器,網(wǎng)關(需要兩塊網(wǎng)卡)外:12.0.0.1 內(nèi):192.168.200.1

CentOS 7-2:網(wǎng)站服務器(Apache)192.168.200.110

CentOS 7-3:網(wǎng)站服務器(Apache)192.168.200.120

CentOS 7-4:提供共享存儲 192.168.200.130

win7-1:客戶端 12.0.0.12

yum在線安裝前置操作:

1.共享存儲服務器CentOS 7-4的操作:
[root@localhost ~]# rpm -q nfs-utils
nfs-utils-1.3.0-0.48.el7.x86_64
[root@localhost ~]# rpm -q rpcbind 
rpcbind-0.2.0-42.el7.x86_64
2.節(jié)點服務器CentOS 7-2和7-3兩臺節(jié)點服務器:
[root@localhost ~]# yum install httpd -y
3.調(diào)度器網(wǎng)關CentOS 7-1操作:
//要先添加一個網(wǎng)絡適配器,成為兩個網(wǎng)卡
[root@localhost ~]# yum install ipvsadm -y

共享存儲服務器CentOS 7-4:

//先將網(wǎng)卡更改為僅主機模式
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO="static"      //將dhcp改為static
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.200.130      //在末行下插入:IP,子網(wǎng),網(wǎng)關
NETMASK=255.255.255.0
GATEWAY=192.168.200.1
修改完成后輸入:wq保存退出
[root@localhost ~]# service network restart 
Restarting network (via systemctl):                        [  確定  ]
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start nfs.service
[root@localhost ~]# systemctl status nfs.service
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
   Active: active (exited) since 二 2019-11-26 17:42:05 CST; 11s ago
......省略多行,狀態(tài)為Active說明正常
[root@localhost ~]# systemctl start rpcbind.service
[root@localhost ~]# systemctl status rpcbind.service
● rpcbind.service - RPC bind service
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; indirect; vendor preset: enabled)
   Active: active (running) since 二 2019-11-26 17:40:23 CST; 4min 26s ago
......省略多行,狀態(tài)為Active說明正常
[root@localhost ~]# vim /etc/exports
/usr/share *(ro,sync)
/opt/accp 192.168.200.0/24(rw,sync)
/opt/benet 192.168.200.0/24(rw,sync)
//加入以上內(nèi)容之后輸入:wq保存退出
[root@localhost ~]# cd /opt/
[root@localhost opt]# mkdir benet accp
[root@localhost opt]# ls -l
總用量 0
drwxr-xr-x. 2 root root 6 11月 26 17:50 accp
drwxr-xr-x. 2 root root 6 11月 26 17:50 benet
drwxr-xr-x. 2 root root 6 3月  26 2015 rh
[root@localhost opt]# chmod 777 accp/ benet/        //提升權限
[root@localhost opt]# ls -l
總用量 0
drwxrwxrwx. 2 root root 6 11月 26 17:50 accp
drwxrwxrwx. 2 root root 6 11月 26 17:50 benet
drwxr-xr-x. 2 root root 6 3月  26 2015 rh
[root@localhost opt]# exportfs -rv      //進行發(fā)布
exporting 192.168.200.0/24:/opt/benet
exporting 192.168.200.0/24:/opt/accp
exporting *:/usr/share

節(jié)點服務器(CentOS 7-2)上的操作:

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO="static"      //將dhcp改為static
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.200.110      //在末行下插入:IP,子網(wǎng),網(wǎng)關
NETMASK=255.255.255.0
GATEWAY=192.168.200.1
修改完成后輸入:wq保存退出
[root@localhost ~]# service network restart 
Restarting network (via systemctl):                        [  確定  ]
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start httpd.service
[root@localhost ~]# netstat -ntap | grep 80 
tcp6       0      0 :::80               :::*            LISTEN          7315/httpd          
//設置網(wǎng)絡適配器為僅主機
[root@localhost ~]# ping 192.168.200.130
PING 192.168.200.130 (192.168.200.130) 56(84) bytes of data.
64 bytes from 192.168.200.130: icmp_seq=1 ttl=64 time=0.754 ms
64 bytes from 192.168.200.130: icmp_seq=2 ttl=64 time=0.368 ms
64 bytes from 192.168.200.130: icmp_seq=3 ttl=64 time=0.398 ms
[root@localhost ~]# showmount -e 192.168.200.130
Export list for 192.168.200.130:
/usr/share *
/opt/benet 192.168.200.0/24
/opt/accp  192.168.200.0/24
[root@localhost ~]# mount.nfs 192.168.200.130:/opt/accp /var/www/html/
[root@localhost ~]# df -h
文件系統(tǒng)           容量    已用  可用   已用%  掛載點
/dev/sda2          20G   4.3G   16G  22%  /
devtmpfs           898M     0  898M   0%  /dev
tmpfs              912M     0  912M   0%  /dev/shm
tmpfs              912M  9.0M  903M   1%  /run
tmpfs              912M     0  912M   0%  /sys/fs/cgroup
/dev/sda1          6.0G  174M  5.9G   3%  /boot
/dev/sda5          10G    54M   10G   1%  /home
tmpfs              183M  4.0K  183M   1%  /run/user/42
tmpfs              183M   20K  183M   1%  /run/user/0
/dev/sr0           4.3G  4.3G     0  100% /run/media/root/CentOS 7 x86_64
192.168.200.130:/opt/accp   20G  3.4G   17G   17% /var/www/html
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is accp web" > index.html
[root@localhost html]# ls
index.html
此時回到存儲服務器CentOS 7-4上看是否有此文件:
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
accp  benet  rh
[root@localhost opt]# cd accp/
[root@localhost accp]# ls
index.html
[root@localhost accp]# cat index.html 
this is accp web
//此時有我們新建的index.html文件
驗證:在CentOS 7-2節(jié)點服務器上使用火狐瀏覽器輸入:127.0.0.1看是否可以顯示我們寫入的網(wǎng)頁內(nèi)容:

搭建LVS負載均衡群集(理論+實戰(zhàn),理解更輕松)

節(jié)點服務器(CentOS 7-3)上的操作:

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO="static"      //將dhcp改為static
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.200.120      //在末行下插入:IP,子網(wǎng),網(wǎng)關
NETMASK=255.255.255.0
GATEWAY=192.168.200.1
//修改完成后輸入:wq保存退出
[root@localhost ~]# service network restart 
Restarting network (via systemctl):                        [  確定  ]
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start httpd.service
[root@localhost ~]# netstat -ntap | grep 80 
tcp6       0      0 :::80               :::*            LISTEN          7315/httpd   //設置網(wǎng)絡適配器為僅主機
[root@localhost ~]# ping 192.168.200.130
PING 192.168.200.130 (192.168.200.130) 56(84) bytes of data.
64 bytes from 192.168.200.130: icmp_seq=1 ttl=64 time=0.532 ms
64 bytes from 192.168.200.130: icmp_seq=2 ttl=64 time=1.01 ms
64 bytes from 192.168.200.130: icmp_seq=3 ttl=64 time=0.940 ms
[root@localhost ~]# showmount -e 192.168.200.130
Export list for 192.168.200.130:
/usr/share *
/opt/benet 192.168.200.0/24
/opt/accp  192.168.200.0/24
[root@localhost ~]# mount.nfs 192.168.200.130:/opt/benet /var/www/html/
[root@localhost ~]# df -h
文件系統(tǒng)            容量  已用    可用  已用%       掛載點
/dev/sda2           20G  3.4G   17G   17%    /
devtmpfs           898M     0  898M    0%    /dev
tmpfs              912M     0  912M    0%    /dev/shm
tmpfs              912M  9.0M  903M    1%    /run
tmpfs              912M     0  912M    0%    /sys/fs/cgroup
/dev/sda1          6.0G  174M  5.9G    3%    /boot
/dev/sda5          10G   54M   10G    1%     /home
tmpfs              183M  4.0K  183M    1%    /run/user/42
tmpfs              183M   20K  183M    1%    /run/user/0
/dev/sr0           4.3G  4.3G     0  100%    /run/media/root/CentOS 7 x86_64
192.168.200.130:/opt/benet   20G  3.4G   17G   17% /var/www/html
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is benet web" > index.html
[root@localhost html]# ls
index.html
此時回到存儲服務器CentOS 7-4上看是否有此文件:
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
accp  benet  rh
[root@localhost opt]# cd accp/
[root@localhost accp]# ls
index.html
[root@localhost accp]# cat index.html 
this is benet web
//此時有我們新建的index.html文件
驗證:在CentOS 7-3節(jié)點服務器上使用火狐瀏覽器輸入:127.0.0.1看是否可以顯示我們寫入的網(wǎng)頁內(nèi)容:

搭建LVS負載均衡群集(理論+實戰(zhàn),理解更輕松)

調(diào)度,網(wǎng)關服務器CentOS 7-1的操作:

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-ens33  ifdown-ppp       ifup-ib      ifup-Team
ifcfg-lo     ifdown-routes    ifup-ippp    ifup-TeamPort
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens36
[root@localhost network-scripts]# vim ifcfg-ens36
BOOTPROTO="static"      //將dhcp改為static
NAME="ens36"        //把名稱改為ens36
UUID號刪除
DEVICE="ens36"      //把名稱改為ens36
ONBOOT="yes"
IPADDR=12.0.0.1     //在末行下插入:IP,子網(wǎng)
NETMASK=255.255.255.0
//修改完成后輸入:wq保存退出
[root@localhost network-scripts]# vim ifcfg-ens33
BOOTPROTO="static"      //將dhcp改為static
DEVICE="ens33"
ONBOOT="yes"        //在末行下插入:IP,子網(wǎng)
IPADDR=192.168.200.1
NETMASK=255.255.255.0
//修改完成后輸入:wq保存退出
[root@localhost network-scripts]# service network restart 
Restarting network (via systemctl):                        [  確定  ]
[root@localhost network-scripts]# vim /etc/sysctl.conf 
//在最后一行按o轉(zhuǎn)下行插入
net.ipv4.ip_forward=1
//修改完成后輸入:wq保存退出
[root@localhost network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
[root@localhost network-scripts]# iptables -t nat -F
[root@localhost network-scripts]# iptables -F
[root@localhost network-scripts]# iptables -t nat -A POSTROUTING -o ens36 -s 192.168.200.0/24 -j SNAT --to-source 12.0.0.1
win7-1中驗證轉(zhuǎn)發(fā):
先把網(wǎng)卡設置為僅主機模式,網(wǎng)卡配置為靜態(tài)地址,同時關閉防火墻:

搭建LVS負載均衡群集(理論+實戰(zhàn),理解更輕松)

此時用內(nèi)部的節(jié)點服務器ping:12.0.0.12測試能否ping通:
[root@localhost html]# ping 12.0.0.12
PING 12.0.0.12 (12.0.0.12) 56(84) bytes of data.
64 bytes from 12.0.0.12: icmp_seq=1 ttl=127 time=1.14 ms
64 bytes from 12.0.0.12: icmp_seq=2 ttl=127 time=1.78 ms
64 bytes from 12.0.0.12: icmp_seq=3 ttl=127 time=1.02 ms
//此時可以ping通,說明回應沒有問題
加載LVS內(nèi)核模塊:
[root@localhost network-scripts]# modprobe ip_vs
[root@localhost network-scripts]# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port Forward Weight ActiveConn InActConn
啟動服務:(注意:在CentOS 7中必須要先保存在啟動服務,否則會報錯!?。。?/h5>
[root@localhost network-scripts]# ipvsadm --save > /etc/sysconfig/ipvsadm
[root@localhost network-scripts]# systemctl start ipvsadm.service 
編寫配置規(guī)則:
[root@localhost network-scripts]# cd /opt/
[root@localhost opt]# vim nat.sh
#!/bin/bash
ipvsadm -C          //清除內(nèi)核虛擬服務器表中的所有記錄
ipvsadm -A -t 12.0.0.1:80 -s rr         //添加新的虛擬的服務器
ipvsadm -a -t 12.0.0.1:80 -r 192.168.200.110:80 -m
ipvsadm -a -t 12.0.0.1:80 -r 192.168.200.120:80 -m
ipvsadm
//輸入完成后輸入:wq保存退出
[root@localhost opt]# source nat.sh
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  localhost.localdomain:http rr
  -> 129.168.200.110:http         Masq    1      0          0         
  -> 129.168.200.120:http         Masq    1      0          0         
驗證:使用win7-1客戶終端訪問12.0.0.1,看是否可以成功:

搭建LVS負載均衡群集(理論+實戰(zhàn),理解更輕松)

此時可以訪問,說明LVS負載均衡群集成功提供服務,試驗成功!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI