Nginx與lvs詳解

詳細(xì)描述常見(jiàn)nginx常用模塊和模塊的使用示例

nginx常見(jiàn)的模塊分類(lèi):

核心模塊:core module
Nginx 核心功能模塊負(fù)責(zé)Nginx的全局應(yīng)用,主要對(duì)應(yīng)主配置文件的Main區(qū)塊和Events區(qū)塊

  • Main 區(qū)塊:常見(jiàn)的配置如下
user nginx;
    設(shè)置運(yùn)行nginx的用戶(hù)名,每個(gè)指令都心“ ; ”結(jié)尾

worker_processes auto;
    配置工作進(jìn)程的數(shù)量,應(yīng)用小于等于當(dāng)前主機(jī)的物理核心數(shù)
    auto表示自動(dòng)檢查內(nèi)核核心數(shù),并啟動(dòng)

error_log /var/log/nginx/error.log;
    錯(cuò)誤日志文件路徑

pid /run/nginx.pid;
    指定存儲(chǔ)nginx主進(jìn)程進(jìn)程號(hào)碼的文件路徑

load_module file
    指明要裝載的動(dòng)態(tài)模塊

include file | mask
    指明包含進(jìn)來(lái)的其他配置文件

worker_cpu_affinity auto [cpumask]
    cpu綁定的配置,auto表示自動(dòng)綁定cpu核心
    cpumask表示自定義核心數(shù),表示如下:
        CPU MASK:
            00000000:8個(gè)0表示cpu編號(hào)
            00000001:0號(hào)cpu
            00000010:1號(hào)cpu
            00000100:2號(hào)cpu
                          ......
            10000000:8號(hào)cpu

worker_priority number
    指定worker進(jìn)程的nice值
    設(shè)定worker進(jìn)程優(yōu)先級(jí)在[-20,20]之間

worker_rlimit_nofile number
    worker進(jìn)程所能夠打開(kāi)的文件數(shù)量上限

daemon on|off  是否以守護(hù)進(jìn)程方式運(yùn)行Nignx

master_process on|off
    是否以master/worker模型運(yùn)行nginx;默認(rèn)為on

  • Events 區(qū)塊:在配置文件中以events{}形式配置,常見(jiàn)的選項(xiàng)有:
worker_connections 1024;
#單進(jìn)程所能夠打開(kāi)的最大并發(fā)連接數(shù)數(shù)量

user select | poll
#指明并發(fā)連接請(qǐng)求的處理方法(select | poll)

accept_mutex on | oof
處理新的連接請(qǐng)求的方法;
on意味著由各worker輪流處理新請(qǐng)求
off意味著每個(gè)新請(qǐng)求的到達(dá)都會(huì)通知所有的worker進(jìn)程

標(biāo)準(zhǔn)模塊:HTTP modules,Standard HTTP modules,Optional HTTP modules,Mail modules等。這些模塊非常豐富,列舉常見(jiàn)的模塊

ngx_http_auth_basic_module模塊:實(shí)現(xiàn)基于用戶(hù)的訪問(wèn)控制,使用basic機(jī)制進(jìn)行用戶(hù)認(rèn)證

auth_basic string | off;認(rèn)證的用戶(hù)字符
auth_basic_user_file file;認(rèn)證的文件路徑
配置示例:
location /admin/ {
    alias /webapps/app1/data/;
    auth_basic "Admin Area";
    auth_basic_user_file /etc/nginx/.ngxpasswd;
                            }

ngx_http_stub_status_module模塊:用于輸出nginx的基本狀態(tài)信息

Active connections: 291 
server accepts handled requests
    16630948 16630948 31070465 
Reading: 6 Writing: 179 Waiting: 106    
                    
Active connections: 活動(dòng)狀態(tài)的連接數(shù);
accepts:已經(jīng)接受的客戶(hù)端請(qǐng)求的總數(shù);
handled:已經(jīng)處理完成的客戶(hù)端請(qǐng)求的總數(shù);
requests:客戶(hù)端發(fā)來(lái)的總的請(qǐng)求數(shù);
Reading:處于讀取客戶(hù)端請(qǐng)求報(bào)文首部的連接的連接數(shù);
Writing:處于向客戶(hù)端發(fā)送響應(yīng)報(bào)文過(guò)程中的連接數(shù);
Waiting:處于等待客戶(hù)端發(fā)出請(qǐng)求的空閑連接數(shù);

