lvs 負載均衡兩臺提供Discuz!論壇服務的服務器

規劃:

   1、使用lvs的nat模型,負載均衡兩臺 Discuz 服務器。

   2、Discuz 服務器的數據共享解決方案:

       部署 Discuz! 論壇程序要解決兩大問題:

       (1)、用戶上傳的附件屬於非結構化的數據要保存在文件系統中。

          要解決兩臺提供Discuz!論壇服務的附件共享問題:

 (A)、當用戶上傳附件時通過地址重寫機制把它定向到一臺Discuz!服務器,再通過

                rsync+innotify實時數據同步方式把附件同步到另一臺Discuz!服務器。

 (B)、使用rsync推、拉數據的方式,實現用戶上傳的附件同步,這樣無論用戶把附件上傳

               到那臺服務器都可以附件同享。

       (2)、論壇結構化的數據保存在共享數據庫服務器。

   3、Discuz!論壇程序是.php格式的。意思是:這裏選擇使用:lamp 平臺運行論壇程序。php 以模塊的方式與 httpd 服務器交互。

整個 Discuz! 論壇系統的結構如下圖所示:

wKiom1QO5bKjT3m-AAH2n1hQEeI365.jpg

一、構建LAMP平臺,用來運行:Discuz! 論壇程序

 在 real1.9527du.com(192.168.60.99) 和 real2.9527du.com(192.168.60.40 兩臺主機部署 LAMP 平臺中的:httpd 和 php.php 以模塊的方式與 httpd 交互

 MySQL 數據庫安裝在 DataServer.9527du.com(192.168.60.88)主機,作爲兩臺 Discuz! 服務器的共享數據庫服務器

LAMP 平臺使用的軟件:

httpd-2.4.1.tar.bz2
php-5.4.26.tar.bz2
mariadb-5.5.36-linux-i686.tar.gz

在  real1.9527du.com(192.168.60.99) 編譯安裝 httpd和php

1、編譯安裝 httpd 

解壓

[root@real1 lamp]# tar -xf httpd-2.4.1.tar.bz2
[root@real1 lamp]# cd httpd-2.4.1

編譯配置

[root@real1 httpd-2.4.1]# ./configure \ 
--prefix=/usr/local/httpd-2.4.1 \       指定程序的安裝位置
--sysconfdir=/etc/httpd-2.4.1 \         指定配置文件的存放位置
--enable-so \                           使用動態裝載模塊的功能,
--enable-mods-shared=all \               編譯所有的模塊
--enable-ssl  \                         支持 https 加密數據傳輸
--enable-mpms-shared=all  \             安裝所有的mpm
--with-mpm=prefork  \                   設置默認使用的MPM
--enable-info \                         開啓httpd的狀態信息功能
--enable-cgi  \                         支持cgi
--enable-rewrite \                      支持地址重定向功能
--with-apr=/usr/local/apr-1.4/ \        指定依賴的apr位置,
--with-apr-util=/usr/local/apr-util-1.4/ \
--with-pcre \
--with-z                                支持壓縮傳輸,節省帶寬但消耗CPU時間週期

編譯

[root@real1 httpd-2.4.1]#make && make install


2、安裝 php

解壓

[root@real1 admin]# tar -xf php-5.4.26.tar.bz2
[root@real1 admin]# cd php-5.4.26

編譯配置

[root@real1 php-5.4.26]# ./configure \
--prefix=/usr/local/php-6.4 \                             指定程序的安裝位置
--with-apxs2filter=/usr/local/httpd-2.4.1/bin/apxs \      把php編譯成httpd的模塊
--enable-mod-charset  \                                   支持更多的字符集
--with-config-file-path=/etc \                            配置文件的路徑
--with-config-file-scan-dir=/etc/php.d \            php一般會到php.d 找它模塊的配置文件
--with-libxml-dir \                                       支持xml
--with-openssl \                                          支持openssl 加密傳輸
--with-zlib \                                             支持zlib壓縮
--with-bz2 \                                              支持bz2壓縮
--with-pcre-dir \                                         
--with-gd \                                               圖形庫顯示圖形必需的
--with-jpeg-dir \                                         支持jpeg格式的圖片
--with-png-dir \                                           支持png格式的圖片
--with-zlib-dir \
--with-freetype-dir \                                      
--enable-mbstring  \                                      支持中文
--enable-sockets \                                        支持UNIX SOCK
--enable-maintainer-zts  \                                 安全的線程
--with-mysql=mysqlnd \    使用php內置連接數據庫的驅動。這樣就不需要事先安裝好數據庫了。
--with-mysqli=mysqlnd \                                   使用php內置連接數據庫的驅動 
--with-pdo-mysql=mysqlnd                                  使用php內置連接數據庫的驅動

編譯安裝

[root@real1 php-5.4.26]# make && make install

提供php的配置文件

[root@real1 php-5.4.26]# cp php.ini-production /etc/php.ini

(1)、設置httpd的配置文件讓httpd支持動態格式的網頁.php

設置支持.php格式的主頁

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

添加.php格式的網頁的處理器

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

查看是否加載php模塊

[root@real1 ~]# grep "LoadModule[[:space:]]*php" /etc/httpd-2.4.1/httpd.conf
LoadModule php5_module        modules/libphp5.so

說明:

    在 rele2.9527du.com(192.168.60.99)主機也是一樣的編譯安裝httpd、php程序和設置。



3、在 DataServer.9527du.com(192.168.60.88)安裝 MariaDB 數據庫服務器。


(1)、準備好安裝 MariaDB 數據庫的條件

創建MariaDB數據庫的數據目錄

[root@DataServer /]# mkdir -p  mysqldata/mysql

創建初始化數據庫的用戶 mysql,且把該用戶創建成系統用戶默認登陸shell爲:/sbin/nologing

[root@DataServer /]# user -r  -s /sbin/nologin mysql
[root@DataServer /]# id mysql
uid=388(mysql) gid=388(mysql) groups=388(mysql)

因爲發起對數據目標中的數據操作(增、刪、改、查)的進程的屬主是:mysql,所以要修改數據目錄的屬主屬組爲:mysql.

root@DataServer mysqldata]# chown mysql:mysql mysql
[root@DataServer mysqldata]# ll
total 4
drwxr-xr-x 2 mysql mysql 4096 Sep  8 10:41 mysql

