LVS-NAT類型搭建WordPress

一、實驗環(huán)境:

1.各軟件版本:

系統(tǒng)版本:CentOS Linux release 7.4.1708 (Core)

php版本:PHP 7.2

nginx版本:nginx-1.12.2

數(shù)據(jù)庫版本:MariaDB 10

WordPress版本:4.9.4

關(guān)閉防火墻與selinux

2.實驗架構(gòu)及IP分配:

調(diào)度器一臺:

安裝ipvsadm

VIP:192.168.11.128(模擬公網(wǎng)IP,域名www.ready.cn) DIP:172.16.142.128

realserver兩臺:

兩臺都安裝nginx+PHP,且保持配置一致

RIP1:172.16.142.11 RS2:172.16.142.22

數(shù)據(jù)庫服務(wù)器一臺:

安裝MariaDB

IP:172.16.142.33

存儲服務(wù)器一臺:

提供NFS存儲服務(wù)

二、所需軟件安裝配置,并測試PHP是否正常解析

1.RS1和RS2安裝nginx+PHP:

a>安裝epel源

rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm

b>yum安裝nginx

yum install -y nginx

c>yum安裝PHP

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

yum install -y yum install php72w-common php72w-fpm php72w-opcache php72w-gd php72w-mysqlnd php72w-mbstring php72w-pecl-redis php72w-pecl-memcached php72w-devel

d>測試兩臺RS是否可以正常接收并響應(yīng)web請求,添加nginx server,創(chuàng)建網(wǎng)站目錄,將用戶請求結(jié)尾為.php的URL交給php-fpm解析,并創(chuàng)建phpinfo做頁面測試

vim /etc/nginx/conf.d/php.conf

server {

listen      80;

server_name  www.ready.org;  #因為是做試驗,所以將hosts中添加本地域名解析

location / {

root  html/php;  #nginx配置中網(wǎng)站的默認(rèn)根路徑為/usr/share/nginx/,這里指定的是相對路徑

index  index.html index.htm;

}

location ~ .*\.(php|php5)?$ { #以任意開頭以.php結(jié)尾的URI用此location做匹配

root  html/php;

fastcgi_pass  127.0.0.1:9000; #將請求交由fastcgi(php-fpm)處理

fastcgi_index index.php;

include fastcgi.conf;

                }

}

mkdir -p /usr/share/nginx/html/php/ #創(chuàng)建網(wǎng)站目錄(不是必須步驟,也可使用默認(rèn)目錄)

vim /usr/share/nginx/html/php/index.php

e>將nginx啟動并設(shè)置開機啟動

systemctl start nginx

systemctl enable nginx

f>開啟php-fpm并設(shè)置為開機啟動

g>瀏覽器訪問www.ready.orgwww.ready.org/index.php,顯示以下界面便表示nginx與PHP已安裝成功。

然后刪除index.php文件,安全因素。

以上步驟不涉及負(fù)載均衡的使用,下面進(jìn)行加入負(fù)載均衡服務(wù)器的配置測試,先刪除之前配置好的nginx測試server,或?qū)⑵渥⑨尩?/p>

mv /etc/nginx/conf.d/php.conf /etc/nginx/conf.d/php.conf.backup

若是要將RS1、RS2并入負(fù)載均衡集群,則需將RS1、RS2設(shè)置為監(jiān)聽本機80端口且將結(jié)尾為.php的URI交php-fpm解析

即修改/etc/nginx/nginx.conf將匹配PHP解析的location加入默認(rèn)配置即可,修改如下:

vim /etc/nginx/nginx.conf

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include      mime.types;

    default_type  application/octet-stream;

            log_format  main  '$remote_addr - $remote_user [$time_local] "$request"

'

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;

    tcp_nopush          on;

    tcp_nodelay        on;

    keepalive_timeout  65;

    types_hash_max_size 2048;

    server {

        listen      80;

        #server_name _; #我理解的是此負(fù)載均衡集群中沒有涉及域名解析,所以server_name可以省略

        location / {

            root  /usr/share/nginx/html/php; #我覺得root路徑盡量要用絕對路徑,最好是寫在location中,這樣便于管理,方便查閱

            index  index.php index.html index.htm; #不要忘了加index.php,不加會導(dǎo)致只輸入域名時返回403錯誤

        }

        location ~ .*\.(php|php5)?$ {

            root  /usr/share/nginx/html/php;

        index index.php;

        fastcgi_pass  127.0.0.1:9000;

        fastcgi_index index.php;

        include fastcgi.conf;

                }

        }

}

2.安裝ipvsadm并配置

a>安裝ipvsadm

yum install -y ipvsadm

b>配置lvs服務(wù)器,測試調(diào)度是否成功

ipvsadm -A -t 192.168.11.128:80 -s rr #添加tcp協(xié)議VIP,端口為80,調(diào)度算法為輪詢

