Maison  >  Article  >  développement back-end  >  nginx+keepalived 高可用负载均衡

nginx+keepalived 高可用负载均衡

WBOY
WBOYoriginal
2016-08-08 09:27:521098parcourir

话就不多说了,nginx安装与配置,还有负载均衡呢,可以看我写的另一篇文章《nginx负载均衡实战》,还有关于负载均衡呢,大家可以看一下我写的另外两篇文章,一个是《lvs+keepalived负载均衡》,另一个是《haproxy+keepalived负载均衡》,三种负载均衡的区别呢,可以看一下我转载的一篇文章《软件级负载均衡器(LVS/HAProxy/Nginx)的特点简介和对比》,下面直接进入配置步骤:

1.系统环境

[plain] view plaincopy

  1. 系统版本:CentOS release 5.9 (Final) x86 32位      
  2. nginx版本:   1.2.8    
  3. keepalived版本:    1.2.4  
  4.   
  5. 主keepalived:192.168.207.130  
  6.   
  7. 从keepalived:192.168.207.131  
  8.   
  9.   
  10. VIP:192.168.207.140   
  11. WEB_1:192.168.207.129 80端口   
  12. WEB_2:192.168.207.130 8080端口   
  13. WEB_3:192.168.207.131 8080端口  

2.自定义nginx配置文件


在192.168.207.130和192.168.207.131上操作

[plain] view plaincopy

  1. useradd nginx  
  2. vi /usr/local/nginx/conf/nginx.conf  

内容如下:

[plain] view plaincopy

  1. #运行用户      
  2. user nginx nginx;      
  3. #启动进程      
  4. worker_processes 2;      
  5. #全局错误日志及PID文件      
  6. error_log logs/error.log notice;      
  7. pid logs/nginx.pid;      
  8. #工作模式及每个进程连接数上限      
  9. events {      
  10.     use epoll;      
  11.     worker_connections 1024;     #所以nginx支持的总连接数就等于worker_processes * worker_connections    
  12. }      
  13. #设定http服务器,利用它的反向代理功能提供负载均衡支持      
  14. http {      
  15.     #设定mime类型      
  16.     include mime.types;  #这个是说nginx支持哪些多媒体类型,可以到conf/mime.types查看支持哪些多媒体    
  17.     default_type application/octet-stream;   #默认的数据类型     
  18.     #设定日志格式      
  19.     
  20.     log_format main '$remote_addr - $remote_user [$time_local] '     
  21.     '"$request" $status $bytes_sent '     
  22.     '"$http_referer" "$http_user_agent" '     
  23.     '"$gzip_ratio"';      
  24.     
  25.     log_format download '$remote_addr - $remote_user [$time_local] '     
  26.     '"$request" $status $bytes_sent '     
  27.     '"$http_referer" "$http_user_agent" '     
  28.     '"$http_range" "$sent_http_content_range"';      
  29.     #设定请求缓冲      
  30.     client_header_buffer_size 1k;      
  31.     large_client_header_buffers 4 4k;      
  32.     #开启gzip模块      
  33.     #gzip on;      
  34.     #gzip_min_length 1100;      
  35.     #gzip_buffers 4 8k;      
  36.     #gzip_types text/plain;      
  37.     #output_buffers 1 32k;      
  38.     #postpone_output 1460;      
  39.     #设定access log      
  40.     access_log logs/access.log main;      
  41.     client_header_timeout 3m;      
  42.     client_body_timeout 3m;      
  43.     send_timeout 3m;      
  44.     sendfile on;      
  45.     tcp_nopush on;      
  46.     tcp_nodelay on;      
  47.     keepalive_timeout 65;      
  48.     #设定负载均衡的服务器列表      
  49.     
  50.     upstream mysvr {      
  51.         #weigth参数表示权值,权值越高被分配到的几率越大     
  52.         server 192.168.207.129:80 weight=5;      
  53.         server 192.168.207.130:8080 weight=5;      
  54.         server 192.168.207.131:8080 weight=5;    
  55.     }      
  56.     server { #这个是设置web服务的,监听8080端口    
  57.         listen        8080;    
  58.         server_name    192.168.207.131;          #这个根据系统ip变化  
  59.         index     index.html index.htm;    
  60.         root        /var/www/html;    
  61.         #error_page     500 502 503 504    /50x.html;    
  62.         #location = /50x.html {    
  63.         #    root     html;    
  64.         #}    
  65.         }     
  66.     #设定虚拟主机      
  67.     server {      
  68.         listen 80;      
  69.         server_name 192.168.207.140;                   #这里是VIP  
  70.         #charset gb2312;      
  71.         #设定本虚拟主机的访问日志      
  72.         access_log logs/three.web.access.log main;      
  73.         #如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid      
  74.         #如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好      
  75.         #location ~ ^/(img|js|css)/{      
  76.         #   root /data3/Html;      
  77.         #   expires 24h;    
  78.         #}     
  79.             #对 "/" 启用负载均衡      
  80.         location / {      
  81.             proxy_pass http://mysvr;  #以这种格式来使用后端的web服务器    
  82.             proxy_redirect off;      
  83.             proxy_set_header Host $host;      
  84.             proxy_set_header X-Real-IP $remote_addr;      
  85.             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;      
  86.             client_max_body_size 10m;      
  87.             client_body_buffer_size 128k;      
  88.             proxy_connect_timeout 90;      
  89.             proxy_send_timeout 90;      
  90.             proxy_read_timeout 90;      
  91.             proxy_buffer_size 4k;      
  92.             proxy_buffers 4 32k;      
  93.             proxy_busy_buffers_size 64k;      
  94.             proxy_temp_file_write_size 64k;    
  95.         }      
  96.         #设定查看Nginx状态的地址 ,在安装时要加上--with-http_stub_status_module参数    
  97.         location /NginxStatus {      
  98.             stub_status on;      
  99.             access_log on;      
  100.             auth_basic "NginxStatus";      
  101.             auth_basic_user_file conf/htpasswd;     #设置访问密码,htpasswd -bc filename username password    
  102.         }    
  103.     }    
  104. }     