(2)、安裝 MariaDB數據庫

解壓數據庫程序到 /use/local 目錄下

[root@DataServer admin]# tar -xf mariadb-5.5.36-linux-i686.tar.gz -C /usr/local/
[root@DataServer admin]# cd /usr/local/

創建軟鏈接,不直接修改目錄名稱,爲了以後升級方面

[root@DataServer local]# ln -sv mariadb-5.5.36-linux-i686 mysql
create symbolic link `mysql' to `mariadb-5.5.36-linux-i686'
[root@DataServer local]# ll mysql
lrwxrwxrwx 1 root root 25 Sep  8 09:30 mysql -> mariadb-5.5.36-linux-i686

修改解壓後的所有程序的屬主屬組爲:mysql ,因爲做數據庫的初始化時,需要以mysql的身份執行一些初始化腳本。

root@DataServer local]# cd mysql/
[root@DataServer mysql]# chown -R mysql:mysql ./*
[root@DataServer mysql]# ll
total 216
drwxr-xr-x  2 mysql mysql   4096 Sep  8 09:34 bin
-rw-r--r--  1 mysql mysql  17987 Feb 24  2014 COPYING
-rw-r--r--  1 mysql mysql  26545 Feb 24  2014 COPYING.LESSER
drwxr-xr-x  3 mysql mysql   4096 Sep  8 09:34 data
drwxr-xr-x  2 mysql mysql   4096 Sep  8 09:34 docs
....

初始化數據庫

[root@DataServer mysql]# ./scripts/mysql_install_db --datadir=/mysqldata/mysql/  --user=mysql
Installing MariaDB/MySQL system tables in '/mysqldata/mysql/' ...
OK
Filling help tables...
OK

說明:

   初始化數據庫已經成功。

(3)、提供啓動 MariaDB 數據庫的條件

爲數據庫提供配置文件

[root@DataServer mysql]# cp support-files/my-large.cnf /etc/my.cnf

根據實際情況修改數據庫的數據目錄的位置等參數

[root@DataServer mysql]# vim /etc/my.cnf
thread_concurrency = 2
datadir = /mysqldata/mysql/

爲 MariaDB 數據庫提供啓動腳本

[root@DataServer mysql]# cp support-files/mysql.server /etc/init.d/mysqld
root@DataServer mysql]# ll /etc/init.d/mysqld
-rwxr-xr-x 1 root root 11844 Sep  8 10:48 /etc/init.d/mysqld

爲了安全考慮,修改數據庫程序的屬主爲:root 屬組爲:mysql

[root@DataServer mysql]# chown -R root:mysql ./*
[root@DataServer mysql]# ll
total 216
drwxr-xr-x  2 root mysql   4096 Sep  8 09:34 bin
-rw-r--r--  1 root mysql  17987 Feb 24  2014 COPYING
-rw-r--r--  1 root mysql  26545 Feb 24  2014 COPYING.LESSER
drwxr-xr-x  3 root mysql   4096 Sep  8 09:34 data
drwxr-xr-x  2 root mysql   4096 Sep  8 09:34 docs
.....

爲了使用 MariaDB 數據庫提供的工具命令方便,把它所在的路徑導出來

[root@DataServer /]# vim /etc/profile.d/mysql.sh
PATH=$PATH:/usr/local/mysql/bin/

(3)、啓動 MariaDB 數據庫並測試

啓動數據庫服務

[root@DataServer /]# service  mysqld start
Starting MySQL..                                           [  OK  ]

使用客戶端工具mysql連接數據庫

root@DataServer /]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.36-MariaDB-log MariaDB Server

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

說明:

    數據庫已經順利啓動。


(4)、修改數據庫資源的訪問,原因安裝數據數據庫時默認安裝的用戶是沒有密碼的。極其不安全。

刪除多餘的默認用戶

MariaDB [(none)]> drop user 'root'@'dataserver.9527du.com';
MariaDB [(none)]> drop user 'root'@'::1';
MariaDB [(none)]> drop user ''@'localhost';
MariaDB [(none)]> drop user ''@'dataserver.9527du.com';
MariaDB [(none)]> drop user 'root'@'localhost';

給保留的用戶添加密碼,

MariaDB [(none)]> set password for 'root'@'127.0.0.1' = password('root');
Query OK, 0 rows affected (0.00 sec)

設置遠程管理數據庫的用戶

MariaDB [(none)]> grant all on *.* to 'admin'@'%.%.%.%' identified by 'admin';
Query OK, 0 rows affected (0.00 sec)

修改後的 MariaDB 數據庫的用戶列表

MariaDB [(none)]> select user,host,password from mysql.user;
+-------+-----------+-------------------------------------------+
| user  | host      | password                                  |
+-------+-----------+-------------------------------------------+
| admin | %.%.%.%   | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
| root  | 127.0.0.1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-------+-----------+-------------------------------------------+
2 rows in set (0.00 sec)

遠程測試是否能夠登陸操作數據庫

D:\>mysql -uadmin -h192.168.60.88 -p
Enter password: *****
Welcome to the MySQL monitor.  Commands end with ; or \g.
。。。。。
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

說明:

    MariaDB數據庫已經安裝完成


(5)、創建Discuz論壇使用的數據庫

MariaDB [(none)]> create database discuz;
Query OK, 1 row affected (0.00 sec)

創建Discuz論壇程序訪問數據庫的用戶,採用最小權限法則,該用戶只有:Discuz 數據庫的所有權限。

MariaDB [(none)]> grant all on discuz.* to 'discuz'@'192.168.60.%' identified by 'discuz';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

說明:

    LAMP 平臺的數據庫已經安裝測試成功。


4、測試,LAMP 平臺

說明:都是在real1.9527du.com(192.168.60.99)主機,通過【curl】訪問測試的。

(1)、添加測試頁

[root@real1 htdocs]# cat index.php
<?php
    $link = mysql_connect('192.168.60.88','admin','admin');
    if ($link)
       echo "Success...";  ----> 如果連接數據庫成功會輸出:Success...
    else
       echo "Failure...";  ----> 如果連接數據庫失敗會輸出:Failure...
?>

(2)、測試real1.9527du.com(192.168.60.99)主機搭建的 lamp 平臺

[root@real1 httpd-2.4.1]# curl http://192.168.60.99/index.php
Success...

說明:

    連接數據庫成功

通過ssh遠程工具,關閉數據庫服務

[root@real1 httpd-2.4.1]# ssh 192.168.60.88 'service mysqld stop'
[email protected]'s password:
Shutting down MySQL.[  OK  ]

再次測試是否能夠連接數據庫

[root@real1 httpd-2.4.1]# curl http://192.168.60.99/index.php
Failure...

說明:

    連接數據庫失敗。

192.168.60.99 主機的:lamp 平臺已經搭建成功!!!

(3)、測試real2.9527du.com(192.168.60.40)主機搭建的:lamp 平臺

把測試頁面發送到 192.168.60.40

[root@real1 httpd-2.4.1]# scp ./htdocs/index.php 192.168.60.40:/usr/local/httpd-2.4.1/htdocs/
[email protected]'s password:
index.php                                     100%  146     0.1KB/s   00:00

測試 是否能夠連接到數據庫 

[root@real1 httpd-2.4.1]# curl http://192.168.60.40/index.php                   
Failure...

說明:

    連接數據庫失敗。

通過ssh遠程工具,開啓數據庫服務

[root@real1 httpd-2.4.1]# ssh 192.168.60.88 'service mysqld start'              
[email protected]'s password:
Starting MySQL....[  OK  ]

再次訪問測試

[root@real1 httpd-2.4.1]# curl http://192.168.60.40/index.php                   
Success...

說明:

    已經連接到數據庫服務器了。192.168.60.40 主機的:lamp 平臺已經搭建成功!!!



說明:

   提供 Discuz 論壇服務的兩個LAMP平臺都搭建成功。


二、使用rsync數據同步服務解決,兩臺 Discuz! 服務器數據共享問題;

使用rsync 數據同步服務爲:real1.9527du.com(192.168.60.99)和

real2.9527du.com(192.168.60.40) 兩臺提供 Discuz 論壇服務的服務器的提供數據共享解決方案。

real1.9527du.com(192.168.60.99)作爲rsync的服務器端

real2.9527du.com(192.168.60.40)作爲rsync客戶端

1、安裝配置rsync服務器

(1)使用lvm邏輯卷保存論壇程序

查看VG卷組是否還有剩餘空間

[root@real1 /]# vgdisplay vg0 | grep "[[:space:]]*\/[[:space:]]*Size"
  Alloc PE / Size       6656 / 52.00 GiB
  Free  PE / Size       1023 / 7.99 GiB

 創建lvm

[root@real1 /]# lvcreate -L 4G -n lvrsync vg0
  Logical volume "lvrsync" created

格式化lvm

[root@real1 /]# mke2fs -t ext4 /dev/vg0/lvrsync
[root@real1 /]# echo $?
0

(2)、掛載

創建掛載點

[root@real1 /]# mkdir /web

設置開機自動掛載

[root@real1 /]# vim /etc/fstab
/dev/mapper/vg0-lvrsync /web                    ext4    defaults,acl        0 0
[root@real1 /]# mount -a
[root@real1 /]# mount | grep "lvrsync"
/dev/mapper/vg0-lvrsync on /web type ext4 (rw)

(3)安裝 rsync程序併爲其提供配置文件

[root@real1 /]# yum install rsync

讓 rsync 以獨立守護進程方式工作。

rsync工作成服務模式,要有配置文件,爲rsync服務器提供配置文件

[root@real1 /]# vim /etc/rsyncd.conf
#
#
uid = nobody                      
gid = nobody
use chroot = no                  
max connections = 10              ------> 併發量
strict modes = yes
pid file = /var/run/rsyncd.pid     -----> rsync的PID文件位置
log file = /var/log/rsyncd.lock    -----> 鎖文件的位置

[web]                              -----> rsync服務器導出的存儲空間的標識
path = /web                        ------> 尋出的存儲空間在文件系統的位置
ignore errors                      ------> 數據同步時是否忽略一些IO錯誤,繼續實現數據同步
read only = no                     -------> 允許客戶端上傳數據
write only = no                    -------> 允許客戶端拉取數據

hosts allow = 192.168.60.0/24      --------> 允許訪問rsync服務器的客戶端端
hosts deny = *

list = false                       --------> 不允許列出rsync服務器導出的存儲空間
uid = root
gid = root

2、配置 rsync 客戶端實時向rsync服務器推送數據

使用 innotify 機制實現數據的實時同步

(1)、使用lvm邏輯卷做爲rsync導出的存儲空間。

查看卷組是否還有空間創建lvm

[root@real2 /]# vgdisplay | grep "[[:space:]]*\/[[:space:]]*Size"
  Alloc PE / Size       6656 / 52.00 GiB
  Free  PE / Size       1023 / 7.99 GiB

  創建lvm

[root@real2 /]# lvcreate -L 4G -n web vg0
  Logical volume "web" created

  格式化lvm

[root@real2 /]# mke2fs  -t ext4 /dev/vg0/web
[root@real2 /]# echo $?
0

(2)、掛載

創建掛載點

[root@real2 /]# mkdir  /web

設置自動掛載

[root@real2 /]# vim /etc/fstab
/dev/mapper/vg0-web     /web                    ext4    defaults,acl        0 0
[root@real2 /]# mount -a
[root@real2 /]# mount | grep "web"
/dev/mapper/vg0-web on /web type ext4 (rw)

(3)、安裝: innotify

inotify-tools-3.14.tar.gz 

[root@real2 admin]# tar -xf  inotify-tools-3.14.tar.gz
[root@real2 admin]#cd inotify-tools-3.14
[root@real2 inotify-tools-3.14]#./configure --prefix=/usr/local/inotify-tools
[root@real2 inotify-tools-3.14]#Make && make install

(4)、配置 當 /web 目錄的文件發生改變(create、move、delete、modify)就觸發 rsync 客戶端向 rsync 服務端推送數據。

腳本如下:

#/bin/bash
#
#

rsyncServer=192.168.60.99

src=/web/
dst=web

rsync -azrtopg --delete $src $rsyncServer::$dst

/usr/local/inotify-tools/bin/inotifywait -mrq  -e create,move,delete,modify  $src | while read files;do
   rsync -azrtopg --delete $src $rsyncServer::$dst
done

(5)、在 rsync 服務端啓動rsync 服務

[root@real1 /]# rsync --daemon --config=/etc/rsyncd.conf --ipv4

查看是否監聽

[root@real1 /]# netstat -antpl | grep rsync
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      2382/rsync

在服務端以後臺方式運行innotify監測腳本

[root@real2 ~]# ./inotify.sh &
[1] 2348

(6)、測試:rsync + inotify 實現數據實時同步:

在 rsync 客戶端創建文件

[root@real2 ~]# touch  /web/test.txt

查看 rsync 服務器端導出的存儲空間是否有該文件

[root@real1 web]# ll
total 16
drwx------. 2 root root 16384 Sep  8 11:32 lost+found
-rw-r--r--. 1 root root     0 Sep  8 11:47 test.txt

說明:

    在rsync 客戶端,innotify 已經監測到 /web 文件系統的 create 事件


在 rsync 客戶端修改 test.txt 文件

[root@real2 ~]# echo "test rsync server" > /web/test.txt

看看 rsync 服務器端的 test.txt 文件的內容是否改變

[root@real1 web]# cat test.txt
test rsync server

說明:

   在 rsync 客戶端,innotify 已經監測到 /web 文件系統的"modify"文件的事件


在 rsync 客戶端刪除 test.txt 文件

