天天看點

分離mysql和存儲實作雙web負載均衡

此篇博文僅限涉及web伺服器部分,至于負載均衡的實作可以按照需求來做,可以是基于DNS解析的負載均衡,也可以用lvs或者專用的負載均衡器(如F5或randware)

這是一個比較簡單的負載均衡的實作,沒有考慮到ha。

<a href="http://lustlost.blog.51cto.com/attachment/201207/11/2600869_1341965076UPEB.jpg"></a>

具體規劃:

将2台web伺服器做成一個discuz論壇的小叢集。合理配置設定2台web伺服器的任務,分離檔案共享和mysql應用,使2台伺服器同時提供web服務,但由web1充當網絡存儲,實作資料的同步,web2充當資料庫伺服器,提供資料的調用。合理的配置設定2台伺服器的負載,實作真正意義的負載均衡。

此情景隻是一個負載均衡的小叢集,未考慮到高可用的情況,在中小型企業中,如果一台伺服器撐不住,但業務也不是特别重要,允許在伺服器挂掉的情況下能斷線一段時間的情況下使用。

具體是在web1上做NFS檔案共享,将網頁放置在web1上。将資料庫放置在web2上。然後2台web伺服器同時提供web服務,外界通路2台伺服器時得到的網頁結果是一樣的

實作過程

首先在web1中安裝nfs,apache,php

[root@localhost ~]# yum install nfs-utils httpd php php-mysql –y

建立網站目錄

[root@localhost ~]# mkdir /dz

為了友善,将網站目錄的權限修改為777(在真正為企業配置時千萬别這麼來,這裡隻是為了實驗友善)

[root@localhost /]# chmod 777 dz -R

修改網址apache的配置檔案,指向dz目錄,這裡隻修改一行

DocumentRoot "/dz"

關閉selinux和iptables,并打開httpd

<a href="http://lustlost.blog.51cto.com/attachment/201207/11/2600869_1341965081NVnD.jpg"></a>

在dz中建立php測試頁面,確定php運作正常

[root@localhost dz]# vi index.php #寫入以下内容

&lt;?php  

phpinfo();  

?&gt; 

打開頁面測試一下

<a href="http://lustlost.blog.51cto.com/attachment/201207/11/2600869_1341965084MXKN.jpg"></a>

配置nfs服務,共享網站目錄dz

[root@localhost dz]# vi /etc/exports #加入下面一行

/dz     192.168.92.11(rw,async) 

啟動nfs服務

<a href="http://lustlost.blog.51cto.com/attachment/201207/11/2600869_1341965114Qk3H.jpg"></a>

web1暫時配置到這裡,先把配置web2配置一下

在web2上安裝apache,php,mysql-server

[root@localhost ~]# yum install nfs-utils httpd php php-mysql mysql-server -y

然後先挂載web1的nfs目錄

[root@localhost ~]# mount -t nfs 192.168.92.10:/dz /dz

和web1一樣關閉selinux和iptables 并打開httpd,

然後修改apache的配置和web1一樣網站路徑為/dz,不過這裡的/dz就是從web1網絡挂載過來的/dz了,這裡不再詳細列出過程了,和web1的一樣

然後開始為mysql配置論壇的資料庫table和賬戶

[root@localhost ~]# mysql

mysql&amp;gt; create database dz;  

Query OK, 1 row affected (0.01 sec) 

mysql&amp;gt; grant all privileges on dz.* to [email protected] identified by '123456';  

Query OK, 0 rows affected (0.01 sec) 

mysql&amp;gt; grant all privileges on dz.* to [email protected] identified by '123456';  

Query OK, 0 rows affected (0.00 sec) 

mysql&amp;gt; flush privileges;  

資料庫配置好後開始配置discuz

首先到WEB1上進行配置。

[root@localhost dz]# unzip Discuz_X2.5_SC_UTF8.zip #解壓discuz程式

進入/dz/config目錄 打開編輯config_global_default.php檔案。

[root@localhost config]# vi config_global_default.php 修改以下幾行内容

$_config['db'][1]['dbhost']             = '192.168.92.11'; #資料庫一定要指向web2  

$_config['db'][1]['dbuser']             = 'dzroot';   

$_config['db'][1]['dbpw']               = '123456';  

$_config['db'][1]['dbcharset']          = 'utf8';  

$_config['db'][1]['pconnect']           = 0;  

$_config['db'][1]['dbname']             = 'dz';  

$_config['db'][1]['tablepre']           = 'pre_'; 

然後将其複制為以下名稱(為了防止安裝discuz時報錯)

[root@localhost config]# cp config_global_default.php config_global.php 

[root@localhost config]# cp config_ucenter_default.php config_ucenter.php

浏覽器中打開192.168.92.10,開始discuz的安裝,進行到這一步時,對相應提示的目錄進行權限的修改

<a href="http://lustlost.blog.51cto.com/attachment/201207/11/2600869_1341965116VUyD.jpg"></a>

安裝成功後,用浏覽器打開192.168.92.10,進入管理者背景,添加一個闆塊

<a href="http://lustlost.blog.51cto.com/attachment/201207/11/2600869_1341965121iaID.jpg"></a>

然後打開192.168.92.11

<a href="http://lustlost.blog.51cto.com/attachment/201207/11/2600869_1341965125B6yx.jpg"></a>

可以看到2個web之間的内容是同步的

這就簡單的搭建了一個小小的負載均衡的叢集應用

實作比較簡單,重要的思路的開放。能夠讓不同的應用的壓力均攤到每個伺服器上,不浪費伺服器性能,這才是負載均衡的意義。

本文轉自lustlost 51CTO部落格,原文連結:http://blog.51cto.com/lustlost/926764,如需轉載請自行聯系原作者