配置示例:
location  /basic_status {
    stub_status;
}

ngx_http_ssl_module模塊:ssl安全模塊功能

ssl on | off;
    是否開(kāi)啟ssl安全功能
                        
ssl_certificate file;
    當(dāng)前虛擬主機(jī)使用PEM格式的證書(shū)文件;
                        
ssl_certificate_key file;
    當(dāng)前虛擬主機(jī)上與其證書(shū)匹配的私鑰文件;
                        
ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];
    支持ssl協(xié)議版本,默認(rèn)為后三個(gè);
                        
ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
    builtin[:size]:使用OpenSSL內(nèi)建的緩存,此緩存為每worker進(jìn)程私有;                      
    [shared:name:size]:在各worker之間使用一個(gè)共享的緩存;
                        
ssl_session_timeout time;
    客戶(hù)端一側(cè)的連接可以復(fù)用ssl session cache中緩存 的ssl參數(shù)的有效時(shí)長(zhǎng);

配置示例:
server {
    listen 443 ssl;
    server_name www.magedu.com;
    root /vhosts/ssl/htdocs;
    ssl on;
    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;
    ssl_session_cache shared:sslcache:20m;
}

ngx_http_referer_module模塊:反盜鏈功能

valid_referers none | blocked | server_names | string ...;
    定義referer首部的合法可用值;
                    
        none:請(qǐng)求報(bào)文首部沒(méi)有referer首部;
        blocked:請(qǐng)求報(bào)文的referer首部沒(méi)有值;
        server_names:參數(shù),其可以有值作為主機(jī)名或主機(jī)名模式;
            arbitrary_string:直接字符串,但可使用*作通配符;
            regular expression:被指定的正則表達(dá)式模式匹配到的字符串;要使用~打頭,例如 ~.*\.magedu\.com;

配置示例:
valid_referers none block server_names *.magedu.com *.mageedu.com magedu.* mageedu.* ~\.magedu\.;
                    
if($invalid_referer) {
    return http://www.magedu.com/invalid.jpg;
}

以上僅僅是nginx配置模塊的冰冊(cè)一角,詳細(xì)的配置文檔可以查看官方文檔:http://nginx.org/en/docs/

簡(jiǎn)述Linux集群類(lèi)型、系統(tǒng)擴(kuò)展方式及調(diào)度方法

Linux集群類(lèi)型分為:LB(負(fù)載均衡),HA(高可用),HP(高性能)幾種類(lèi)型。
系統(tǒng)的擴(kuò)展方式一般有向上擴(kuò)展和向外擴(kuò)展兩種,在linux服務(wù)中一般是向外擴(kuò)展。
調(diào)度方法有靜態(tài)方法與動(dòng)態(tài)方法兩天類(lèi)型

靜態(tài)方法:是僅根據(jù)算法本身進(jìn)行調(diào)度主要有以下幾種方法:

  • RR:roundrobin,輪詢(xún)
  • WRR:Weighted RR,加權(quán)輪詢(xún)
  • SH:Source Hashing,實(shí)現(xiàn)session sticky,對(duì)源IP地址進(jìn)行hash;將來(lái)自于同一個(gè)IP地址的請(qǐng)求始終發(fā)往第一次挑中的RS,實(shí)現(xiàn)會(huì)話(huà)綁定
  • DH:DH:Destination Hashing;目標(biāo)地址hash,將發(fā)往同一個(gè)目標(biāo)IP的請(qǐng)求始終轉(zhuǎn)發(fā)至第一次挑中的RS,典型使用場(chǎng)景是正向代理緩存場(chǎng)景中的負(fù)載均衡

