搭建LVS負(fù)載均衡群集(理論+實戰(zhàn),理解更輕松)-創(chuàng)新互聯(lián)

LVS負(fù)載均衡群集

理解負(fù)載均衡群集的原理

成都創(chuàng)新互聯(lián)公司長期為近1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為涇縣企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè),涇縣網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

掌握LVS-NAT的部署

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

群集的含義:

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

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

1.使用價格昂貴的小型機、大型機
2.使用普通服務(wù)器構(gòu)建服務(wù)群集

企業(yè)群集分類

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

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

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

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

高可用群集(High Availability Cluster):

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

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

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

負(fù)載均衡群集工作模式分析

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

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

NAT模式

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

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

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

TUN模式

IP隧道(IP Tunnel):

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

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

DR模式

直接路由(Direct Routing):

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

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

負(fù)載均衡群集架構(gòu)

####負(fù)載均衡的結(jié)構(gòu):

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

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

關(guān)于LVS虛擬服務(wù)器

Linux Virtual Server:

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

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

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

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

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

3.最少連接(Least Connections)

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

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

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

使用ipvsadm工具

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

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

NFS共享存儲服務(wù)

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

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

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

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

Demo:LVS負(fù)載均衡群集

環(huán)境準(zhǔn)備:

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

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

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

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

win7-1:客戶端 12.0.0.12

yum在線安裝前置操作:

1.共享存儲服務(wù)器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é)點服務(wù)器CentOS 7-2和7-3兩臺節(jié)點服務(wù)器:
[root@localhost ~]# yum install httpd -y
3.調(diào)度器網(wǎng)關(guān)CentOS 7-1操作:
//要先添加一個網(wǎng)絡(luò)適配器,成為兩個網(wǎng)卡
[root@localhost ~]# yum install ipvsadm -y

共享存儲服務(wù)器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)關(guān)
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/        //提升權(quán)限
[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é)點服務(wù)器(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)關(guān)
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          
//設(shè)置網(wǎng)絡(luò)適配器為僅主機
[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
此時回到存儲服務(wù)器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é)點服務(wù)器上使用火狐瀏覽器輸入:127.0.0.1看是否可以顯示我們寫入的網(wǎng)頁內(nèi)容:

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

節(jié)點服務(wù)器(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)關(guān)
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   //設(shè)置網(wǎng)絡(luò)適配器為僅主機
[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
此時回到存儲服務(wù)器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é)點服務(wù)器上使用火狐瀏覽器輸入:127.0.0.1看是否可以顯示我們寫入的網(wǎng)頁內(nèi)容:

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

調(diào)度,網(wǎng)關(guān)服務(wù)器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)卡設(shè)置為僅主機模式,網(wǎng)卡配置為靜態(tài)地址,同時關(guān)閉防火墻:

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

此時用內(nèi)部的節(jié)點服務(wù)器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通,說明回應(yīng)沒有問題
加載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
啟動服務(wù):(注意:在CentOS 7中必須要先保存在啟動服務(wù),否則會報錯?。。。?/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)核虛擬服務(wù)器表中的所有記錄
ipvsadm -A -t 12.0.0.1:80 -s rr         //添加新的虛擬的服務(wù)器
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負(fù)載均衡群集(理論+實戰(zhàn),理解更輕松)

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

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)頁標(biāo)題:搭建LVS負(fù)載均衡群集(理論+實戰(zhàn),理解更輕松)-創(chuàng)新互聯(lián)
分享URL:http://bm7419.com/article2/cediic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊定制網(wǎng)站、ChatGPT、網(wǎng)站設(shè)計公司、品牌網(wǎng)站制作、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)