设为首页 收藏本站
查看: 2761|回复: 0

[经验分享] nginx+keepalievd,实现负载均衡和故障点切换。keepalived双机热备。

[复制链接]
发表于 2015-9-4 12:21:26 | 显示全部楼层 |阅读模式
  接上文  学习日记4:nginx负载均衡(二)2012.01.08

  ......
  到此,整体工作还差一个备用nginx proxy,其中涉及到keepalived,做心跳。

     
  
DSC0000.png
  黄线以上做完了。
  
  
  本次主要做的是1加一台从nginx_proxy ,暂且命令为front2(为从)。之前的nginx_proxy命令为front(为主)
  
  1 在主nginx上配置keepalived。



cd /usr/local/src/
wget http://keepalived.org/software/keepalived-1.2.7.tar.gz
tar -xzvf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7/
./configure --prefix=/usr/local/keepalived
  这时候会报错的!



configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files.            !!!
  缺少gcc,安装下面扩展



yum install gcc gcc-c++ autoconf automake
  继续编译,下面还会报错,类似这样的错误。
  



make: *** No targets specified and no makefile found.  Stop.


configure: error: ! OpenSSL is not properly installed on your system. configure: error: !
  
  对此,我们安装以下扩展即可解决。



yum -y install openssl-devel


yum -y install popt-devel
  
  在编译



./configure --prefix=/usr/local/keepalived
  
  这时候,会出现下面的结果,这个结果就表明是ok的。到此你的keepalived安装是成功的!




DSC0001.gif
Keepalived configuration
------------------------
Keepalived version       : 1.2.7
Compiler                 : gcc
Compiler flags           : -g -O2
Extra Lib                : -lpopt -lssl -lcrypto
Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
IPVS use libnl           : No
Use VRRP Framework       : Yes
Use VRRP VMAC            : Yes
SNMP support             : No
Use Debug flags          : No

  
  继续下面的操作
  





make
make install
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
cd /etc/keepalived
vim keepalived.conf

  
   
  keepalived.conf配置文件比较重要
  front1 :主nginx的配置文件如下:





! Configuration File for keepalived
global_defs {
router_id LVS_01
}
vrrp_instance VI_1 {
state MASTER  #master 主nginx
interface eth1
virtual_router_id 51
mcast_src_ip 192.168.65.133 #本机ip地址也即主nginx地址
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.65.128 #vip 虚拟出来的ip地址 要求在内网中未被使用
}
}

  
  到此,主nginx配置完成。同样的操作,在从nginx上也配置一遍。
  不过从nginx的配置文件如下:





[iyunv@localhost nginx]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_01
}
vrrp_instance VI_1 {
state BACKUP #slave
interface eth2
virtual_router_id 51
mcast_src_ip 192.168.65.137 #slave ip
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.65.128 #vip
}
}

  
  重启两边的keepalived服务:



[iyunv@localhost conf.d]# service keepalived restart
Stopping keepalived:                                       [FAILED]
Starting keepalived:                                       [  OK  ]
  主nginx上,用





[iyunv@localhost conf.d]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00: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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:20:e9:20 brd ff:ff:ff:ff:ff:ff
inet 192.168.65.133/24 brd 192.168.65.255 scope global eth1
inet 192.168.65.128/32 scope global eth1
inet6 fe80::20c:29ff:fe20:e920/64 scope link
valid_lft forever preferred_lft forever

  到此,我们的vip算是真正的绑定上了。
  查看keepalived的日志:





root@localhost conf.d]# tail -f /var/log/messages
Jan  9 18:53:06 localhost Keepalived_healthcheckers[3355]: Using LinkWatch kernel netlink reflector...
Jan  9 18:53:06 localhost Keepalived_vrrp[3356]: Configuration is using : 34824 Bytes
Jan  9 18:53:06 localhost Keepalived_vrrp[3356]: Using LinkWatch kernel netlink reflector...
Jan  9 18:53:06 localhost Keepalived_vrrp[3356]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)]
Jan  9 18:53:06 localhost Keepalived_vrrp[3356]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jan  9 18:53:07 localhost Keepalived_vrrp[3356]: VRRP_Instance(VI_1) Entering MASTER STATE
Jan  9 18:53:07 localhost Keepalived_vrrp[3356]: VRRP_Instance(VI_1) setting protocol VIPs.
Jan  9 18:53:07 localhost avahi-daemon[1765]: Registering new address record for 192.168.65.128 on eth1.IPv4.
Jan  9 18:53:07 localhost Keepalived_healthcheckers[3355]: Netlink reflector reports IP 192.168.65.128 added
Jan  9 18:53:07 localhost Keepalived_vrrp[3356]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.65.128
Jan  9 18:53:12 localhost Keepalived_vrrp[3356]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.65.128

  从nginx上,同样





[iyunv@localhost nginx]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00: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: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:a7:81:c7 brd ff:ff:ff:ff:ff:ff
inet 192.168.65.137/24 brd 192.168.65.255 scope global eth2
inet6 fe80::20c:29ff:fea7:81c7/64 scope link
valid_lft forever preferred_lft forever

  我们发现并没有绑定vip 128
  同时;





[iyunv@localhost nginx]# tail -f /var/log/messages
Jan  9 19:16:25 localhost Keepalived_healthcheckers[22420]: Registering Kernel netlink reflector
Jan  9 19:16:25 localhost Keepalived_healthcheckers[22420]: Registering Kernel netlink command channel
Jan  9 19:16:25 localhost Keepalived_vrrp[22421]: Opening file '/etc/keepalived/keepalived.conf'.
Jan  9 19:16:25 localhost Keepalived_healthcheckers[22420]: Opening file '/etc/keepalived/keepalived.conf'.
Jan  9 19:16:25 localhost Keepalived_vrrp[22421]: Configuration is using : 34822 Bytes
Jan  9 19:16:25 localhost Keepalived_healthcheckers[22420]: Configuration is using : 4821 Bytes
Jan  9 19:16:25 localhost Keepalived_vrrp[22421]: Using LinkWatch kernel netlink reflector...
Jan  9 19:16:25 localhost Keepalived_vrrp[22421]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jan  9 19:16:25 localhost Keepalived_vrrp[22421]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)]
Jan  9 19:16:25 localhost Keepalived_healthcheckers[22420]: Using LinkWatch kernel netlink reflector...

  
  貌似从nginx似乎出了什么问题,先不着急。
  这时候我们停止掉主nginx的服务,再看看效果怎么样!



killall nginx
  再观察从nginx的状态变化





[iyunv@localhost nginx]# tail -f /var/log/messages
Jan  9 19:16:25 localhost Keepalived_healthcheckers[22420]: Registering Kernel netlink reflector
Jan  9 19:16:25 localhost Keepalived_healthcheckers[22420]: Registering Kernel netlink command channel
Jan  9 19:16:25 localhost Keepalived_vrrp[22421]: Opening file '/etc/keepalived/keepalived.conf'.
Jan  9 19:16:25 localhost Keepalived_healthcheckers[22420]: Opening file '/etc/keepalived/keepalived.conf'.
Jan  9 19:16:25 localhost Keepalived_vrrp[22421]: Configuration is using : 34822 Bytes
Jan  9 19:16:25 localhost Keepalived_healthcheckers[22420]: Configuration is using : 4821 Bytes
Jan  9 19:16:25 localhost Keepalived_vrrp[22421]: Using LinkWatch kernel netlink reflector...
Jan  9 19:16:25 localhost Keepalived_vrrp[22421]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jan  9 19:16:25 localhost Keepalived_vrrp[22421]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)]
Jan  9 19:16:25 localhost Keepalived_healthcheckers[22420]: Using LinkWatch kernel netlink reflector...
Jan  9 19:17:33 localhost Keepalived_vrrp[22421]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jan  9 19:17:34 localhost Keepalived_vrrp[22421]: VRRP_Instance(VI_1) Entering MASTER STATE
Jan  9 19:17:34 localhost Keepalived_vrrp[22421]: VRRP_Instance(VI_1) setting protocol VIPs.
Jan  9 19:17:34 localhost Keepalived_vrrp[22421]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168.65.128
Jan  9 19:17:34 localhost avahi-daemon[1810]: Registering new address record for 192.168.65.128 on eth2.IPv4.
Jan  9 19:17:34 localhost Keepalived_healthcheckers[22420]: Netlink reflector reports IP 192.168.65.128 added
Jan  9 19:17:39 localhost Keepalived_vrrp[22421]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168.65.128

  从第33秒后发现,备用nginx已经转为master状态。
  再看





[iyunv@localhost nginx]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00: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: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:a7:81:c7 brd ff:ff:ff:ff:ff:ff
inet 192.168.65.137/24 brd 192.168.65.255 scope global eth2
inet 192.168.65.128/32 scope global eth2
inet6 fe80::20c:29ff:fea7:81c7/64 scope link
valid_lft forever preferred_lft forever

  发现没有?vip飘过来了!
  

  针对Keepalived的不足,用check_nginx.sh来监控nginx进程,实现真正意义上的负载均衡高可用.
此脚本思路其实也很简单,即放置在后台一直监控nginx进程;如进程消失,尝试重启nginx,
如是失败则立即停掉本机的keepalived服务,让另一台负载均衡器接手.






vi /shell/nginx_pid.sh
###################################################
#!/bin/bash
while :
do
nginxpid=`ps -C nginx --no-header | wc -l`
if [ $nginxpid -eq 0 ]
then
/usr/local/nginx-0.8.53/sbin/nginx
sleep 5
if [ $nginxpid -eq 0 ]
then
/etc/init.d/keepalived stop
fi
fi
sleep 5
done
###################################################

  
  
  我们仍然可以继续访问我们的站点。至此。故障点切换宣告结束。
  
  
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-109364-1-1.html 上篇帖子: keepalived+LVS的关键点知识 下篇帖子: Keepalived高可用软件的安装与配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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