六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 451|回复: 0

nginx+keepalived实现nginx双主高可用的负载均衡

[复制链接]
 楼主| 发表于 2014-5-20 18:22:14 | 显示全部楼层 |阅读模式
nginx+keepalived实现nginx双主高可用的负载均衡
一、前言:
在互联网上面,网站为用户提供原始的内容访问,同时为用户提供交互操作。提供稳定可靠的服务,可以给用户带来良好的体验,保证用户的正常访问使用,在网站的可靠性方面,有很多的技术可以来提供,这类技术可以分为:
高可用:保证服务的可靠,稳定,实现故障的屏蔽,避免了单点故障。
高性能:多台服务器连接起来,处理一个复杂的计算问题。
负载均衡:将用户请求引导到后端多台服务器,实现服务器请求的负载。
我们将这类技术称之为集群负载均衡,可以提供负载均衡和高可用的有硬件和软件,软件方面有haproxy,lvs,keepalived,nginx,heartbeat,corosync等等,而这里我们采用的是nginx-keepalived来构建。
Nginx有很强的代理功能,但是一台nginx 就形成了单点,现在使用keepalived来解决这个问题,keepalived可以实现故障转移切换,实现后端的健康检查,前端的高可用,使网站故障记录大大降低,避免了单点故障造成网站无法访问的问题,确保了网站业务的正常运行。
二、Nginx+keepalived有两种配置方案:
2.1Nginx+keepalived 主从配置
这种方案,使用一个vip地址,前端使用2台机器,一台做主,一台做备,但同时只有一台机器工作,另一台备份机器在主机器不出现故障的时候,永远处于浪费状态,对于服务器不多的网站,该方案不经济实惠,所以本次不予采用。
2.2Nginx+keepalived 双主配置
这种方案,使用两个vip地址,前端使用2台机器,互为主备,同时有两台机器工作,当其中一台机器出现故障,两台机器的请求转移到一台机器负担,非常适合于当前架构环境,故本次采用此方案对网站进行高可用架构。
三、Nginx+keepalived 主从配置
3.1Nginx+keepalived 主从配置详情请见http://kling.blog.51cto.com/3320545/1240359
这里不做重点介绍。
四、Ningx+Keepalived 双主配置
4.1、拓扑结构

4.2、测试环境如下:
系统:Ceentos 6.4 64位
前端node1服务器:
DIP: 192.168.122.2
VIP: 192.168.122.22
前端node2服务器:
DIP: 192.168.122.3
VIP:192.168.122.23
后端服务器:
web server01:192.168.122.4
web server02:192.168.122.5
web server03:192.168.122.6
4.3、软件安装
分别在两台前端服务器上安装nginx+keepalived,使用脚本如下:
  1. #!/bin/bash
  2. # author: kuangl
  3. # mail: kuangl@orient-media.com
  4. # description: The installation of Nginx files.
  5. # -------------------------------------------------------- #
  6.          ## Nginx_install
  7. # -------------------------------------------------------- #
  8. # Nginx installation
  9. #CURRENT_PATH=$(pwd)
  10. for
  11. i in $(rpm -q gcc gcc-c++ kernel-devel openssl-devel zlib-devel
  12. popt-devel popt-static libnl-devel wget make |grep 'not installed' | awk
  13. '{print $2}')
  14. do
  15.     yum -y install $i
  16. done
  17. [ -d /root/software ]
  18. [ "$?" != 0 ] && mkdir /root/software
  19. cd /root/software
  20. [ !  -e pcre-8.33.tar.gz ] && wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
  21. tar -zxvf pcre-8.33.tar.gz
  22. cd pcre-8.33
  23. ./configure
  24. make && make install
  25. echo $? || [ $? != 0  ] || echo  " installation pcre  failed" || exit 1
  26. cd /root/software
  27. [ ! -e nginx-1.2.9.tar.gz ] && wget http://nginx.org/download/nginx-1.2.9.tar.gz
  28. tar -zxvf nginx-1.2.9.tar.gz
  29. cd nginx-1.2.9
  30. ./configure
  31.   --prefix=/usr/local/nginx --with-http_ssl_module
  32. --with-http_sub_module --with-http_stub_status_module  
  33. --with-http_gzip_static_module
  34. make && make install
  35. echo $? || [ $? != 0  ] || echo  " installation  nginx  failed" || exit 1
  36. # -------------------------------------------------------- #
  37.             ## Keepalived_intsall
  38. # -------------------------------------------------------- #
  39. # Keepalived installation
  40. cd /root/softwarae
  41. [ ! -e keepalived-1.2.4.tar.gz ] &&  wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gz
  42. tar -zxvf keepalived-1.2.4.tar.gz
  43. cd keepalived-1.2.4
  44. ln -s /usr/src/kernels/$(uname -r) /usr/src/kernels/linux
  45. ./configure
  46. --prefix=/usr  --bindir=/usr/bin  --sbindir=/usr/bin  
  47. --libexecdir=/usr/libexec --localstatedir=/var --libdir=/lib64  
  48. --infodir=/usr/share/info  --sysconfdir=/etc
  49. --mandir=/usr/local/share/man   --with-kernel-dir=/usr/src/kernels/linux
  50. make && make install
  51. echo $? || [ $? != 0  ] || print " installation keepalived  failed" || exit 1
  52. chkconfig --add keepalived
  53. chkconfig --level 345 keepalived on
