什么是負載均衡
在計算機的世界,這就是大家耳熟能詳的負載均衡(load balancing),所謂負載均衡,就是說如果一組計算機節點(或者一組進程)提供相同的(同質的)服務,那么對服務的請求就應該均勻的分攤到這些節點上。
這里的服務是廣義的,可以是簡單的計算,也可能是數據的讀取或者存儲。負載均衡也不是新事物,這種思想在多核CPU時代就有了,只不過在分布式系統中,負載均衡更是無處不在,這是分布式系統的天然特性決定的,分布式就是利用大量計算機節點完成單個計算機無法完成的計算、存儲服務,既然有大量計算機節點,那么均衡的調度就非常重要。
負載均衡的意義在於,讓所有節點以最小的代價、最好的狀態對外提供服務,這樣系統吞吐量最大,性能更高,對於用戶而言請求的時間也更小。而且,負載均衡增強了系統的可靠性,最大化降低了單個節點過載、甚至crash的概率。不難想象,如果一個系統絕大部分請求都落在同一個節點上,那么這些請求響應時間都很慢,而且萬一節點降級或者崩潰,那么所有請求又會轉移到下一個節點,造成雪崩。
如何實現負載均衡
回答可以如下:在nginx里面配置一個upstream,然后把相關的服務器ip都配置進去。然后采用輪詢的方案,然后在nginx里面的配置項里,proxy-pass指向這個upstream,這樣就能實現負載均衡。
nginx的負載均衡有4種模式:
1)、輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。
2)、weight
指定輪詢幾率,weight和訪問比率成正比,用於后端服務器性能不均的情況。
3)、ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
4)、fair , url_hash(第三方)
按后端服務器的響應時間來分配請求,響應時間短的優先分配。
負載均衡配置方法
打開nginx.conf文件,在http節點下添加upstream節點:
upstream webname {
server 192.168.0.1:8080;
server 192.168.0.2:8080;
}
其中webname是自己取的名字,最后會通過這個名字在url里訪問的,像上面這個例子一樣什么都不加就是默認的輪詢,第一個請求過來訪問第一個server,第二個請求來訪問第二個server。依次輪着來。
upstream webname {
server 192.168.0.1:8080 weight 2;
server 192.168.0.2:8080 weight 1;
}
這個weight也很好理解,權重大的被訪問的概率就大,上面這個例子的話,訪問2次server1,訪問一次server2
upstream webname {
ip_hash;
server 192.168.0.1:8080;
server 192.168.0.2:8080;
}
ip_hash的配置也很簡單,直接加一行就可以了,這樣只要是同一個ip過來的都會到同一台server上。然后在server節點下進行配置:
location /name {
proxy_pass http://webname/name/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
proxy_pass里面用上面配的webname代替了原來的ip地址。
這樣就基本完成了負載均衡的配置。
下面是主備的配置:
還是在upstream里面
upstream webname {
server 192.168.0.1:8080;
server 192.168.0.2:8080 backup;
}
設置某一個節點為backup,那么一般情況下所有請求都訪問server1,當server1掛掉或者忙的的時候才會訪問server2
upstream webname {
server 192.168.0.1:8080;
server 192.168.0.2:8080 down;
}
設置某個節點為down,那么這個server不參與負載。
實現實例
1 測試環境
由於沒有服務器,所以本次測試直接host指定域名,然后在VMware里安裝了三台CentOS。
測試域名 :a.com
A服務器IP :192.168.5.149 (主)
B服務器IP :192.168.5.27
C服務器IP :192.168.5.126
2 部署思路
A服務器做為主服務器,域名直接解析到A服務器(192.168.5.149)上,由A服務器負載均衡到B服務器(192.168.5.27)與C服務器(192.168.5.126)上。
3 域名解析
由於不是真實環境,域名就隨便使用一個a.com用作測試,所以a.com的解析只能在hosts文件設置。
打開:C:Windows\System32\drivers\etc\hosts
在末尾添加
192.168.5.149 a.com
保存退出,然后啟動命令模式ping下看看是否已設置成功
A服務器nginx.conf設置
打開nginx.conf,文件位置在nginx安裝目錄的conf目錄下。
在http段加入以下代碼
upstream a.com {
server 192.168.5.126:80;
server 192.168.5.27:80;
}
server{
listen 80;
server_name a.com;
location / {
proxy_pass http://a.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
保存重啟nginx
B、C服務器nginx.conf設置
打開nginx.conf,在http段加入以下代碼
server{
listen 80;
server_name a.com;
index index.html;
root /data0/htdocs/www;
}
保存重啟nginx
測試
當訪問a.com的時候,為了區分是轉向哪台服務器處理我分別在B、C服務器下寫一個不同
內容的index.html文件,以作區分。打開瀏覽器訪問a.com結果,刷新會發現所有的請求
均分別被主服務器(192.168.5.149)分配到B服務器(192.168.5.27)與C服務器
(192.168.5.126)上,實現了負載均衡效果。
多域名、多泛域名負載綁定示例:
比如綁定*.w2w2.cn、*.xk8ds2d.cn兩個域名到負載上:
user www www; worker_processes auto; worker_cpu_affinity auto; error_log /home/wwwlogs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; multi_accept off; accept_mutex off; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 50m; sendfile on; sendfile_max_chunk 512k; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]."; #limit_conn_zone $binary_remote_addr zone=perip:10m; ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section. server_tokens off; access_log off; # 代到本機 8080端口 server{ listen 8080; server_name *.w2w2.cn *.xk8ds2d.cn; index index.html index.php; root /home/wwwroot/default; include enable-php.conf; } # 負載均衡模塊 upstream *.w2w2.cn { server 103.242.133.21:80; server 127.0.0.1:8080; } #監聽80端口的訪問 server{ listen 80; server_name *.w2w2.cn; location / { proxy_pass http://*.w2w2.cn; #proxy_set_header Host $host; #proxy_set_header X-Real-IP $remotr_addr; #proxy_set_header X-Forwarde-For $proxy_add_x_forwarded_for; } } # 負載均衡模塊 upstream *.xk8ds2d.cn { server 103.242.133.21:80; server 127.0.0.1:8080; } #監聽80端口的訪問 server{ listen 80; server_name *.xk8ds2d.cn; location / { proxy_pass http://*.xk8ds2d.cn; #proxy_set_header Host $host; #proxy_set_header X-Real-IP $remotr_addr; #proxy_set_header X-Forwarde-For $proxy_add_x_forwarded_for; } } include vhost/*.conf; }
副服務器nginx.conf代碼:
user www www; worker_processes auto; worker_cpu_affinity auto; error_log /home/wwwlogs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; multi_accept off; accept_mutex off; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 50m; sendfile on; sendfile_max_chunk 512k; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]."; #limit_conn_zone $binary_remote_addr zone=perip:10m; ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section. server_tokens off; access_log off; server { listen 80 default_server reuseport; #listen [::]:80 default_server ipv6only=on; server_name _; index index.html index.htm index.php; root /home/wwwroot/default; #error_page 404 /404.html; # Deny access to PHP files in specific directory #location ~ /(wp-content|uploads|wp-includes|images)/.*.php$ { deny all; } include enable-php.conf; location /nginx_status { stub_status on; access_log off; } location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*.(js|css)?$ { expires 12h; } location ~ /.well-known { allow all; } location ~ /. { deny all; } access_log /home/wwwlogs/access.log; } include vhost/*.conf; }
來源:http://www.startphp.cn/front/php/0327160.html