LB 負載均衡集羣分兩類: LVS (四層)和 nginx 或 haproxy (七層)
客戶端通過訪問分發器的 VIP 來訪問網站
|
現在應用更復雜,比如現在網站頁面有: .php .html .png .jpeg .jsp 等, 有勱態頁面有靜
態頁面。靜態頁面一般是丌變的,想訪問更快些,前面學習過 SQUID。
|
但是前面的 LVS 是四層的。基於 IP 的。現在需要在應用層基於丌同的應用迚行分發。
|
七層 LB , Nginx / Haproxy 都可以支持 7 層 LB
現在實現以下功能,拓撲圖:
注:使用 Nginx 或 Haproxy 時,這裏訪問處理都需要經過分發器,沒有像 LVS/DR 方式。
工作中,希望這樣:
靜態文件處理:可以使用 nginx 或 apache
動文件處理: apache ,tomcat
圖片文件處理: squid
使用 nginx 實現動靜分離的負載均衡集羣
1. Nginx 負載均衡基礎知識
nginx 的 upstream 目前最常用 3 種方式的分配
1)、輪詢(默認)
每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器 down 掉,能自動剔除。
2)、weight
挃定輪詢機率,weight 和訪問比率成正比,用於後端服務器性能不均的情況。
3)、ip_hash
每個請求挄訪問 ip 的 hash 結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決 session 的
問題。
4)、fair(第三方)
挄後端服務器的響應時間來分配請求,響應時間短的優先分配。
5)、url_hash(第三方) url 哈希
實例 1:使用 nginx 實現負載均衡和動靜分離
源碼編譯安裝 nginx
一、安裝 nginx 時必須先安裝相應的編譯工具
[root@xuegod63 ~]#yum -y install gcc gcc-c++ autoconf automake
[root@xuegod63 ~]#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
zlib:nginx 提供 gzip 模塊,需要 zlib 庫支持
openssl:nginx 提供 ssl 功能
pcre:支持地址重寫 rewrite 功能
安裝 nginx:
[root@xuegod63 ~]# ll nginx-1.8.0.tar.gz -h #整個 nginx 文件不到 813K,很小
-rw-r--r-- 1 root root 813K Jul 14 20:17 nginx-1.8.0.tar.gz
[root@xuegod63 ~]# tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/src/
[root@xuegod63 ~]# cd /usr/local/src/nginx-1.8.0/
[root@xuegod63 ~]# ./configure --prefix=/server/nginx-1.8.0 --with-http_dav_module
--with-http_stub_status_module --with-http_addition_module --with-http_sub_module
--with-http_flv_module --with-http_mp4_module
查看參數:
[root@xuegod63 nginx-1.8.0]# ./configure --help | grep mp4
參數:
--with-http_dav_module 吭用 ngx_http_dav_module 支持(增加 PUT,DELETE,MKCOL:創建集
合,COPY 和 MOVE 方法)默認情況下爲關閉,需編譯開啓
--with-http_stub_status_module 吭用 ngx_http_stub_status_module 支持(獲取 nginx 自上次啓
動以來的工作狀態)
--with-http_addition_module 啓用 ngx_http_addition_module 支持(作爲一個輸出過濾器,支持
不完全緩衝,分部分響應請求)
--with-http_sub_module 啓用 ngx_http_sub_module 支持(允許用一些其他文本替換 nginx 響應中
的一些文本)
--with-http_flv_module 啓用 ngx_http_flv_module 支持(提供尋求內存使用基於時間的偏移量文件)
--with-http_mp4_module 啓用對 mp4 文件支持(提供尋求內存使用基於時間的偏移量文件)
編譯和安裝:
[root@xuegod63 ~]#make -j 4
[root@xuegod63 ~]#make install
生成運行 nginx 的用戶:
[root@xuegod63 nginx-1.8.0]# useradd -u 8000 -s /sbin/nologin nginx
[root@xuegod63 nginx-1.8.0]# id !$
id nginx
uid=8000(nginx) gid=8000(nginx) groups=8000(nginx)
nginx 主要目錄結構:
[root@xuegod63 /]# ls /server/nginx-1.8.0/
conf html logs sbin
conf #配置文件
html #網站根目錄
logs #日誌
sbin #nginx 啓動腳本
主配置文件:
[root@xuegod63 /]# ls /server/nginx-1.8.0/conf/nginx.conf
啓動 nginx:
[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx
[root@xuegod63 /]# netstat -antup | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
5281/httpd
[root@xuegod63 /]# netstat -antup | grep :80
開機啓動:
[root@xuegod63 nginx-1.8.0]# echo '/server/nginx-1.8.0/sbin/nginx & ' >> /etc/rc.local
測試:
http://192.168.1.63/
nginx 服務日常操作:
測試配置文件語法:
[root@xuegod63 nginx-1.8.0]# /server/nginx-1.8.0/sbin/nginx -t
nginx: the configuration file /server/nginx-1.8.0/conf/nginx.conf syntax is ok
nginx: configuration file /server/nginx-1.8.0/conf/nginx.conf test is successful
重新加載配置文件
[root@xuegod63 nginx-1.8.0]# /server/nginx-1.8.0/sbin/nginx -s reload
關閉 nginx
[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx -s stop
[root@xuegod63 /]# /server/nginx-1.8.0/sbin/nginx -s start #沒有 start 參數
nginx: invalid option: "-s start"
配置 nginx 成爲分發器,實現動靜分離
[root@xuegod63 conf]# cd /server/nginx-1.8.0/conf #配置文件目錄
[root@xuegod63 conf]# cp nginx.conf nginx.conf.back #備份一下配置文件
[root@xuegod63 conf]# vim nginx.conf
[root@xuegod63 nginx-1.8.0]# vim /server/nginx-1.8.0/conf/nginx.conf #指定啓動 nginx 用戶
改:# user nobody;
爲:user nginx nginx;
改:
43 location / {
44 root html;
45 index index.html index.htm; #在 location / { 。。。} 中添加以下內容
#定義分發策略
location / {
root html;
index index.html index.htm;
if ($request_uri ~* \.html$){
proxy_pass http://htmlservers;
}
if ($request_uri ~* \.php$){
proxy_pass http://phpservers;
}
proxy_pass http://picservers;
}
如圖:
把以一內容註釋掉,否則 php 文件直接在 nginx 服務器上解析了,不再解析給後端服務器:
# location ~ \.php$ {
73 # root html;
74 # fastcgi_pass 127.0.0.1:9000;
75 # fastcgi_index index.php;
76 # fastcgi_param SCRIPT_FILENAME
/server/nginx-1.8.0/html$fastcgi_script_name;
77 # include fastcgi_params;
78 # }
如圖:
#定義負載均衡設備的 Ip
#定義負載均衡設備的 Ip
在配置文件 nginx.conf 的最後一行}前,添加以下內容:
upstream htmlservers { #定義負載均衡服務器組名稱
server 192.168.1.62:80;
server 192.168.1.64:80;
}
upstream phpservers{
server 192.168.1.62:80;
server 192.168.1.64:80;
}
upstream picservers {
server 192.168.1.62:80;
server 192.168.1.64:80;
}
#後期工作中,根據工作中的需要,配置成具體業務的 IP 地址
如圖:
保存退出。
重新加載 nginx 服務器配置文件:
[root@xuegod63 conf]# /server/nginx-1.8.0/sbin/nginx -t
nginx: the configuration file /server/nginx-1.8.0/conf/nginx.conf syntax is ok
nginx: configuration file /server/nginx-1.8.0/conf/nginx.conf test is successful
[root@xuegod63 conf]# /server/nginx-1.8.0/sbin/nginx -s reload
配置後端服務器: xuegod62
配置 web 服務器:
[root@xuegod62 html]# yum install httpd php -y
生成靜態測試文件:
root@xuegod62 html]#echo 192.168.1.62 > /var/www/html/index.html
生成動態測試文件:
[root@xuegod62 html]#vim /var/www/html/test.php #寫如以下內容:
192.168.1.62-php
<?php
phpinfo();
?>
生成圖片文件:
上傳如下圖片,到“xuegod62 網站/var/www/html/目錄下:
啓動 apache 服務器:
[root@xuegod62 html]# service httpd restart
配置後端服務器: xuegod64
IP: 192.168.1.64
配置 web 服務器:
[root@xuegod64 html]# yum install httpd php -y
生成靜態測試文件:
echo 192.168.1.64 > /var/www/html/index.html
生成動態測試文件:
vim /var/www/html/test.php #寫如以下內容:
192.168.1.64-php
<?php
phpinfo();
?>
生成圖片文件:
上傳如下圖片,到“xuegod64 網站/var/www/html/目錄下:
[root@xuegod64 html]# service httpd restart
到此 nginx 實現負載均衡結束。
測試轉發靜態頁面:
http://192.168.1.63/index.html
測試轉發勱態頁面:
http://192.168.1.63/test.php
測試轉發圖片:
http://192.168.1.63/pic.jpg
測試性能:
擴展: 文件打開數過多
[root@xuegod64 html]# ab -n 1000 -c 1000 http://192.168.1.62/index.html #運行正常
[root@xuegod64 html]# ab -n 2000 -c 2000 http://192.168.1.62/index.html #報錯
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.1.62 (be patient)
socket: Too many open files (24) # 測試時,一次打開的 socket 文件太多。
#ulimit -a #查看
#ulimit -n
1024
系統默認一個迚程最多同時允許打開 1024 的文件
解決:
#ulimit -n 10240 #報錯的解決方法
總結,擴展:
如有 tomcat ,apache,squid 配置爲如下:
[root@xuegod63 conf]# vim nginx.conf # 在最後添加以下內容。 定義服務器組
upstream tomcat_servers {
server 192.168.1.2:8080;
server 192.168.1.1:8080;
server 192.168.1.11:8080;
}
upstream apache_servers {
server 192.168.1.5:80;
server 192.168.1.177:80;
server 192.168.1.15:80;
}
upstream squid_servers {
server 192.168.1.26:3128;
server 192.168.1.55:3128;
server 192.168.1.18:3128;
}
#本文中的IP可根據實際的來配置