揭秘企业级web负载均衡完美架构

本文介绍了一种基于Nginx和Keepalived的企业级负载均衡高可用架构方案,包括具体的安装配置步骤,以及如何实现自动故障转移。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相信很多朋友对企业级的负载均衡高可用实例非常感兴趣,此篇文章根据成熟的线上环境而写,旨在帮助大家迅速架构一个企业级的负载均衡高可用的web环境。

此系统架构仅映射内网VIP的80及443端口于外网的Juniper防火墙下,其它端口均关闭,内网所有机器均关闭iptables及ipfw防火墙;外网DNS指向即通过Juniper映射出来的外网地址,而此映射的地址对映的其实是内网VIP地址。这里说下端口的问题,有的朋友可能会很疑惑,这样映射端口行不?通过项目实践得知,这样完全是可行的,php-cgi需要的9000端口及MySQL的3306端口均可走内网,完全不影响业务系统的运行。

另外,我维护的电子商务网站并发大约在1000左右,此时,Nginx+Apache集群运行得非常稳定,尤其是apache,并没有想象中那般弱;其实,在内存足够(>=8G)的情况,测试时不连数据库的话,单台apache+php5能顶得住6000并发,而且相当稳定。在网站升级架构方面,我不赞成全面淘汰生级,锦上添花式的升级会更好。

第一部分:Nginx+Keepalived的说明及环境说明

喜欢看我博客或文章的朋友都知道,我一直主力推崇Nginx+Keepalived作web的负载均衡高可用架构,并积极将其用于项目方案中;Nginx负载均衡作服务器遇到的故障一般有①服务器网线松动等网络故障;②服务器硬件故障从而crash;③nginx服务死掉;遇到前二者情况,keeaplived是能起到HA的作用的;然而遇到③种情况就没有办法了,但可以通过shell监控解决这问题,从而实现真正意义上的负载均衡高可用。此篇的最新更新时间为2010年6月25号,下面将其安装步骤详细说明下:

环境:

  1. centos5.3(64位)、nginx-0.7.51、keepalived-1.1.15
  2. 主nginx负载均衡器:192.168.0.154
  3. 辅nginx负载均衡器:192.168.9.155
  4. vip:192.168.0.188

第二部分:分别安装Nginx负载均衡器及相关配置脚本

先安装Nginx负载均衡器,nginx负载的配置就用一般的模板来配置了

  1. #添加运行nginx的用户和组www
  2. groupaddwww
  3. useradd-gwwwwww
  4. wgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz
  5. tarzxvfpcre-7.8.tar.gz
  6. cdpcre-7.8/
  7. ./configure
  8. make&&makeinstall
  9. wgethttp://sysoev.ru/nginx/nginx-0.7.51.tar.gz
  10. tarzxvfnginx-0.7.51.tar.gz
  11. cdnginx-0.7.51/
  12. ./configure--user=www--group=www--prefix=/usr/local/webserver/nginx--with-http_stub_status_module--with-http_ssl_module
  13. make&&makeinstall

