Nginx由於配置簡單,擴展能力好,也很適合用來做負載均衡的解決方案.
環境說明:
192.168.1.191: 後台web服務器01
192.168.1.192: 後台web服務器02
192.168.1.190: Nginx Master服務器(lb0)
192.168.1.189: Nginx Backup服務器(lb1)
192.168.1.195: 負責對外提供服務的虛擬IP,該VIP由KeepAlived管理
VIP的綁定說明:
Internet-- | ============= | ISP Router| ============= | | | |eth0 -> 192.168.1.190 (connected to lan) |-lb0==| | |eth1 -> 192.168.1.195 (vip master) | | |eth0 -> 192.168.1.189 (connected to lan) |-lb1==| |eth1 -> 192.168.1.195 (vip backup)
VIP可以綁定到eth1,通過eth1與外網連接對外提供服務.本文配置測試的時候綁定到eth0:1
1.Nginx安裝
wget -c http://nginx.org/download/nginx-1.2.6.tar.gz tar zxvf nginx-1.2.6.tar.gz cd nginx-1.2.6 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_stub_status_module --with-pcre --with-http_gzip_static_module --with-openssl=/data/install/openssl-1.0.1c make && make install
2. Nginx配置
#vi /usr/local/nginx/conf/nginx.conf
user www www; worker_processes 2; pid logs/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; } http { ## # basic settings ## include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; ## # logging settings ## #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; #error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; gzip_disable "msie6"; #include /usr/local/nginx/conf/sites-enabled/*; include lb.conf; }
#vi /usr/local/nginx/conf/lb.conf
upstream backend { #ip_hash; server 192.168.1.191:80 max_fails=2 fail_timeout=5s; server 192.168.1.192:80 max_fails=2 fail_timeout=5s; } server { listen 80; server_name 192.168.1.195; #listen on VIP location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
將lb0機器上的nginx配置文件nginx.conf和lb.conf復制到lb1機器上.
3.KeepAlived的配置及啟動
#vi /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind=1 #add this line to sysctl.conf
#sysctl -p
lb0和lb1的KeepAlived配置keepalived.conf如下:(不同的部分見紅色部分注釋)
#vi /usr/local/keepalived/etc/keepalived.conf
global_defs { router_id LVS_MASTER_1 } vrrp_script check_http_port { script "/usr/local/nginx/monitoring.sh" interval 2 #check every 2 secs weight 2 # add 2 points of priority if OK } vrrp_instance VI_1 { state MASTER # or "BACKUP" on backup interface eth0 priority 101 # 101 on master, 100 on backup advert_int 1 virtual_router_id 195 authentication { auth_type PASS auth_pass 123456 } track_script { check_http_port } virtual_ipaddress { 192.168.1.195 } }
Nginx監控腳本:
#cat /usr/local/nginx/monitoring.sh
#!/bin/bash if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]; then /usr/local/nginx/sbin/nginx sleep 5 if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ];then killall keepalived fi fi
KeepAlived的啟動
/usr/local/keepalived/sbin/keepalived -D -f /usr/local/keepalived/etc/keepalived/keepalived.conf
4.測試
原文:http://www.zrwm.com/?p=1824