Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1730422
  • 博文数量: 234
  • 博客积分: 4966
  • 博客等级: 上校
  • 技术积分: 3322
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-13 01:03
文章分类

全部博文(234)

文章存档

2017年(2)

2016年(1)

2015年(8)

2014年(11)

2013年(44)

2012年(27)

2011年(22)

2010年(30)

2009年(37)

2008年(6)

2007年(45)

2006年(1)

分类: 系统运维

2012-05-14 22:24:55

实验环境:
vmware guest: ha1 ha2
system: debian 6.0.5
ip:
ha1  eth0 192.168.121.133 (Nginx_MASTER)  eth0:0 192.168.121.134  (Apache Web_1 Real Server)
ha2  eth0 192.168.121.135 (Nginx_BACKUP)  eth0:0 192.168.121.136  (Apache Web_2 Real Server)
     Nginx_VIP_IP: 192.168.121.121
实验要求:测试keepalived高可用,测试Nginx负载均衡,测试Nginx中ip_hash功能

1.基本软件安装(因只功能测试,全部采用aptitude安装)
aptitude install nginx apache2 keepalived
2.基本配置
在ha1中配置,nginx默认采用80端口,apache采用8080端口,且/var/www目录中新建index.htm和index.html文件,分别进行标识,ha2中依此配置,测试各应用配置成功
root@ha1:~# cat /var/www/index.htm

        This is Ha1 Apache Server!
        192.168.121.134

root@ha1:~# cat /var/www/index.html

        This is Ha1 Nginx Server!
        192.168.121.133

3.配置keepalived高可用
ha1的keepalived配置文件如下

点击(此处)折叠或打开

  1. ! Configuration File for keepalived

  2. global_defs {
  3. notification_email {
  4. acassen
  5. }
  6. notification_email_from edgeman_03@163.com
  7. smtp_server smtp.163.com
  8. smtp_connect_timeout 30
  9. router_id LVS_DEVEL
  10. }

  11. #vrrp_script chk_nginx {
  12. # script "/tmp/check_http.sh"
  13. # interval 2
  14. # weight 2
  15. # }

  16. vrrp_instance NGINX {
  17. state MASTER
  18. interface eth0
  19. virtual_router_id 50
  20. mcast_src_ip 121.168.121.133
  21. priority 100
  22. advert_int 1
  23. authentication {
  24. auth_type PASS
  25. auth_pass 820411
  26. }
  27. # track_script {
  28. # chk_nginx
  29. # }
  30. virtual_ipaddress {
  31. 192.168.121.121
  32. }
  33. }
ha2的配置文件如下

点击(此处)折叠或打开

  1. ! Configuration File for keepalived

  2. global_defs {
  3. notification_email {
  4. acassen
  5. }
  6. notification_email_from edgeman_03@163.com
  7. smtp_server smtp.163.com
  8. smtp_connect_timeout 30
  9. router_id LVS_DEVEL
  10. }

  11. #vrrp_script chk_nginx {
  12. # script "/tmp/check_http.sh"
  13. # interval 2
  14. # weight 2
  15. # }

  16. vrrp_instance NGINX {
  17. state BACKUP
  18. interface eth0
  19. virtual_router_id 50
  20. mcast_src_ip 192.168.121.135
  21. priority 99
  22. advert_int 1
  23. authentication {
  24. auth_type PASS
  25. auth_pass 820411
  26. }
  27. # track_script {
  28. # chk_nginx
  29. # }
  30. virtual_ipaddress {
  31. 192.168.121.121
  32. }
  33. }
配置成功后效果应该是访问会跳转到ha1的nginx的页面,如果ha1关机或者ha1上的keepliaved务停止就转到ha2,但如果此时如果是ha1上的nginx服务停止,则不会提供web服务,于我们的要求不符,继续向下配置
4.配置nginx负载均衡
ha1,ha2上的nginx部分配置文件

点击(此处)折叠或打开

  1. upstream backend
  2. {
  3. ip_hash;
  4. server 192.168.121.134:8080;
  5. server 192.168.121.136:8080;
  6. }

  7. server {

  8. listen 80;
  9. server_name localhost;
  10. access_log /var/log/nginx/localhost.access.log;

  11. location / {
  12. # root /var/www;
  13. # index index.html;
  14. proxy_pass
  15. }
按这个配置好后,此时访问的效果是出现ha1上apache的页面,此时就算停止nginx服务,也同样会有web服务提供,但出现的页面是ha2上apache的页面,大家可以进行其它如关机或者停止keepalived服务等测试,各会有相应测试结果
5.测试nginx中的ip_hash效果
在第4步中如果服务都正常且有应用ip_hash时,多次刷新页面都是会出现ha1上的apache的页面,但此时如果注释掉 ip_hash并重启服务,再刷新页面,则是ha1和ha2上的apache页面交替出现,此时相信大家明白ip_hash是做什么用的了吧



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