[root@real2 ~]# rm -f /web/test.txt

看看 rsync 服務端的 test.txt 文件是否也被刪除

[root@real1 web]# ll
total 16
drwx------. 2 root root 16384 Sep  8 11:32 lost+found

說明:

    從上面測試得知,使用 rsync + innotify 實現兩臺服務器的論壇程序共享已經成功。


3、由於沒有做策略,當用戶上傳附件時候,把它定向到某臺特定的discuz!服務器。

所以,做週期性任務計劃,定時從rsync 服務器端拉取數據到本地,實現無論用戶上傳附件到哪臺服務器。都可以實現附件共享。

[root@real2 ~]# crontab -e
crontab: installing new crontab
*/1 * * * * /usr/bin/rsync -aptgoz 192.168.60.99::web /web

使用 rsync 客戶端,向rsync發出數據同步請求的方式:拉取數據到本地和推送數據到rsync導出的存儲空間。

從而實現,無論用戶訪問 Discuz 時被 lvs 分發到哪臺主機都可有下載用戶上傳的附件。 


三、部署 Discuz! 論壇程序.

1、在 real2.9527du.com(192.168.60.40) 部署 Discuz! 論壇程序.

也就是在 rsync客戶端部署 Discuz! 論壇程序,會自動通過 rsync的數據同步服務,同步到另一臺Discuz 服務器上。

(1)、創建部署論壇程序的目錄

[root@real2 /]# cd web/
[root@real2 /web]# mkdir html

(2)、修改 httpd 的配置文件,【DocumentRoot】定義的文檔根目錄爲:/web/html

修改real2 主機的httpd配置文件

[root@real2 upload]# vim /etc/httpd-2.4.1/httpd.conf
DocumentRoot "/web/html"
<Directory "/web/html">
   Options -Indexes -FollowSymLinks
   AllowOverride None
    Require all granted
</Directory>

同樣也修改 real1.9527du.com 的配置文件

[root@real1 web]# vim /etc/httpd-2.4.1/httpd.conf
DocumentRoot "/web/html"
<Directory "/web/html">
   Options -Indexes -FollowSymLinks
   AllowOverride None
    Require all granted
</Directory>

(3)、啓動 httpd 服務

[root@real2 upload]# cd /usr/local/httpd-2.4.1/
[root@real2 httpd-2.4.1]# ./bin/apachectl restart

(4)、安裝:

[root@real2 html]# unzip Discuz_X2.5_SC_GBK.zip

wKioL1QO7mXiLygWAARA-B0DIoM845.jpg

點擊“我同意”

wKioL1QO7rHiZGoBAALq0LJfTQg768.jpg


按要求修改上述目錄的權限

wKioL1QO7z3A1VRTAAJnLGtj4iA221.jpg


填寫連接數據庫的信息

wKiom1QO75jCtcOkAAHmPIOVxGM224.jpg

在該臺服務器部署 Discuz!成功!


2、啓動 rsync 服務, 把Discuz 論壇程序同步到另一臺服務器(real1.9527du.com)。

(1)、啓動rsync服務

[root@real1 web]# rsync --daemon --config=/etc/rsyncd.conf --ipv4
[root@real1 web]# netstat -anptl | grep rsync
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      2330/rsync

(2)、在rsyn客戶端啓動innotify監測腳本

[root@real2 ~]# ./inotify.sh &
[1] 2561

(3)、查看數據是否同步過來

[root@real1 web]# ll html/
total 9312
-rw-r--r--.  1 root root 9522601 Aug 16 13:54 Discuz_X2.5_SC_GBK.zip
drwxr-xr-x.  2 root root    4096 Oct 31  2012 readme
drwxr-xr-x. 12 root root    4096 Oct 31  2012 upload
drwxr-xr-x.  4 root root    4096 Oct 31  2012 utility

(4)、啓動 real1.9527du.com 主要的http服務

root@real1 web]# cd /usr/local/httpd-2.4.1/
[root@real1 httpd-2.4.1]# ./bin/apachectl start

3、訪問測試

