一、LVS/DR簡單介紹
企業常用模型
DR(Direct Routing)直接路由模式。LB收到請求包後,将請求包中目标MAC位址轉換為某個標明RS的MAC位址後将包轉發出去,RS收到請求包後 ,可直接将應答内容傳給使用者。此時要求LB和所有RS都必須在一個實體段内,且LB與RS群共享一個虛拟IP。
lvs-dr模型特征 (常用)
1、叢集節點必須在一個實體網絡上(mac)
2、rip可以使用公網位址
3、director處理入站請求,
4、realserver網關不指向director
5、不支援端口映射
6、大多數作業系統支援realservers
7、dr dirctor 比nat director性能優越
二、實驗說明 實驗拓樸圖
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiInBnaukzM1MDN1IzMy8CX0AjMxAjMvwFduVWboNWY0RXYvwVbvNmLvR3YxUjL5M3Lc9CX6MHc0RHaiojIsJye.jpg)
172.16.23.10是LVS與網際網路通信的實體位址,簡稱為DIP
172.16.23.1是LVS與内部真實伺服器通信的位址,簡稱為VIP
172.16.23.12與172.16.23.12就是LVS通過一些排程算法,選擇伺服器響應的真實伺服器web位址,這兩個伺服器上還有一個lo上網卡别名lo:0 172.16.23.1 即跟VIP位址一樣 web是nginx+php
172.16.23.7 是mysql伺服器位址
本次實驗主要是通過LVS這個負載均衡通過一些排程算法,對web進行負載均衡,而且是可進行加密進行,即實作ssl,并且通過web1,web2分别安裝pam進行對mysql這台伺服器進行管理。都是橋接
三、實作過程
1、建構web1,web2這兩台伺服器,上一篇部落格已經寫過,這裡隻進行ssl說明。
Web1上先CA憑證
cd /etc/pki/CA
vim ../tls/openssl.cnf 修改一些參數找到 [CA_default] dir 設定為dir = /etc/pki/CA
(umask 077;openssl genrsa 1024>private/cakey.pem)
openssl req -new -x509 -key private/cakey.pem -out cacert.pem
mkdir newcerts certs crl
touch index.txt
echo 01>serial
echo 01>crlnumber
2、web1建httpd證書
cd /etc/httpd/conf
mkdir ssl
cd ssl
(umask 077;openssl genrsa 1024 > httpd.key)
openssl req -new -key httpd.key -out httpd.csr
3、向CA即自已申請證書
openssl ca -in httpd.csr -out httpd.crt
ca進行簽發
4、修改配置檔案即/etc/nginx/nginx.conf 肉容中下
server {
listen 443;
server_name www.server1.com;
ssl on;
ssl_certificate /etc/httpd/ssl/httpd.crt; /httpd證書
ssl_certificate_key /etc/httpd/ssl/httpd.key; /證書私鑰
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM
# ssl_prefer_server_ciphers on; 這兩項先不起用
location / {
root /www;
index index.php index.html index.htm; //添加index.php
}
location ~ \.php$ {
root /www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param HTTPS on ; /這裡要添加如下内容,啟用https
include fastcgi_params;
}
}
5、安裝mysql圖形管理工具
(1) 需要在網上下載下傳phpMyAdmin-2.11.10-all-languages
Tar xf phpMyAdmin-2.11.10-all-languages –C / //解壓到根目錄下
mv phpMyAdmin-2.11.10 www /www 即是本地網頁存放目錄
(2) cd /www 添加配置檔案
cp config.sample.inc.php config.inc.php
(3) vim config.inc.php 修改以下參數
$cg['Servers'][$i]['host'] = 'localhost'; $cfg['Servers'][$i]['host'] = '172.16.23.7';
//mysql的主機ip位址
$cfg['blowfish_secret'] = '';修改為$cfg['blowfish_secret'] = 'fsfsfsfs';
//這裡随便寫些值,主要是cookier加密,不改無法登入。
(4)驗證通路
http://172.16.23.11
輸入mysql伺服器上邊的
使用者名:pam
及密碼:12345
登入成功
Web2設定類似,這裡不再書寫!
6、mysql伺服器進行設定
進入mysql指令行,給web1,web2授權通路資料庫
grant all privileges on *.* to [email protected] identified by '12345';
grant all privileges on *.* to [email protected] identified by '12345';
flush privileges;
7、web1登入pam時出現問題及解決方法
Cookies 必須啟用才能登入。
多次嘗試無法登入,修改多次config.inc.php無結果。
最後上網查可能是時間有問題。
我就在不能登入的那台伺服器看,時間錯了兩個月
Date 0414220812.33 //月日時分年秒
Hwclock –w //指設定好的系統時間同步到硬體裡
進行嘗成功!
8、LVS/DR設定
持久防火牆标記包持久性
實作将同一個服務的不同端口定向到同一個realserver
即用戶端請求發往80,及443端口都将由LVS通過算法定向到同一個realserver
ipvsadm -C
iptables -t mangle -L -n
iptables -t mangle -A PREROUTING -i eth0 -p tcp -d 172.16.23.1 --dport 80 -j MARK --set-mark 20 [0-99整數] //定義标記
iptables -t mangle -A PREROUTING -i eth0 -p tcp -d 172.16.23.1 --dport 443 -j MARK --set-mark 20 [0-99]
iptables 定義叢集,及叢集服務清單
ipvsadm -a -f 20 -r 172.16.23.11 -g -w 3
ipvsadm -a -f 20 -r 172.16.23.12 -g -w 2
ipvsadm -Ln
9、 進行驗證
Xp用戶端浏覽器輸入 http://172.16.23.1,出現如下内容。
成功!
要mysql伺服器上進行ab測試
ab -c 100 -n 5000 http://172.16.23.1/index.php
LVS伺服器上檢視,ipvsadm –Lcn
至此整個過程完成!!!