3.自定义keepalived配置文件

[plain] view plaincopy

  1. vi /etc/keepalived/keepalived.conf  

内容如下:

[plain] view plaincopy

  1. global_defs {  
  2.    notification_email {  
  3.         root@localhost.localdomain  
  4.    }  
  5.    notification_email_from notify@keepalived.com  
  6.    smtp_server 127.0.0.1  
  7.    smtp_connect_timeout 30  
  8.    router_id LVS_DEVEL  
  9. }  
  10.   
  11. vrrp_script chk_http_port {  
  12.                 script "/etc/keepalived/check_nginx.sh"         ###监控脚本  
  13.                 interval 2                             ###监控时间  
  14.                 weight 2                                ###目前搞不清楚  
  15. }  
  16. vrrp_instance VI_1 {  
  17.         state MASTER                            ### 设置为 主  
  18.         interface eth0                             ### 监控网卡  
  19.         virtual_router_id 51                    ### 这个两台服务器必须一样  
  20.         priority 101                                 ### 权重值 MASTRE 一定要高于 BAUCKUP  
  21.         authentication {  
  22.                      auth_type PASS  
  23.                      auth_pass 1111  
  24.         }  
  25.         track_script {  
  26.                 chk_http_port                     ### 执行监控的服务  
  27.         }  
  28.         virtual_ipaddress {  
  29.              192.168.207.140                            ###    VIP 地址  
  30.         }  
  31. }  

4.写自定义脚本

[plain] view plaincopy

  1. vi /etc/keepalived/check_nginx.sh  

内容如下:

[plain] view plaincopy

  1. !/bin/bash  
  2. A=`ps -C nginx --no-header |wc -l`                ## 查看是否有 nginx进程 把值赋给变量A  
  3. if [ $A -eq 0 ];then                                         ## 如果没有进程值得为 零  
  4.         /usr/local/nginx/sbin/nginx  
  5.         sleep 3  
  6.         if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then  
  7.                   /etc/init.d/keepalived stop                       ## 则结束 keepalived 进程  
  8.         fi  
  9. fi  

这里是检查nginx是否启动好,如果没有启动,先启动 nginx,隔了3秒后还没有启动好,则将keepalived进程也关闭,这样从keepalived就能接手过去了,提供高可用性,在这里呢,keepalived服务是提供高可用性,而nginx是提供后端web服务器的负载均衡。
这里还要给脚本加上执行权限,如下

[plain] view plaincopy

  1. chmod +x /etc/keepalived/check_nginx.sh  

5.启动服务,并测试


在这里先说一下啊,在WEB_1上,我是使用系统自带的apache昨晚web服务器的,比较省事,这样呢,我只要启动好主从keepalived就ok了,因为它会利用check_nginx.sh脚本来自动启动nginx。
都启动好了。
访问http://192.168.207.140就可以轮训访问后端的三台web服务器内容啦
这里我们把主keepalived服务给关掉,来测试高可用性
然后会在从keepalived服务器上的/var/log/messages看到这样的日志

[plain] view plaincopy

  1. Apr 19 17:42:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE  
  2. Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE  
  3. Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.  
  4. Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140  
  5. Apr 19 17:42:45 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 added  
  6. Apr 19 17:42:45 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.207.140 added  
  7. Apr 19 17:42:45 localhost avahi-daemon[4204]: Registering new address record for 192.168.207.140 on eth0.  

继续访问http://192.168.207.140,依旧可以访问后端的三台web服务器
然后再把原主keepalived打开,可以观察到原从keepalived服务器的日志显示

[plain] view plaincopy

  1. Apr 19 17:42:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140  
  2. Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert  
  3. Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE  
  4. Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.  
  5. Apr 19 17:44:06 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 removed  
  6. Apr 19 17:44:06 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.207.140 removed  
  7. Apr 19 17:44:06 localhost avahi-daemon[4204]: Withdrawing address record for 192.168.207.140 on eth0.  

说明有恢复了原来的主从结果。
生产环境中,后端的机器也可能会挂掉,但是呢,这就不用你操心啦,nginx会自动把session分配到好的后端web服务器上的啦

ok,到这里全部结束了,实践亲测,祝君成功

From: http://blog.csdn.net/zmj_88888888/article/details/8825471

以上就介绍了nginx+keepalived 高可用负载均衡,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn