一、概述:
LB負載均衡集群分兩類: LVS (四層)和 nginx或haproxy (七層)
客戶端通過訪問分發器的VIP來訪問網站
|
現在應用更復雜,比如現在網站頁面有: .php .html .png .jpeg .jsp 等, 有動態頁面有靜態頁面。
靜態頁面一般是不變的,想訪問更快些,可以學習SQUID。
|
但是前面的LVS是四層的。基於IP的。現在需要在應用層基於不同的應用進行分發。
|
七層LB , Nginx / Haproxy都可以支持7層LB
現在實現以下功能,拓撲圖:
工作中,希望這樣:
靜態文件處理:可以使用nginx 或apache
動文件處理: apache ,tomcat
圖片文件處理: squid
使用nginx實現動靜分離的負載均衡集群
Nginx 負載均衡基礎知識
Nginx 的 upstream 負載的5種方式,目前最常用 前3 種方式
1)、輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器 down 掉,能自動剔除。
2)、weight
指定輪詢幾率,weight 和訪問比率成正比,用於后端服務器性能不均的情況。
3)、ip_hash
每個請求按訪問 ip 的 hash 結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決 session 的問題。
4)、fair(第三方)
按后端服務器的響應時間來分配請求,響應時間短的優先分配。
5)、url_hash(第三方) url哈西
按訪問url的hash結果來分配請求,使同樣的url定向到同一個后端服務器,后端服務器為緩存時比較有效
二、實驗操作:
1、源碼編譯安裝nginx
1、安裝nginx時必須先安裝相應的編譯工具和相關依賴 [root@xuegod63 ~]#yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel zlib:nginx提供gzip模塊,需要zlib庫支持 openssl:nginx提供ssl功能 pcre:支持地址重寫rewrite功能 安裝nginx: [root@xuegod63 ~]# ll nginx-1.12.2.tar.gz -h #整個nginx文件不到只813K,很小 -rw-r--r-- 1 root root 813K Jul 14 20:17 nginx-1.12.2.tar.gz [root@xuegod63 ~]# tar -zxvf nginx-1.12.2.tar.gz -C /usr/local/src/ [root@xuegod63 ~]# cd /usr/local/src/nginx-1.12.2/ [root@xuegod63 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --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.12.2]# ./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文件支持(提供尋求內存使用基於時間的偏移量文件) 編譯和安裝: (查看CPU邏輯數cat /proc/cpuinfo | grep processor | wc -l) [root@xuegod63 nginx-1.12.2]# make -j 4 [root@xuegod63 nginx-1.12.2]# make install 生成運行nginx的用戶: [root@xuegod63 nginx-1.12.2]# useradd -u 8000 -s /sbin/nologin nginx [root@xuegod63 nginx-1.12.2]# id !$ id nginx uid=8000(nginx) gid=8000(nginx) groups=8000(nginx) nginx主要目錄結構: [root@xuegod63 nginx-1.12.2]# cd /usr/local/nginx/ [root@xuegod63 nginx]# ls conf html logs sbin conf #配置文件 html #網站根目錄 logs #日志 sbin #nginx啟動腳本 主配置文件: [root@xuegod63 nginx]# ls /usr/local/nginx/conf/ fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default fastcgi_params koi-win nginx.conf scgi_params.default win-utf 啟動nginx: [root@xuegod63 nginx]# ls conf html logs sbin [root@xuegod63 nginx]# ./sbin/nginx [root@xuegod63 nginx]# netstat -antup|grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7857/nginx: master [root@xuegod63 nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [root@xuegod63 nginx]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@xuegod63 nginx]# nginx -v nginx version: nginx/1.12.2
測試:
http://192.168.1.63/
2.nginx服務日常操作
配置nginx成為分發器,實現動靜分離
[root@xuegod63 conf]# cd /usr/local/nginx/conf #配置文件目錄 [root@xuegod63 conf]# cp nginx.conf nginx.conf.bak #備份一下配置文件 [root@xuegod63 conf]# vim nginx.conf [root@xuegod63 conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@xuegod70 conf]# nginx -s reload 改:# 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服務器上解析了,不再解析給后端服務器: 72 # 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]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@xuegod63 conf]# nginx -s reload
3.配置后端服務器
[root@xuegod62 ~]## yum install httpd php -y 生成靜態測試文件: [root@xuegod62 ~]# echo 192.168.10.71 > /var/www/html/index.html [root@xuegod62 ~]# vim /var/www/html/test.php 生成動態測試文件: [root@xuegod62 html]#vim /var/www/html/test.php #寫如以下內容: 192.168.1.62-php <?php phpinfo(); ?> 啟動apache服務器: [root@xuegod62 html]# service httpd restart
生成圖片文件:
上傳如下圖片,到xuegod62網站/var/www/html/目錄下:
然后進行測試:http://192.168.1.63
三、測試性能
擴展: 文件打開數過多 [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 #報錯的解決方法
四、Nginx負載的5種策略設置方法:
1、輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。
upstream backserver {
server 192.168.1.62;
server 192.168.1.64;
}
2、指定權重
指定輪詢幾率,weight和訪問比率成正比,用於后端服務器性能不均的情況。
upstream backserver {
server 192.168.1.62 weight=1;
server 192.168.1.64 weight=2;
}
3、IP綁定 ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
upstream backserver {
ip_hash;
server 192.168.1.62:80;
server 192.168.1.64:80;
}
4、fair(第三方)
按后端服務器的響應時間來分配請求,響應時間短的優先分配。
upstream backserver {
server server1;
server server2;
fair;
}
5、url_hash(第三方)
按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效。
upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
總結,擴展:
如有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;
}