IT技术互动交流平台

CenOS5 x86下配置Nginx+keepalived主从负载均衡

作者:关鸣  发布日期:2013-11-29 09:19:15

一、环境:
centos5.8x86_64、nginx-0.8.46、keepalived-1.1.19
主nginx负载均衡器:192.168.1.122
辅nginx负载均衡器:192.168.1.123
vip:192.168.1.128
两台Apache Web服务器


二、安装keepalived
cd/usr/local/src
wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz
tar zxvfkeepalived-1.1.19.tar.gz
cdkeepalived-1.1.19
./configure--prefix=/usr/local/keepalived
make
makeinstall


以下三行是为了将keepalived做成服务启动方式,方便管理
cp /usr/local/keepalived/sbin/keepalived/usr/sbin/
cp/usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp/usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir/etc/keepalived


上面的安装过程可以写一个脚本,名字为install_keepalived.sh,内容如下所示:


#!/bin/bash
cd/usr/local/src
wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz
tar zxvfkeepalived-1.1.19.tar.gz
cdkeepalived-1.1.19
./configure--prefix=/usr/local/keepalived
make
makeinstall
cp/usr/local/keepalived/sbin/keepalived /usr/sbin/
cp/usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp/usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

 


给其执行权限即可执行了,chmod +x install_keepalived.sh。
mkdir/etc/keepalived
cd/etc/keepalived/
vimkeepalived.conf


!Configuration File for keepalived
global_defs{
  notification_email {
  yuhongchun027@163.com
       }
  notification_email_fromkeepalived@chtopnet.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}
vrrp_instanceVI_1 {
   state MASTER
   interface eth0
   virtual_router_id 51
   mcast_src_ip 192.168.1.122               <==主nginx的IP地址
   priority 101
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass chtopnet
   }
   virtual_ipaddress {
       192.168.1.128                      <==vip地址
   }
}
servicekeepalived start


[root@localhostkeepalived]# ip addr
1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
   link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00
   inet 127.0.0.1/8 scope host lo
   inet6 ::1/128 scope host
      valid_lft forever preferred_lft forever
2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
   link/ether 00:0c:29:61:b5:f0 brdff:ff:ff:ff:ff:ff
   inet 192.168.1.122/24 brd 192.168.1.255scope global eth0
  inet192.168.1.128/32 scope global eth0
   inet6 fe80::20c:29ff:fe61:b5f0/64 scopelink
      valid_lft forever preferred_lft forever
3: sit0:<NOARP> mtu 1480 qdisc noop
   link/sit 0.0.0.0 brd 0.0.0.0
4:virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue
   link/ether 00:00:00:00:00:00 brdff:ff:ff:ff:ff:ff
   inet 192.168.122.1/24 brd 192.168.122.255scope global virbr0


说明主机上面的VIP地址已经启动,这样主服务器就配置好了,辅机的配置大致一样,除了配置文件有少部分的变化,下面贴出辅机的配置文件:
!Configuration File for keepalived
global_defs{
  notification_email {
  yuhongchun027@163.com
       }
  notification_email_fromkeepalived@chtopnet.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}
vrrp_instanceVI_1 {
   state BACKUP
   interface eth0
   virtual_router_id 51
   mcast_src_ip 192.168.1.123              <==主nginx的IP的地址
   priority 100
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass chtopnet
   }
   virtual_ipaddress {
       192.168.1.128
   }
}
检查其配置
[root@localhostkeepalived]# ip addr
1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
   link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00
   inet 127.0.0.1/8 scope host lo
   inet6 ::1/128 scope host
      valid_lft forever preferred_lft forever
2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
   link/ether 00:0c:29:3b:8f:cc brdff:ff:ff:ff:ff:ff
   inet 192.168.1.123/24 brd 192.168.1.255scope global eth0
   inet6 fe80::20c:29ff:fe3b:8fcc/64 scopelink
      valid_lft forever preferred_lft forever
3: sit0:<NOARP> mtu 1480 qdisc noop
   link/sit 0.0.0.0 brd 0.0.0.0


测试其效果方法很简单,分别在两台Web的上/var/www/html/下建立不同的主页及内容文
件index.html,然后通过负载均衡机器测试即可。


实验推荐步骤:
一、在主从Nginx机器上面分别配置好nginx的upstream模块,在XP客户端机器上面可以分别访问两台LB机器,均能使其达到负载均衡的目的;
二、分别停用或重启Nginx机器,然后在XP客户端访问VIP地址,看服务是否停止;多用ip addr看VIP地址的漂移情况。
三、Keepalived的工作原理是priority级别,谁级别高谁做老大,你可以提升或降低其优先级,看VIP地址发生漂移没?

 


重点部分放在两台Nginx LB机器上面,多用ip addr 观察VIP漂移情况,另外,tcpdump工具也可以抓包分析下。

Tag标签: Nginx   keepalived   主从负载均衡  
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规