動(dòng)態(tài)方法:主要根據(jù)每RS當(dāng)前的負(fù)載狀態(tài)及調(diào)度算法進(jìn)行調(diào)度

  • LC:least connections,最少連接數(shù)算法,計(jì)算方式如下:
    Overhead=activeconns(活動(dòng)鏈接)*256+inactiveconns(非活動(dòng)鏈接)
  • WLC:Weighted LC,權(quán)重最少連接數(shù)算法,計(jì)算方式如下:
    Overhead=(activeconns*256+inactiveconns)/weight(權(quán)重)
  • SED:Shortest Expection Delay,最短期望延遲,計(jì)算方式如下:
    Overhead=(activeconns+1)*256/weight
    此之外還有NQ,LBLC,(動(dòng)態(tài)的DH算法),LBLCR,(帶復(fù)制功能的LBLC)等幾個(gè)算法不進(jìn)行一一列舉了。

簡(jiǎn)述lvs四種集群優(yōu)點(diǎn)及使用場(chǎng)景

四種集群方式分別為:lvs-nat,lvs-dr,lvs-tun,lvs-fullnat。在介紹這四種工作原理之前,了解一下lvs集群中的一些術(shù)語(yǔ)。

vs:Virtual Server,Director,Dispatcher,Balancer
rs:Real Server,upstream server,backend server
CIP:Client IP
VIP:Virtual server IP
RIP:Real server IP
DIP:Director IP

lvs-nat:
多目標(biāo)IP的DNAT,通過(guò)將請(qǐng)求報(bào)文中的目標(biāo)地址和目標(biāo)端口修改為某挑出的RS的RIP地址和PORT端口來(lái)實(shí)現(xiàn)轉(zhuǎn)發(fā)。網(wǎng)絡(luò)拓?fù)鋱D如下:

lvs-nat網(wǎng)絡(luò)拓?fù)鋱D.png

工作原理:當(dāng)client向服務(wù)器(RS集群)發(fā)送請(qǐng)求時(shí),源地址為CIP地址,目標(biāo)地址為VIP,調(diào)度器Diretory收到報(bào)文后發(fā)現(xiàn)是發(fā)給后端RS服務(wù)器的數(shù)據(jù),通過(guò)調(diào)度發(fā)給其中的一臺(tái)RS(假設(shè)為RS1服務(wù)器),于是通過(guò)DIP向RS1的RIP1地址發(fā)送報(bào)文。
RS1服務(wù)器接收?qǐng)?bào)文后,響應(yīng)client的請(qǐng)求,于是RS1通過(guò)RIP1向Director的DIP地址發(fā)送響應(yīng)報(bào)文,Director收到報(bào)文后將源地址IP從RIP改為VIP,目標(biāo)IP不變發(fā)送給client。
這就完成了一次請(qǐng)求與響應(yīng)的過(guò)程,期間client是與Director通信,并不知道RS服務(wù)器的存在。

該方式有以下幾點(diǎn)特點(diǎn):

  • RIP和DIP必須在同一個(gè)IP網(wǎng)絡(luò),且應(yīng)該使用私網(wǎng)地址;RS的網(wǎng)關(guān)要指向DIP
  • 請(qǐng)求報(bào)文和響應(yīng)報(bào)文都必須經(jīng)由Director轉(zhuǎn)發(fā);所有的請(qǐng)求和發(fā)送都經(jīng)過(guò)Director,Director易于造成系統(tǒng)擁塞
  • 支持端口映射,可修改請(qǐng)求報(bào)文的目標(biāo)PORT
  • vs(Director)必須是Linux系統(tǒng),rs可以是任意系統(tǒng)

lvs-nat是典型的反代服務(wù)器的一種應(yīng)用,Director可以做靜態(tài)內(nèi)容的緩存,減輕后端RS服務(wù)器的IP壓力,并且能隱藏后端的RS服務(wù)器

lvs-dr:(Direc Routing)
直接路由,通過(guò)為請(qǐng)求報(bào)文重新封裝一個(gè)MAC首部進(jìn)行轉(zhuǎn)發(fā),源MAC是DIP所在的接口的MAC,目標(biāo)MAC是某挑選出的RS的RIP所在接口的MAC地址;源IP/PORT,以及目標(biāo)IP/PORT均保持不變,由于lvs-dr是基于mac進(jìn)行轉(zhuǎn)發(fā)的,所以Director和各RS都得配置使用VIP,但是Director和各RS都有VIP要保證請(qǐng)求只發(fā)給Director而不是直接發(fā)給RS則需要確保前端路由器將目標(biāo)IP為VIP的請(qǐng)求報(bào)文發(fā)往Director

實(shí)現(xiàn)方法有以下幾種:

  • 在前端網(wǎng)關(guān)做靜態(tài)綁定,直接綁定mac,不進(jìn)行ARP廣播,但是如果Director出現(xiàn)故障則會(huì)出現(xiàn)找不到mac地址或重新綁定的情況。
  • 在RS上使用aprtables
  • 在RS上修改內(nèi)核參數(shù)以限制arp通告及應(yīng)答級(jí)別將apr_announce和apr_ignore這兩個(gè)參數(shù)修改一下就可以了,一般推薦第三種方式。
    其網(wǎng)絡(luò)拓?fù)鋱D如下:


    lvs-dr網(wǎng)絡(luò)拓?fù)鋱D.png

工作原理:CIP向VIP請(qǐng)求報(bào)文,經(jīng)過(guò)route時(shí)發(fā)現(xiàn)VIP在自己的網(wǎng)絡(luò)中,于是就通過(guò)ARP方式將報(bào)文發(fā)給VIP主機(jī),這里的Director和RS都有VIP地址,所以要確保請(qǐng)求報(bào)文只能被Director接收,之后Director通過(guò)調(diào)度在報(bào)文上加上RS的MAC地址把報(bào)文扔給router,touter通過(guò)新的MAC地址找的RS主機(jī),并發(fā)送請(qǐng)求報(bào)告。
RS接收到請(qǐng)求報(bào)文之后發(fā)送用自己的VIP地址(一般不與RIP同網(wǎng)卡,而在lo網(wǎng)卡上)直接通過(guò)router響應(yīng)報(bào)文,響應(yīng)報(bào)文不經(jīng)過(guò)Director,從而減輕Director的負(fù)載壓力。

除了上面提到的mac問(wèn)題以外該方式還有以下幾點(diǎn)特點(diǎn):

  • RS的RIP可以使用私網(wǎng)地址,也可以是公網(wǎng)地址;RIP與DIP在同一IP網(wǎng)絡(luò);RIP的網(wǎng)關(guān)不能指向DIP,以確保響應(yīng)報(bào)文不會(huì)經(jīng)由Director
  • RS跟Director要在同一個(gè)物理網(wǎng)絡(luò)(經(jīng)過(guò)mac地址轉(zhuǎn)發(fā)的)
  • 請(qǐng)求報(bào)文要經(jīng)由Director,但響應(yīng)不能經(jīng)由Director,而是由RS直接發(fā)往Client
  • 不支持端口映射

lvs-dr是為減輕Director負(fù)載壓力的一種實(shí)現(xiàn)方式

lvs-tun:
轉(zhuǎn)發(fā)方式:不修改請(qǐng)求報(bào)文的IP首部(源IP為CIP,目標(biāo)IP為VIP),而是在原IP報(bào)文之外再封裝一個(gè)IP首部(源IP是DIP,目標(biāo)IP是RIP),將報(bào)文發(fā)往調(diào)度挑選出的目標(biāo)RS;
RS直接響應(yīng)給客戶(hù)端(源IP是VIP,目標(biāo)IP是CIP)
其網(wǎng)絡(luò)拓?fù)鋱D如下:

lvs-tun網(wǎng)絡(luò)拓?fù)鋱D.png

工作原理:CIP向VIP發(fā)送請(qǐng)求報(bào)文,通過(guò)router到達(dá)Director,Director接受報(bào)文發(fā)現(xiàn)是集群服務(wù)后端報(bào)文直接在報(bào)文首部再添加RS服務(wù)的RIP地址首部(假設(shè)為RS1),并通過(guò)Internet發(fā)送到RS1服務(wù)器,RS接受報(bào)文后拆封報(bào)文時(shí)第一層的RIP是本機(jī)地址,但是還有一層VIP地址首部,因此RS要支持隧道功能,能理解VIP地址并接受處理請(qǐng)求。
RS向CIP響應(yīng)報(bào)文時(shí),直接使用VIP做源地址,CIP做目標(biāo)地址通過(guò)Internet向Client響應(yīng)報(bào)文。

