Chinaunix首页 | 论坛 | 博客
  • 博客访问: 351886
  • 博文数量: 26
  • 博客积分: 495
  • 博客等级: 下士
  • 技术积分: 562
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-26 13:50
文章分类

全部博文(26)

文章存档

2015年(9)

2014年(6)

2013年(7)

2012年(2)

2011年(2)

分类: 系统运维

2014-11-26 10:43:21

负载均衡是个老生常谈的话题,之前很早就接触过LVS+keepalived ,对其原理也仔细学习过,这次温故而知新,巩固一下这方面的知识,本次试验在ESXI 上面完成,试验环境如下:
LB1(NGINX + keepalived)  //nginx 为前端负载调度器 ,keepalived 实现HA 为MASTER
LB2(NGINX + keepalived)  //nginx 为前端负载调度器 ,keepalived 实现HA 为BACKUP
NODE1   //REAL SERVER
NODE2   //REAL SERVER
192.168.1.241,192.168.1.242 // VIP,现在一般都是互为主备,资源合理利用,例如一些多线机房,不同的ip线路

安装步骤
1.MASTER 安装
#yum install keepalived    //现在直接用yum 安装
#yum install nginx  
#vim /etc/keepalived/keepalived.conf
内容如下:

点击(此处)折叠或打开

  1. ! Configuration File for keepalived

  2. global_defs {
  3.    router_id LVS_DEVEL
  4. }

  5. vrrp_script chk_http_port {
  6.     script "/usr/bin/killall -0 nginx"
  7.     interval 2
  8.     weight 2
  9. }

  10. vrrp_instance VI_1 {
  11.     state MASTER
  12.     interface eth0
  13.     virtual_router_id 51
  14.     priority 101    //备份机 优先级数字要比这个小
  15.     advert_int 5
  16.     authentication {
  17.         auth_type PASS
  18.         auth_pass 1111
  19.     }
  20.     track_script {
  21.         chk_http_port
  22.     }
  23.     virtual_ipaddress {
  24.         192.168.1.241
  25.     }
  26. }

  27. vrrp_instance VI_2 {
  28.     state BACKUP
  29.     interface eth0
  30.     virtual_router_id 52
  31.     priority 100
  32.     advert_int 5
  33.     authentication {
  34.         auth_type PASS
  35.         auth_pass 1111
  36.     }
  37.     track_script {
  38.         chk_http_port
  39.     }

  40.     virtual_ipaddress {
  41.         192.168.1.242
  42.     }
  43. }
编辑nginx配置文件,内容如下

点击(此处)折叠或打开

  1. user nobody;
  2. worker_processes 2;

  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;

  6. #pid logs/nginx.pid;


  7. events {
  8.     worker_connections 65535;
  9. }


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

  13.     #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14.     # '$status $body_bytes_sent "$http_referer" '
  15.     # '"$http_user_agent" "$http_x_forwarded_for"';

  16.     #access_log logs/access.log main;
  17.     sendfile on;
  18.     #tcp_nopush on;
  19.     #keepalive_timeout 0;
  20.     keepalive_timeout 65;
  21.     #proxy_cache_path /app/nginx/cache keys_zone=one:10m inactive=1d max_size=1g;
  22.     #proxy_cache_key "$host$request_uri$cookie_user";
  23.     #proxy_cache_min_uses 5;
  24.     #proxy_cache_valid 200 302 10m;
  25.     #proxy_cache_valid 404 1m;

  26.     upstream backend {
  27.         server 192.168.1.64 weight=5;
  28.         server 192.168.1.139 weight=15;
  29.         server 192.168.1.70 backup;
  30.     }

  31.     include vhosts/*.conf;
  32. }
default 虚拟机配置如下

点击(此处)折叠或打开

  1. server {
  2.         listen 80;
  3.         root /app/nginx/html/;
  4.         access_log /app/nginx/logs/test.log ;
  5.         error_log /app/nginx/logs/error.log;
  6.         index index.html;

  7.         location ~ \.php$ {
  8.                 fastcgi_pass 127.0.0.1:9000;
  9.                 fastcgi_index index.php;
  10.                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  11.                 include fastcgi_params;
  12.         }

  13.         location /status {
  14.                 stub_status on;
  15.                 access_log /app/nginx/logs/status.log;
  16.                 auth_basic "mypass";
  17.                 allow 127.0.0.1;
  18.                 deny all;
  19.         }

  20.         #这段是我编译安装的淘宝开发的footer 模块,当时为了方便看测试结果
  21.         #location /test {
  22.         #        #footer "";
  23.         #        #footer "/* host: $server_name - $date_local */";
  24.         #        footer "/* host: $hostname - $date_local */";
  25.         #}

  26.         location / {
  27.                 #proxy_redirect off;
  28.                 proxy_set_header Host $host;
  29.                 proxy_set_header X-Real-IP $remote_addr;
  30.                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  31.                 proxy_pass http://backend;
  32.         }
  33. }
BACKUP 安装雷同,只需要注意keepalived的优先级,测试过程很简单,注意查看/var/log/messages ,看虚拟ip 的浮动情况

总结:我只用keepalived的VRRP 协议机制保障服务的可用性,real server 健康检查机制由nginx自己完成,具体请参考





阅读(2054) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~