ipvsadm -a -t 192.168.11.128:80 -r 172.16.142.11 -m #添加RS1,類型為net

ipvsadm -a -t 192.168.11.128:80 -r 172.16.142.22 -m #添加RS2,類型為net

-s 設(shè)置調(diào)度算法

rr 輪詢

wrr 加權(quán)輪詢

sh 原地址哈希調(diào)度,保證整個系統(tǒng)的出入口唯一

wlc 加權(quán)最小連接數(shù)調(diào)度

c>開啟Linux的內(nèi)核轉(zhuǎn)發(fā)功能

此步驟很關(guān)鍵,因為關(guān)系到請求報文能否經(jīng)由VIP后再轉(zhuǎn)發(fā)至DIP。

sysctl -w net.ipv4.ip_forward=1

瀏覽器訪問192.168.11.128與192.168.11.128/index.php

若出現(xiàn)測試頁面即為配置成功

3.安裝并配置MariaDB

a>在172.16.142.33上yum安裝MariaDB(以下數(shù)據(jù)庫配置有很多安全隱患,待后續(xù)深入學(xué)習(xí)數(shù)據(jù)庫后會另起博文詳解數(shù)據(jù)庫的相關(guān)配置)

vim /etc/yum.repos.d/mariadb.repo

[mariadb]

name = MariaDB

baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.2/centos7-amd64

gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB

gpgcheck=1

yum install -y MariaDB-server

b>配置數(shù)據(jù)庫

mysql_secure_installation #初始化數(shù)據(jù)庫,設(shè)置root密碼,匿名用戶權(quán)限等

mysql -uroot -p123456 #登入數(shù)據(jù)庫進(jìn)行用戶及庫的創(chuàng)建與配置

>GRANT ALL PRIVILEGES ON *.* TO 'mysql'@'%'IDENTIFIED BY '123456' WITH GRANT OPTION; #創(chuàng)建賬戶mysql,密碼為123456,并允許任意IP訪問此賬戶數(shù)據(jù)庫,權(quán)限為ALL

>CREATE DATABASE wordpress; #創(chuàng)建名為wordpress的庫,安裝完WordPress后會要求在數(shù)據(jù)庫創(chuàng)建庫,這里事先創(chuàng)建好

>quit

4.安裝WordPress

a>將下載好的WordPress包解壓至已創(chuàng)建好的/usr/share/nginx/html/php目錄下

b>為模擬互聯(lián)網(wǎng)環(huán)境,將'www.ready.cn 192.168.11.128'hosts文件加入本地解析規(guī)則

c>瀏覽器輸入www.ready.cn,按提示步驟進(jìn)行操作便可安裝完成

三、關(guān)于lvs-nat類型下ipvsadm的一些用法

ipvsadm -ln --stats #狀態(tài)信息

例:

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes

-> RemoteAddress:Port

TCP 192.168.11.128:80 25 951 353 83843 2050036

-> 172.16.142.11:80 12 574 205 46157 1303372

-> 172.16.142.22:80 13 377 148 37686 746664

Conns 請求連接數(shù)

InPkts 入站的報文數(shù)

OutPkts 出站的報文數(shù)

InBytes 入站的字節(jié)數(shù)

OutBytes 出站的字節(jié)數(shù)

ipvsadm -ln --rate #每秒傳輸速率信息

ipvsadm -ln -c #當(dāng)前站點訪問情況

例:

IPVS connection entries

pro expire state source virtual destination

TCP 14:55 ESTABLISHED 192.168.11.1:53035 192.168.11.128:80 172.16.142.22:80

TCP 14:55 ESTABLISHED 192.168.11.1:53016 192.168.11.128:80 172.16.142.11:80

TCP 01:46 TIME_WAIT 192.168.11.1:53029 192.168.11.128:80 172.16.142.11:80

TCP 01:09 TIME_WAIT 192.168.11.1:52990 192.168.11.128:80 172.16.142.11:80

這里需要注意的是每次輸入網(wǎng)址訪問頁面時,并不一定只發(fā)送了一個請求,所以只訪問一次頁面時,也會出現(xiàn)多個RS在響應(yīng)請求。另外這里的state項可以幫助理解tcp/ip協(xié)議的“握手與揮手”

ipvsadm -S -n #保存規(guī)則,一般是保存在/etc/sysconfig/ipvsadm

例:ipvsadm -S -n > /etc/sysconfig/ipvsadm

也可使用systemctl stop ipvsadm.service做自動保存

ipvsadm -C #清空規(guī)則

四、缺陷

網(wǎng)站會有用戶上傳文件的需求,所以必須保證RS1和RS2上傳文件目錄保存同步或者共用一個存儲

可用inotify+rsync或者同時掛載NFS實現(xiàn),后續(xù)會更新演示

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容