其特點(diǎn)如下:

  • DIP,VIP,RIP都應(yīng)該是公網(wǎng)地址
  • RS的網(wǎng)關(guān)不能,也不可能指向DIP(響應(yīng)不應(yīng)該Director)
  • 請(qǐng)求報(bào)文要經(jīng)由Director,但響應(yīng)不能經(jīng)由Director
  • 不支持端口映射
  • RS的OS得支持隧道功能:如果請(qǐng)求報(bào)文數(shù)據(jù)超過(guò)MTU(最大報(bào)文數(shù))時(shí),在添加首部會(huì)超過(guò)MTU而被拆分報(bào)文(要理解內(nèi)層的CIP和VIP的首部)

lvs-tun一般應(yīng)用在Director與RS不在同一網(wǎng)段,并且相隔甚遠(yuǎn)的情況下使用。

lvs-fullnat:
通過(guò)同時(shí)修改請(qǐng)求報(bào)文的源IP地址和目標(biāo)IP地址進(jìn)行轉(zhuǎn)發(fā)。
CIP <--> DIP 請(qǐng)求報(bào)文時(shí)源IP從左往右,響應(yīng)時(shí)目標(biāo)IP從右往左
VIP <--> RIP 請(qǐng)求報(bào)文時(shí)目標(biāo)IP從左往右,響應(yīng)時(shí)源IP從右往左

lvs-fullnat網(wǎng)絡(luò)拓?fù)鋱D.png

工作原理:CIP向VIP發(fā)送請(qǐng)求,Director接收?qǐng)?bào)文后知道是后端服務(wù)報(bào)文,于是修改源地址IP為DIP,目標(biāo)地址IP為RIP,經(jīng)過(guò)路由發(fā)送的RS服務(wù)器上。
RS服務(wù)器響應(yīng)報(bào)文時(shí),先通過(guò)RIP地址將報(bào)文發(fā)給Director的DIP,Director接收到報(bào)文后發(fā)現(xiàn)是Client的報(bào)文于是修改源IP為VIP,目標(biāo)IP為CIP通過(guò)Internet發(fā)送給client。

lvs-fullnat的特點(diǎn)如下:

  • VIP是公網(wǎng)地址,RIP和DIP是私網(wǎng)地址,且通常不在同一IP網(wǎng)絡(luò);因此,RIP的網(wǎng)關(guān)一般不會(huì)指向DIP
  • RS收到的請(qǐng)求報(bào)文源地址是DIP,因此,只能響應(yīng)給DIP;但Director還要將其發(fā)往Client
  • 請(qǐng)求和響應(yīng)報(bào)文都經(jīng)由Director
  • 支持端口映射,此類(lèi)型默認(rèn)不支持(需要修改編譯內(nèi)核)

lvs-fullnat框架一般用于Director與RS不在同一機(jī)房但可以通過(guò)內(nèi)網(wǎng)連接的一種方式,內(nèi)核默認(rèn)是不支持的,需要重新編譯內(nèi)核才能實(shí)現(xiàn)。

描述LVS-NAT、LVS-DR的工作原理并實(shí)現(xiàn)配置

lvs-nat和lvs-dr的工作原理上述已經(jīng)說(shuō)明了,這個(gè)主要將如何實(shí)現(xiàn)配置,在配置之前先說(shuō)明一下ipvsadm工具的一般用法,先yum安裝ipvsadm之后執(zhí)行一下ipvsadm -h命令查看幫助信息,具體如下:

[root@zhu ~]# ipvsadm -h
ipvsadm v1.27 2008/5/15 (compiled with popt and IPVS v1.2.1)
Usage:
  ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]] [-M netmask] [--pe persistence_engine] [-b sched-flags]
  ipvsadm -D -t|u|f service-address
  ipvsadm -C
  ipvsadm -R
  ipvsadm -S [-n]
  ipvsadm -a|e -t|u|f service-address -r server-address [options]
  ipvsadm -d -t|u|f service-address -r server-address
  ipvsadm -L|l [options]
  ipvsadm -Z [-t|u|f service-address]
  ipvsadm --set tcp tcpfin udp
  ipvsadm --start-daemon state [--mcast-interface interface] [--syncid sid]
  ipvsadm --stop-daemon state
  ipvsadm -h