复制代码
4.4、在后端服务器上安装apached
     后端node4
  1. [root@node4 ~]# yum  -y install httpd
  2. [root@node4 html]# echo "this is 192.168.122.4" > /var/www/htmlindex.html
  3. [root@node4 ~]# service httpd start
  4. [root@node4 html]# curl  192.168.122.4
  5. this is 192.168.122.4
复制代码
后端node5
  1. [root@node5 ~]# yum  -y install httpd
  2. [root@node5 html]# echo "this is 192.168.122.5" > /var/www/htmlindex.html
  3. [root@node5 ~]# service httpd start
  4. [root@node5 html]# curl  192.168.122.5
  5. this is 192.168.122.5
复制代码
后端node6
  1. [root@node6 ~]# yum  -y install httpd
  2. [root@node6 html]# echo "this is 192.168.122.6" > /var/www/htmlindex.html
  3. [root@node6 ~]# service httpd start
  4. [root@node6 html]# curl  192.168.122.6
  5. this is 192.168.122.6
复制代码
4.5、node2、node3上配置nginx
  1. [root@node2 ~]# vim /usr/local/nginx/conf/nginx.conf
  2. upstream web1       ##定义负载均衡组为web1
  3.     {
  4.         ip_hash;
  5.         server 192.168.122.4:80;
  6.         server 192.168.122.5:80;
  7.         server 192.168.122.6:80;
  8.     }
  9. server {
  10.         listen       80;
  11.         server_name  dev.test01.com;
  12.         location /
  13.         {
  14.         root /home/kuangl/;
  15.         index index.html index.htm;
  16.         proxy_set_header Host $host;
  17.         proxy_set_header X-Forwarded-For $remote_addr;
  18.         proxy_pass http://web1;
  19.         }
  20.       }
复制代码
4.6、在node2上配置keepalived
  1. [root@node2 conf]# vim /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4.    notification_email {
  5.      404060945@qq.com
  6.    }
  7.    notification_email_from root@localhost
  8.    smtp_server 127.0.0.1
  9.    smtp_connect_timeout 30
  10.    router_id LVS_DEVEL
  11. }
  12. vrrp_script chk_haproxy {
  13.     script "/etc/keepalived/chk_nginx.sh"
  14.     interval 2
  15.     weight 2
  16. }
  17. vrrp_instance VI_1 {
  18.     state MASTER
  19.     interface eth0
  20.     virtual_router_id 200
  21.     priority 250
  22.     advert_int 1
  23.     authentication {
  24.         auth_type PASS
  25.         auth_pass kuanglnginx
  26.     }
  27.    track_script {
  28.         chk_nginx
  29.     }
  30.     virtual_ipaddress {
  31.         192.168.122.22
  32.     }
  33. }
  34. vrrp_instance VI_2 {
  35.     state BACKUP
  36.     interface eth0
  37.     virtual_router_id 251
  38.     priority 100
  39.     advert_int 1
  40.     authentication {
  41.         auth_type PASS
  42.         auth_pass kuangl
  43.     }
  44.     track_script {
  45.         chk_nginx
  46.     }
  47.     virtual_ipaddress {
  48.         192.168.122.23
  49.     }
  50. }
复制代码
4.7、在node3上配置keepalived
  1. ! Configuration File for keepalived
  2. global_defs {
  3.    notification_email {
  4.      404060945@qq.com
  5.    }
  6.    notification_email_from root@localhost
  7.    smtp_server 127.0.0.1
  8.    smtp_connect_timeout 30
  9.    router_id LVS_DEVEL
  10. }
  11. vrrp_script chk_haproxy {
  12.     script "/etc/keepalived/chk_nginx.sh"
  13.     interval 2
  14.     weight 2
  15. }
  16. vrrp_instance VI_1 {
  17.     state BACKUP
  18.     interface eth0
  19.     virtual_router_id 200
  20.     priority 100
  21.     advert_int 1
  22.     authentication {
  23.         auth_type PASS
  24.         auth_pass kuanglnginx
  25.     }
  26.     track_script {
  27.         chk_nginx
  28.     }
  29.     virtual_ipaddress {
  30.         192.168.122.22
  31.     }
  32. }
  33. vrrp_instance VI_2 {
  34.     state MASTER
  35.     interface eth0
  36.     virtual_router_id 251
  37.     priority 250
  38.     advert_int 1
  39.     authentication {
  40.         auth_type PASS
  41.         auth_pass kuangl
  42.     }
  43.     track_script {
  44.         chk_nginx
  45.     }
  46.     virtual_ipaddress {
  47.         192.168.122.23
  48.     }
  49. }
复制代码
4.8、在两台双主服务器上添加自动检测脚本
  1. #!/bin/bash
  2. # description:
  3. # 定时查看nginx是否存在,如果不存在则启动nginx
  4. # 如果启动失败,则停止keepalived
  5. status=$(ps -C nginx --no-heading|wc -l)
  6. if [ "${status}" = "0" ]; then
  7.         /usr/local/nginx/sbin/nginx
  8.         status2=$(ps -C nginx --no-heading|wc -l)
  9.         if [ "${status2}" = "0"  ]; then
  10.                 /etc/init.d/keepalived stop
  11.         fi
  12. fi
复制代码
4.9、开启nginx、keepalived服务
  1. [root@node2 ~]# service keepalived start
  2. [root@node2 ~]# /usr/local/nginx/sbin/nginx
  3. [root@node3 ~]# service keepalived start
  4. [root@node3 ~]# /usr/local/nginx/sbin/nginx
复制代码
4.10、用 ip a 查看VIP


4.11、测试访问
  1. [kuangl@node01 ~]$ curl http://192.168.122.22
  2. this is 192.168.122.6
  3. [kuangl@node01 ~]$ curl http://192.168.122.22
  4. this is 192.168.122.4
  5. [kuangl@node01 ~]$ curl http://192.168.122.22
  6. this is 192.168.122.5
  7. [kuangl@node01 ~]$ curl http://192.168.122.23
  8. this is 192.168.122.6
  9. [kuangl@node01 ~]$ curl http://192.168.122.23
  10. this is 192.168.122.4
  11. [kuangl@node01 ~]$ curl http://192.168.122.23
  12. this is 192.168.122.5
复制代码
五、后端用rsync做数据同步

   node5-node6上配置进程模式,以node5为例
  1. [root@node5 ~]# yum -y install rsync
  2. [root@node5 ~]# vim /etc/rsynsd.conf
  3. uid = root
  4. gid = root
  5. use chroot = no
  6. max connections = 5
  7. pid file = /var/run/rsyncd.pid
  8. lock file = /var/run/rsync.lock
  9. log file = /var/log/rsyncd.log
  10. [web01]                     
  11. path=/home/kuangl/         
  12. comment = update         
  13. ignore errors              
  14. read only = no            
  15. list = no                 
  16. hosts allow = 192.168.122.0/24
  17. auth users = root        
  18. uid = root
  19. gid = root
  20. secrets file = /etc/rsyncd.secrets
  21. [root@node5 ~]# vim /etc/rsyncd.secrets
  22. root:123456
  23. [root@node5 ~]# chmod 0600 /etc/rsyncd.secrets
  24. [root@node5 ~]# ll /etc/rsyncd.secrets
  25. -rw-------. 1 root root 12 Jul 20 19:41 /etc/rsyncd.secrets
  26. [root@node5 ~]# rsync --daemon
  27. [root@node5 ~]# echo "rsync --daemon" >> /etc/rc.local
复制代码
node4上配置命令模式:
  1. [root@node4 ~]# yum -y install rsync
  2. [root@node4 ~]# vim /etc/rsyncd.secrets
  3. 123456
  4. [root@node4 ~]# chmod 0600 /etc/rsyncd.secrets
  5. root@node4
  6. kuangl]# rsync -vzrtopg --delete --progress
  7. --password-file=/etc/rsyncd.secrets  rsync+inotify
  8. root@192.168.122.5::web01
  9. sending incremental file list
  10. rsync+inotify/
  11. rsync+inotify/inotify-tools-3.14.tar.gz
  12.       358772 100%    1.85MB/s    0:00:00 (xfer#1, to-check=2/4)
  13. rsync+inotify/rsync+inotify_client.sh
  14.          617 100%    3.11kB/s    0:00:00 (xfer#2, to-check=1/4)
  15. rsync+inotify/rsync+inotify_server.sh
  16.          900 100%    4.03kB/s    0:00:00 (xfer#3, to-check=0/4)
  17. sent 360679 bytes  received 69 bytes  240498.67 bytes/sec
  18. total size is 360289  speedup is 1.00
复制代码
查看结果
  1. [root@node5 ~]# cd /home/kuangl/
  2. [root@node5 kuangl]# ll
  3. total 8
  4. -rw-r--r--. 1 root root   22 Jul 20 15:16 index.html
  5. drwxr-xr-x. 2 root root 4096 Nov 11  2012 rsync+inotify
复制代码
本文出自 “&思远晨曦” 博客,请务必保留此出处http://kling.blog.51cto.com/3320545/1253474
















nginx+keepalived实现nginx双主高可用的负载均衡


该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表