配置nginx负载均衡器的配置文件vim /usr/local/nginx/conf/nginx.conf,此篇文章仅仅只是我的某项目的配置文档,纯80转发;如果对nginx配置有https要求的可参考张宴的相关文章。

  1. userwwwwww;
  2. worker_processes8;
  3. pid/usr/local/nginx/logs/nginx.pid;
  4. worker_rlimit_nofile65535;
  5. events
  6. {
  7. useepoll;
  8. worker_connections65535;
  9. }
  10. http{
  11. includemime.types;
  12. default_typeapplication/octet-stream;
  13. server_names_hash_bucket_size128;
  14. client_header_buffer_size32k;
  15. large_client_header_buffers432k;
  16. client_max_body_size8m;
  17. sendfileon;
  18. tcp_nopushon;
  19. keepalive_timeout60;
  20. tcp_nodelayon;
  21. fastcgi_connect_timeout300;
  22. fastcgi_send_timeout300;
  23. fastcgi_read_timeout300;
  24. fastcgi_buffer_size64k;
  25. fastcgi_buffers464k;
  26. fastcgi_busy_buffers_size128k;
  27. fastcgi_temp_file_write_size128k;
  28. gzipon;
  29. gzip_min_length1k;
  30. gzip_buffers416k;
  31. gzip_http_version1.0;
  32. gzip_comp_level2;
  33. gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml;
  34. gzip_varyon;
  35. upstreambackend
  36. {
  37. server192.168.1.102:80;
  38. server192.168.1.103:80;
  39. server192.168.1.105:80;
  40. }
  41. server{
  42. listen80;
  43. server_namewww.yuhongchun027.com;
  44. location/{
  45. root/var/www;
  46. indexindex.jspindex.htmindex.html;
  47. proxy_redirectoff;
  48. proxy_set_headerHost$host;
  49. proxy_set_headerX-Real-IP$remote_addr;
  50. proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
  51. proxy_passhttp://backend;
  52. }
  53. location/nginx{
  54. access_logon;
  55. auth_basic"NginxStatus";
  56. auth_basic_user_file/usr/local/nginx/htpasswd;
  57. }
  58. log_formataccess'$remote_addr-$remote_user[$time_local]"$request"'
  59. '$status$body_bytes_sent"$http_referer"'
  60. '"$http_user_agent"$http_x_forwarded_for';
  61. access_log/var/log/access.logaccess;
  62. }
  63. }

小节:

第一部分和第二部分讲的是如何通过安装Nginx来达到负载均衡后端web集群的过程,Nginx能实现自动切换后端有故障的web服务器;但Nginx负载均衡器出了问题怎么办呢,它们之间是如何实现无故障转移的呢?

第三部分:安装Keepalived,让其分别作web及Nginx的HA

安装keepalived,并将其做成服务模式,方便以后调试。

  1. wgethttp://www.keepalived.org/software/keepalived-1.1.15.tar.gz
  2. #tarzxvfkeepalived-1.1.15.tar.gz
  3. #cdkeepalived-1.1.15
  4. #./configure--prefix=/usr/local/keepalived
  5. #make
  6. #makeinstall
  7. #cp/usr/local/keepalived/sbin/keepalived/usr/sbin/
  8. #cp/usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
  9. #cp/usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/
  10. #mkdir/etc/keepalived
  11. #cd/etc/keepalived/
  12. vimkeepalived.conf
  13. !ConfigurationFileforkeepalived
  14. global_defs{
  15. notification_email{
  16. yuhongchun027@163.com
  17. }
  18. notification_email_fromkeepalived@chtopnet.com
  19. smtp_server127.0.0.1
  20. smtp_connect_timeout30
  21. router_idLVS_DEVEL
  22. }
  23. vrrp_instanceVI_1{
  24. stateMASTER
  25. interfaceeth0
  26. virtual_router_id51
  27. mcast_src_ip192.168.0.154<==主nginx的IP地址
  28. priority100
  29. advert_int1
  30. authentication{
  31. auth_typePASS
  32. auth_passchtopnet
  33. }
  34. virtual_ipaddress{
  35. 192.168.0.188<==vip地址
  36. }
  37. }
  38. #servicekeepalivedstart

我们来看一下日志:

  1. [root@ltos~]#tail/var/log/messages
  2. Oct603:25:03ltosavahi-daemon[2306]:Registeringnewaddressrecordfor192.168.0.188oneth0.
  3. Oct603:25:03ltosavahi-daemon[2306]:Registeringnewaddressrecordfor192.168.0.154oneth0.
  4. Oct603:25:03ltosavahi-daemon[2306]:RegisteringHINFOrecordwithvalues'I686'/'LINUX'.
  5. Oct603:25:23ltosavahi-daemon[2306]:Withdrawingaddressrecordforfe80::20c:29ff:feb9:eeaboneth0.
  6. Oct603:25:23ltosavahi-daemon[2306]:Withdrawingaddressrecordfor192.168.0.154oneth0.
  7. Oct603:25:23ltosavahi-daemon[2306]:Hostnameconflict,retryingwith<ltos-31>

