你的位置:首页 > 系统架构
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/bashif [ "$(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 fifi
KeepAlived的启动
/usr/local/keepalived/sbin/keepalived -D -f /usr/local/keepalived/etc/keepalived/keepalived.conf
4.测试
原文:http://www.zrwm.com/?p=1824
发表评论: