高可用负载均衡部署方案---适用中小应用

本文介绍了一种使用Nginx实现高可用负载均衡的方案。通过设置两个Nginx负载均衡器,并利用虚拟IP地址(VIP)进行故障转移,确保了系统的连续性和稳定性。当主负载均衡器出现故障时,备用负载均衡器能够接管任务,从而实现高可用性。
1:使用Nginx做两个负载均衡器。其中一个作为备份
2:负载均衡器绑定一个虚拟IP。用户通过虚拟IP访问应用,由负载均衡器进行代理。如果其中一个应用节点失效,Nginx会从负载均衡器中去掉,等到再次可用时,再加入其中。
3:如果负载均衡器1宕机,负载均衡器2接手工作,通过有一个后台作业,不停运行监测,如果载均衡器1不可到达。就给本机添加同一虚拟IP。这样用户访问就被负载均衡器2接手工作,用户使用不需换IP,同样如果负载均衡器2宕机,相同过程被负载均衡器1接手工作
4:如果有域名,可以使用两个VIP,DNS绑定这两个VIP,如果其中一个负载均衡失效,其vip添加到一台负载均衡所在的机子。等失效负载均衡恢复以后,再添加到本机。
5:该方案可以保证任何点,都有一个备份。保持高可用

测试主机:192.168.20.14 和 193.168.20.16
虚拟IP:192.168.20.23

主机环境:
linux 2.6.9-78.EL
apache-tomcat-6.0.26
jdk1.6.0_20
nginx-0.7.67


添加VIP
Java代码 收藏代码
  1. /sbin/ifconfigeth0: 1 192.168 . 20.23 broadcast 192.168 . 20.255 netmask 255.255 . 255.0 up
/sbin/ifconfig eth0:1 192.168.20.23 broadcast 192.168.20.255 netmask 255.255.255.0 up


切换VIP shell脚本:
主负载均衡运行脚本:nginx_master_ha.sh

Java代码 收藏代码
  1. #!/bin/sh
  2. LANG=C
  3. date=$(date-d"today" + "%Y-%m-%d%H:%M:%S" )
  4. function_bind_vip(){
  5. /sbin/ifconfigeth0:1 192.168 . 20.23 broadcast 192.168 . 20.255 netmask 255.255 . 255.0 up
  6. }
  7. function_restart_nginx(){
  8. kill-USR1`cat/usr/local/nginx/logs/nginx.pid`
  9. }
  10. function_remove_vip(){
  11. /sbin/ifconfigeth0:1 192.168 . 20.23 broadcast 192.168 . 20.255 netmask 255.255 . 255.0 down
  12. }
  13. bind_vip="N" ;
  14. #主负载均衡。如果发现自己不可用,去掉绑定VIP,这时候备负载均衡会接管
  15. while true
  16. do
  17. httpcode_local_rip=`/usr/bin/curl-o/dev/null -s-w%{http_code}http: //192.168.20.15`
  18. if [ "x$httpcode_local_rip" == 'x200' ]
  19. then
  20. if [ "$bind_vip" == "N" ]
  21. then
  22. function_bind_vip
  23. function_restart_nginx
  24. bind_vip="Y"
  25. fi
  26. else
  27. if [ "$bind_vip" == "Y" ]
  28. then
  29. function_remove_vip
  30. bind_vip="N"
  31. fi
  32. fi
  33. sleep5
  34. done
#!/bin/sh
LANG=C
date=$(date -d "today" +"%Y-%m-%d %H:%M:%S")

function_bind_vip() {
	/sbin/ifconfig eth0:1 192.168.20.23 broadcast 192.168.20.255 netmask 255.255.255.0 up
}

function_restart_nginx() {
	kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
}

function_remove_vip() {
	/sbin/ifconfig eth0:1 192.168.20.23 broadcast 192.168.20.255 netmask 255.255.255.0 down
}

bind_vip="N";

#主负载均衡。如果发现自己不可用,去掉绑定VIP,这时候备负载均衡会接管
while true
do
	httpcode_local_rip=`/usr/bin/curl -o /dev/null -s -w %{http_code} http://192.168.20.15`
	
	if [ "x$httpcode_local_rip" == 'x200' ]
	then
		if [ "$bind_vip" == "N" ]
		then
			function_bind_vip
			function_restart_nginx
			bind_vip="Y"
		fi
	else
		if [ "$bind_vip" == "Y" ]
		then
			function_remove_vip
			bind_vip="N"
		fi
	fi
	
	sleep 5
done



Java代码 收藏代码
  1. 备负载均衡运行脚本:nginx_slave_ha.sh
备负载均衡运行脚本:nginx_slave_ha.sh

Java代码 收藏代码
  1. #!/bin/sh
  2. LANG=C
  3. date=$(date-d"today" + "%Y-%m-%d%H:%M:%S" )
  4. function_bind_vip(){
  5. /sbin/ifconfigeth0:1 192.168 . 20.23 broadcast 192.168 . 20.255 netmask 255.255 . 255.0 up
  6. }
  7. function_restart_nginx(){
  8. kill-USR1`cat/usr/local/nginx/logs/nginx.pid`
  9. }
  10. function_remove_vip(){
  11. /sbin/ifconfigeth0:1 192.168 . 20.23 broadcast 192.168 . 20.255 netmask 255.255 . 255.0 down
  12. }
  13. bind_vip="N" ;
  14. #备负载均衡。如果发现主负载均衡不可用,由自己接管,当主负载均衡可用时,交由主负载均衡接管
  15. while true
  16. do
  17. httpcode_remote_rip=`/usr/bin/curl-o/dev/null -s-w%{http_code}http: //192.168.20.15`
  18. if [ "x$httpcode_remote_rip" != 'x200' ]
  19. then
  20. if [ "$bind_vip" == "N" ]
  21. then
  22. function_bind_vip
  23. function_restart_nginx
  24. bind_vip="Y"
  25. fi
  26. else
  27. if [ "$bind_vip" == "Y" ]
  28. then
  29. function_remove_vip
  30. bind_vip="N"
  31. fi
  32. fi
  33. sleep5
  34. done
#!/bin/sh
LANG=C
date=$(date -d "today" +"%Y-%m-%d %H:%M:%S")

function_bind_vip() {
	/sbin/ifconfig eth0:1 192.168.20.23 broadcast 192.168.20.255 netmask 255.255.255.0 up
}

function_restart_nginx() {
	kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
}

function_remove_vip() {
	/sbin/ifconfig eth0:1 192.168.20.23 broadcast 192.168.20.255 netmask 255.255.255.0 down
}

bind_vip="N";

#备负载均衡。如果发现主负载均衡不可用,由自己接管,当主负载均衡可用时,交由主负载均衡接管
while true
do
	httpcode_remote_rip=`/usr/bin/curl -o /dev/null -s -w %{http_code} http://192.168.20.15`
	
	if [ "x$httpcode_remote_rip" != 'x200' ]
	then
		if [ "$bind_vip" == "N"]
		then
			function_bind_vip
			function_restart_nginx
			bind_vip="Y"
		fi
	else
		if [ "$bind_vip" == "Y" ]
		then
			function_remove_vip
			bind_vip="N"
		fi
	fi
	
	sleep 5
done


ngnix.conf配置:

Java代码 收藏代码
  1. #usernobody;
  2. worker_processes2 ;
  3. #error_loglogs/error.log;
  4. #error_loglogs/error.lognotice;
  5. #error_loglogs/error.loginfo;
  6. #pidlogs/nginx.pid;
  7. worker_rlimit_nofile1024 ;
  8. events{
  9. useepoll;
  10. worker_connections1024 ;
  11. }
  12. http{
  13. includemime.types;
  14. default_typeapplication/octet-stream;
  15. #log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
  16. #'$status$body_bytes_sent"$http_referer"'
  17. #'"$http_user_agent""$http_x_forwarded_for"' ;
  18. #access_loglogs/access.logmain;
  19. sendfileon;
  20. tcp_nopushon;
  21. #keepalive_timeout0 ;
  22. keepalive_timeout65 ;
  23. #gzipon;
  24. upstreambackend{
  25. server192.168 . 20.14 : 8080 weight= 10 ;
  26. server192.168 . 20.16 : 8080 weight= 10 ;
  27. }
  28. server{
  29. listen80 ;
  30. server_name192.168 . 20.14 ;
  31. #charsetkoi8-r;
  32. #access_loglogs/host.access.logmain;
  33. location/{
  34. roothtml;
  35. indexindex.jspindex.htmlindex.htm;
  36. proxy_set_headerHost$host;
  37. proxy_set_headerX-Real-IP$remote_addr;
  38. proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
  39. proxy_passhttp://backend;
  40. }
  41. #error_page404 / 404 .html;
  42. #redirectservererrorpagestothestatic page/50x.html
  43. #
  44. error_page500 502 503 504 /50x.html;
  45. location=/50x.html{
  46. roothtml;
  47. }
  48. }
  49. }
#user  nobody;
worker_processes  2;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

worker_rlimit_nofile 1024;
events {
    use epoll;
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream backend { 
	server 192.168.20.14:8080 weight=10; 
	server 192.168.20.16:8080 weight=10; 
    } 
    server {
        listen       80;
        server_name  192.168.20.14;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.jsp index.html index.htm;
	    proxy_set_header       Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass  http://backend; 
        }


        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
    }



}



  • 大小: 22.9 KB
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值