wKioL1QO8NCCrVrIAAJ1M-mP018537.jpg

訪問另一臺服務器,查看測試結果

wKiom1QO8PGBKhJZAAH-GXJA6mk014.jpg

在 192.168.60.44發貼,測試在另一臺服務器是否看到

wKioL1QO8U-iNp5hAAJ_kfNcrXI233.jpg

查看192.168.60.99服務器是否看到在192.168.60.40服務器上傳的附件。

wKiom1QO8ZfjSdiyAAKGhp-k59k654.jpg

已經測試過,還能下載。


說明:

    兩臺服務器部署 Discuz! 論壇程序已經成功!!!


四、配置基於 lvs 的 nat 模型負載均衡兩臺 Discuz! 服務器。

lvs 的nat 模型工作原理:

   ipvs 捕獲到訪問的是集羣服務時,通過調度算法計算該訪問應該被調度到後端的那個 Real Server ,然後ipvs修改訪問集羣服務的數據報文的目標IP地址爲選中的 Real Server,再把數據報文發送到 Real Server.

   當 Real Server 構建完成響應了,數據報文的源IP是RIP,目標IP是CIP。互聯網的數據報文路由是根據目標IP路由的,如果,該 Real Server 能夠連接到外網,該數據報文是可以傳輸到CIP客戶端的。但是客戶端不會接收(因爲客戶端請求的是VIP)。所以,Real Server 響應客戶端時,還應該把數據報文的源地址修改爲VIP。

   所以,RealServer 還應該把數據報文送給 Directory,由它根據nat目標地址轉換會話表,把響應的數據報文的源地址修改爲:VIP,再送給客戶端。

爲了配合 ipvs 完成上述過程,要解決以下問題:

   (1)、Real Server 響應客戶端時,發現自己訪問的網絡與自己不在同一網絡,怎麼把數據報文送給 Director?

   通過默認網關。只要訪問的網絡是別的網絡,都把數據報文送給網關。DIR做爲 Real Server 的默認網關。

   所以,還要求DIP與RIP在同一個網絡中。

   (2)、Real Server 把響應報文通過默認網關送達 Director 。數據報文經過 FORWARD 鏈,送達 POST_ROUTING 會根據nat會話表完成源地址的轉換。

    此時,Director 相當於工作在網絡防火牆狀態,要使用到系統的轉發功能。所以要開啓Linux的 ip_forward 功能。纔可以把數據報文送達 POST_ROUTING鏈。

如下圖:

wKiom1QO8sWz5l-6AALGiksXlm4026.jpg

1、在 real1 配置默認網關(DIP:192.168.60.1)。

[root@real1 ~]# route add default gw 192.168.60.1
[root@real1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.60.1    0.0.0.0         UG    0      0        0 eth1
192.168.60.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1

2、在 real2 配置默認網關(DIP:192.168.60.1)

[root@real2 ~]# route add default gw 192.168.60.1
[root@real2 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.60.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
0.0.0.0         192.168.60.1    0.0.0.0         UG    0      0        0 eth0

3、配置 lvs 規則。

開啓數據報文轉發功能

[root@director ~]# echo 1 > /proc/sys/net/ipv4/ip_forward

定義集羣服務,使用lvs的持久連接功能。

[root@director ~]# ipvsadm -A -t 172.16.60.22:80 -s wlc  -p 30

定義集羣服務的 Real Server

[root@director ~]# ipvsadm -a -t 172.16.60.22:80 -r 192.168.60.40 -m -w 1
[root@director ~]# ipvsadm -a -t 172.16.60.22:80 -r 192.168.60.99 -m -w 1

查看lvs規則

[root@director ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.60.22:80 rr
  -> 192.168.60.99:80             Masq    1      0          0
  -> 192.168.60.40:80             Masq    1      0          0

4、訪問測試:

wKioL1QO8_GTlgB4AALhipst60A875.jpg不斷刷新之後。。。

wKioL1QO9E2QDHnOAAS_5W7-R5U454.jpg


到此爲止,LVS負載均衡兩臺: Discuz! 已經成功!!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章