很显然vrrp已经启动,我们还可以通过命令来检查

  1. [root@ltoshtml]#ipaddr
  2. 1:lo:<LOOPBACK,UP,LOWER_UP>mtu16436qdiscnoqueue
  3. link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
  4. inet127.0.0.1/8scopehostlo
  5. inet6::1/128scopehost
  6. valid_lftforeverpreferred_lftforever
  7. 2:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_fastqlen1000
  8. link/ether00:0c:29:ba:9b:e7brdff:ff:ff:ff:ff:ff
  9. inet192.168.0.154/24brd192.168.0.255scopeglobaleth0
  10. inet192.168.0.188/32scopeglobaleth0
  11. inet6fe80::20c:29ff:feba:9be7/64scopelink
  12. valid_lftforeverpreferred_lftforever
  13. 3:sit0:<NOARP>mtu1480qdiscnoop
  14. link/sit0.0.0.0brd0.0.0.0

说明vip已经启动,这样主服务器就配置好了,辅机的配置大致一样,除了配置文件有少部分的变化,下面贴出辅机的配置文件:

  1. !ConfigurationFileforkeepalived
  2. global_defs{
  3. notification_email{
  4. yuhongchun027@163.com
  5. }
  6. notification_email_fromkeepalived@chtopnet.com
  7. smtp_server127.0.0.1
  8. smtp_connect_timeout30
  9. router_idLVS_DEVEL
  10. }
  11. vrrp_instanceVI_1{
  12. stateBACKUP
  13. interfaceeth0
  14. virtual_router_id51
  15. mcast_src_ip192.168.0.155<==辅nginx的IP的地址
  16. priority100
  17. advert_int1
  18. authentication{
  19. auth_typePASS
  20. auth_passchtopnet
  21. }
  22. virtual_ipaddress{
  23. 192.168.0.188
  24. }
  25. }

第四部分:针对Keepalived的不足,用Nginx_pid.sh来监控nginx进程,实现真正意义上的负载均衡高可用。

针对Nginx+Keepalived,编写nginx监控脚本nginx_pid.sh,此脚本思路其实也很简单,即放置在后台一直监控nginx进程;如进程消失,尝试重启nginx,如是失败则立即停掉本机的keepalived服务,让另一台负载均衡器接手,此脚本直接从生产环境下载:

  1. vim/root/nginx_pid.sh
  2. #!/bin/bash
  3. while:
  4. do
  5. nginxpid=`ps-Cnginx--no-header|wc-l`
  6. if[$nginxpid-eq0];then
  7. /usr/local/nginx/sbin/nginx
  8. sleep5
  9. nginxpid=`ps -C nginx --no-header | wc -l`
  10. if[$nginxpid-eq0];then
  11. /etc/init.d/keepalivedstop
  12. fi
  13. fi
  14. sleep5
  15. done

然后置于后台运行 sh /root/nginx_pid.sh &,这种写法是错误的,这样你用root用户logout后,此进程会消失;正确写法为nohup/bin/bash /root/nginx_pid.sh &,附带下注释:如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令。该命令可以在你退出root帐户之后继续运行相应的进程。nohup就是不挂起的意思( no hang up),哈哈,差点老马失蹄了。

后记:

我的线上环境网络非常复杂,这也是LVS+Keepalived失败的原因。目前此套架构在1000并发的电子商务网站非常稳定,带来的直接影响就是nginx_backup一直处于闲置状态。相对于张宴的双机轮询而言,我感觉他的可能更加完美,因为目前我的Nginx仅仅只做了负载均衡器,如果以后有机会我会尝试做负载均衡器/反向代理加速。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值