Commands:
Either long or short options are allowed.
  --add-service     -A        add virtual service with options
  --edit-service    -E        edit virtual service with options
  --delete-service  -D        delete virtual service
  --clear           -C        clear the whole table
  --restore         -R        restore rules from stdin
  --save            -S        save rules to stdout
  --add-server      -a        add real server with options
  --edit-server     -e        edit real server with options
  --delete-server   -d        delete real server
  --list            -L|-l     list the table
  --zero            -Z        zero counters in a service or all services
  --set tcp tcpfin udp        set connection timeout values
  --start-daemon              start connection sync daemon
  --stop-daemon               stop connection sync daemon
  --help            -h        display this help message

Options:
  --tcp-service  -t service-address   service-address is host[:port]
  --udp-service  -u service-address   service-address is host[:port]
  --fwmark-service  -f fwmark         fwmark is an integer greater than zero
  --ipv6         -6                   fwmark entry uses IPv6
  --scheduler    -s scheduler         one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,
                                      the default scheduler is wlc.
  --pe            engine              alternate persistence engine may be sip,
                                      not set by default.
  --persistent   -p [timeout]         persistent service
  --netmask      -M netmask           persistent granularity mask
  --real-server  -r server-address    server-address is host (and port)
  --gatewaying   -g                   gatewaying (direct routing) (default)
  --ipip         -i                   ipip encapsulation (tunneling)
  --masquerading -m                   masquerading (NAT)
  --weight       -w weight            capacity of real server
  --u-threshold  -x uthreshold        upper threshold of connections
  --l-threshold  -y lthreshold        lower threshold of connections
  --mcast-interface interface         multicast interface for connection sync
  --syncid sid                        syncid for connection sync (default=255)
  --connection   -c                   output of current IPVS connections
  --timeout                           output of timeout (tcp tcpfin udp)
  --daemon                            output of daemon information
  --stats                             output of statistics information
  --rate                              output of rate information
  --exact                             expand numbers (display exact values)
  --thresholds                        output of thresholds information
  --persistent-conn                   output of persistent connection info
  --nosort                            disable sorting output of service/server entries
  --sort                              does nothing, for backwards compatibility
  --ops          -o                   one-packet scheduling
  --numeric      -n                   numeric output of addresses and ports
  --sched-flags  -b flags             scheduler flags (comma-separated)

ipvsadm常用的命令如下:

ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]] [-M netmask] [--pe persistence_engine] [-b sched-flags]
vs服務(wù)相關(guān)的命令
    -A:新增VS
    -E:修改VS
    -D:刪除VS
    -R:重載規(guī)則
    -S:保存規(guī)則(相當(dāng)于ipvsadm-save)
    -C:清空所有
    -L:查看
    -t:tcp協(xié)議
    -u:udp協(xié)議
    -f:firewall MARK,是一個(gè)數(shù)字
    -s:調(diào)度方法,默認(rèn)是wlc
    -p:會(huì)話(huà)保持連接的

---------------------------分割線----------------------------------------

ipvsadm -a|e -t|u|f service-address -r server-address [-g|i|m] [-w weight]
RS相關(guān)的命令
-a,-e,-d,-l,-t|u|f這些命令與VS服務(wù)命令一致,只是大小寫(xiě)轉(zhuǎn)換而已
-r:指明服務(wù)器的IP
-w:權(quán)重

lvs-nat配置:

前期準(zhǔn)備:
各節(jié)點(diǎn)時(shí)間必須同步

Director:

VIP:192.168.80.4(公網(wǎng)地址)
DIP:192.168.10.8(私網(wǎng)地址)
開(kāi)啟核心轉(zhuǎn)發(fā)功能

[root@director ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.80.4  netmask 255.255.255.0  broadcast 192.168.80.255
        inet6 fe80::3f13:7555:73e5:6f08  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:01:ba:2c  txqueuelen 1000  (Ethernet)
        RX packets 122777  bytes 133041537 (126.8 MiB)
        RX errors 0  dropped 2  overruns 0  frame 0
        TX packets 16760  bytes 1547576 (1.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.8  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::a857:646a:90c8:ab64  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:01:ba:36  txqueuelen 1000  (Ethernet)
        RX packets 62  bytes 5983 (5.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 61  bytes 5346 (5.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

---------------------------------分割線----------------------------------

#開(kāi)啟核心轉(zhuǎn)發(fā)功能
[root@director ~]# sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1

RS1:

RIP:192.168.10.9(私網(wǎng)地址并且和Director的私網(wǎng)地址在同一網(wǎng)段)
GW:192.168.10.8(網(wǎng)關(guān)必須指向Director的私網(wǎng)地址)
RS1可以ping通Director的私網(wǎng)地址,安裝nginx或httpd服務(wù),并配置主頁(yè)內(nèi)容

[root@rs1 ~]# ping 192.168.10.8
PING 192.168.10.8 (192.168.10.8) 56(84) bytes of data.
64 bytes from 192.168.10.8: icmp_seq=1 ttl=64 time=0.339 ms
64 bytes from 192.168.10.8: icmp_seq=2 ttl=64 time=2.13 ms
64 bytes from 192.168.10.8: icmp_seq=3 ttl=64 time=0.320 ms
64 bytes from 192.168.10.8: icmp_seq=4 ttl=64 time=0.368 ms
64 bytes from 192.168.10.8: icmp_seq=5 ttl=64 time=0.808 ms
^C
--- 192.168.10.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4004ms
rtt min/avg/max/mdev = 0.320/0.794/2.137/0.695 ms

RS2:
RIP:192.168.10.10(私網(wǎng)地址并且和Director的私網(wǎng)地址在同一網(wǎng)段)
GW:192.168.10.8(網(wǎng)關(guān)必須指向Director的私網(wǎng)地址)
RS2可以ping通Director的私網(wǎng)地址,安裝nginx或httpd服務(wù),并配置主頁(yè)內(nèi)容

[root@rs2 ~]# ping 192.168.10.8
PING 192.168.10.8 (192.168.10.8) 56(84) bytes of data.
64 bytes from 192.168.10.8: icmp_seq=1 ttl=64 time=0.392 ms
64 bytes from 192.168.10.8: icmp_seq=2 ttl=64 time=0.558 ms
64 bytes from 192.168.10.8: icmp_seq=3 ttl=64 time=0.367 ms
64 bytes from 192.168.10.8: icmp_seq=4 ttl=64 time=0.367 ms
64 bytes from 192.168.10.8: icmp_seq=5 ttl=64 time=0.462 ms
^C
--- 192.168.10.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 0.367/0.429/0.558/0.074 ms

驗(yàn)證服務(wù)是否配置成功

[root@director ~]# curl http://192.168.10.9/test1.html    #連接RS1的nginx服務(wù)
<h1>RS1:192.168.10.9</h1>
[root@director ~]# curl http://192.168.10.10/test1.html    #連接RS2的nginx服務(wù)
<h1>RS2:192.168.10.10</h1>

安裝ipvsadm:

[root@director ~]# yum install ipvsadm -y
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: centos.ustc.edu.cn
 * updates: centos.ustc.edu.cn
正在解決依賴(lài)關(guān)系
--> 正在檢查事務(wù)
......
Running transaction
  正在安裝    : ipvsadm-1.27-7.el7.x86_64                                                                 1/1 
  驗(yàn)證中      : ipvsadm-1.27-7.el7.x86_64                                                                 1/1 

已安裝:
  ipvsadm.x86_64 0:1.27-7.el7

完畢!

配置lvs-nat
配置Director

[root@director ~]# ipvsadm -A -t 192.168.80.4:80 -s rr

# -s 指明調(diào)度方式為rr,Director服務(wù)要ip:80

添加RS服務(wù)器

[root@director ~]# ipvsadm -a -t 192.168.80.4:80 -r 192.168.10.9 -m
[root@director ~]# ipvsadm -a -t 192.168.80.4:80 -r 192.168.10.10 -m

# -r:添加RS服務(wù)器;-m:指明是nat模式

檢查配置結(jié)果:

[root@director ~]# 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.80.4:80 rr
  -> 192.168.10.9:80              Masq    1      0          0         
  -> 192.168.10.10:80             Masq    1      0          0 

驗(yàn)證結(jié)果:

[root@node1 ~]# for i in {1..10}; do curl http://192.168.80.4/test1.html; done
<h1>RS1:192.168.10.9</h1>
<h1>RS2:192.168.10.10</h1>
<h1>RS1:192.168.10.9</h1>
<h1>RS2:192.168.10.10</h1>
<h1>RS1:192.168.10.9</h1>
<h1>RS2:192.168.10.10</h1>
<h1>RS1:192.168.10.9</h1>
<h1>RS2:192.168.10.10</h1>
<h1>RS1:192.168.10.9</h1>
<h1>RS2:192.168.10.10</h1>

lvs-dr配置:

前期準(zhǔn)備:
各節(jié)點(diǎn)時(shí)間必須同步

Director:

VIP:192.168.10.8
DIP:192.168.80.4

在ens33:0網(wǎng)卡上配置如下:

[root@director ~]# ifconfig ens33:0 192.168.10.8 netmask 255.255.255.255 broadcast 192.168.10.8 up
[root@director ~]# route add -host 192.168.10.8 dev ens33:0

RS1:

VIP:192.168.10.8
RIP1:192.168.80.20
在lo:0上配置VIP

[root@rs1 ~]# ifconfig lo:0 192.168.10.8 netmask 255.255.255.255 broadcast 192.168.10.8 up

RS2:

VIP:192.168.10.8
RIP2:192.168.80.22
在lo:0上配置VIP

[root@rs2 ~]# ifconfig lo:0 192.168.10.8 netmask 255.255.255.255 broadcast 192.168.10.8 up

兩臺(tái)RS配置腳本寫(xiě)入內(nèi)核,對(duì)arp_ignore&&arp_announce做調(diào)整

[root@rs1 ~]# vim announce.sh 
#!/bin/bash
#
case $1 in
start)
    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce    
    ;;
stop)
    ifconfig lo:0 down
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce    
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac

--------------------------------------分割線------------------------------

[root@rs1 ~]# route add -host 192.168.10.8 dev lo:0

# route add 命令指定192.168.10.8的IP只能經(jīng)過(guò)lo:0這個(gè)網(wǎng)卡發(fā)送出去,確保是VIP響應(yīng)而不是RIP響應(yīng)

在Director上安裝并配置ipvsadm:

[root@director ~]# yum install ipvsadm -y
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.sjtu.edu.cn
 * extras: ftp.sjtu.edu.cn
 * updates: mirrors.shu.edu.cn
......
Running transaction
  正在安裝    : ipvsadm-1.27-7.el7.x86_64                                                                 1/1 
  驗(yàn)證中      : ipvsadm-1.27-7.el7.x86_64                                                                 1/1 

已安裝:
  ipvsadm.x86_64 0:1.27-7.el7                                                                                 

完畢!
[root@director ~]# ipvsadm -A -t 192.168.10.8:80 -s rr
[root@director ~]# ipvsadm -a -t 192.168.10.8:80 -r 192.168.80.20 -g
[root@director ~]# ipvsadm -a -t 192.168.10.8:80 -r 192.168.80.22 -g
[root@director ~]# 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.10.8:80 rr
  -> 192.168.80.20:80             Route   1      0          0         
  -> 192.168.80.22:80             Route   1      0          0 

# -g:表示dr類(lèi)型,默認(rèn)是dr

驗(yàn)證結(jié)果:

[root@node1 ~]# for i in {1..10}; do curl http://192.168.10.8/test1.html; done
<h1>RS1:192.168.80.20</h1>
<h1>RS2:192.168.80.22</h1>
<h1>RS1:192.168.80.20</h1>
<h1>RS2:192.168.80.22</h1>
<h1>RS1:192.168.80.20</h1>
<h1>RS2:192.168.80.22</h1>
<h1>RS1:192.168.80.20</h1>
<h1>RS2:192.168.80.22</h1>
<h1>RS1:192.168.80.20</h1>
<h1>RS2:192.168.80.22</h1>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容