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

[经验分享] 用Keepalived搭建双Nginx server集群,防止单点故障

[复制链接]

尚未签到

发表于 2015-9-4 11:59:31 | 显示全部楼层 |阅读模式
  综述:
  浏览器访问虚拟IP: 192.168.1.57, 该虚拟IP被Keepalived接管,两个Keepalived进程分别运行在物理IP为192.168.1.56和192.168.1.59服务器上,这两个服务器上都运行着Nginx server。Nginx server都监听虚拟IP 192.168.1.57. Nginx背后有一些web app集群,已经配置成upstream.
  
  为了防止Nginx自己成为单点瓶颈,这里采用了双Nginx server的方式。每个Server都是Ubuntu 12.04.
  假定我的第一台Ubuntu server物理IP地址是192.168.1.56,已经安装了Nginx server,现在安装Keepalived,后面称这台为master server.
  



[plain] view plaincopyprint?

  • apt-get install keepalived
  
另外一台Ubuntu server物理IP是192.168.1.59, 也安装Nginx server和Keapalived. 后面称这台为backup server. Nginx的安装以及配置不是本文关注内容,请参考我的其他文章。
  
  现在开始配置master server. 安装完成keepalived后,通过查看脚本/etc/init.d/keepalived里面发现,启动配置文件的路径是
  



[plain] view plaincopyprint?

  • CONFIG=/etc/keepalived/keepalived.conf
  但是该文件现在还不存在。所以我创建一个,内容如下:
  
  



[plain] view plaincopyprint?

  • # Settings for notifications
  • global_defs {
  •     notification_email {
  •         csfreebird@gmail.com     # Email address for notifications
  •     }
  •     notification_email_from keepalived@your_company.com  # The from address for the notifications
  •     smtp_server 127.0.0.1
  •     smtp_connect_timeout 15
  • }

  • # Define the script used to check if haproxy is still working
  • vrrp_script chk_http_port {
  •     script "/etc/keepalived/check_nginx.sh" # check Nginx is alive or not
  •     interval 2 #
  •     weight 2
  • }


  • # Configuation for the virtual interface
  • vrrp_instance VI_1 {
  •     interface eth0
  •     state MASTER        # set this to BACKUP on the other machine
  •     priority 101        # set this to 100 on the other machine
  •     virtual_router_id 51

  •     smtp_alert          # Activate email notifications

  •     authentication {
  •     auth_type PASS
  •     auth_pass 1111      # Set this to some secret phrase
  •     }

  •     # The virtual ip address shared between the two loadbalancers
  •     virtual_ipaddress {
  •     192.168.1.57
  •     }

  •     # Use the script above to check if we should fail over
  •     track_script {
  •     chk_http_port
  •     }
  • }
  说明:
  
  1. smtp_server必须要用127.0.0.1
  2. 自己要创建一个检查nginx进程的脚本



[plain] view plaincopyprint?

  • /etc/keepalived/check_nginx.sh
  内容如下:
  
  



[plain] view plaincopyprint?

  • !/bin/bash

  • # try to start nginx if nginx process is dead
  • # shutdonw keepalived process if start nginx failed

  • pid=`ps -C nginx --no-header |wc -l`
  • if [ $pid -eq 0 ];then
  •     service nginx start
  •     sleep 3
  •     if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
  •         service keepalived stop
  •     fi
  • fi
  3. 修改nginx的所有server配置,将server_name都改为虚拟IP
  
  



[plain] view plaincopyprint?

  • server_name  192.168.1.57;
  
  
  4. 注意,这里的虚拟IP不是用修改/etc/network/interfaces的方式,而是在keepalived配置文件中直接设置,要想判断是否成功,直接ping 就行了,ifconfig是看不到的。
  



[plain] view plaincopyprint?

  • ping 192.168.1.57
  • PING 192.168.1.57 (192.168.1.57) 56(84) bytes of data.
  • 64 bytes from 192.168.1.57: icmp_req=1 ttl=64 time=0.024 ms
  • 64 bytes from 192.168.1.57: icmp_req=2 ttl=64 time=0.020 ms
  
  
  对192.168.1.59做相同的配置,略有变化的是:
  



[plain] view plaincopyprint?

  • vrrp_instance VI_1 {
  •     interface eth0
  •     state BACKUP  // changed
  •     priority 100  // changed
  
  通过轮流关闭master和backup,证明keepalived已经有效工作了。
  
  参考资料:Keepalived官方文档真是够差的。
  http://www.leaseweblabs.com/2011/09/setting-up-keepalived-on-ubuntu-load-balancing-using-haproxy-on-ubuntu-part-2/
  http://blog.iyunv.com/zmj_88888888/article/details/8825471

运维网声明 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-109350-1-1.html 上篇帖子: CentOS6.4环境下布署LVS+keepalived笔记 下篇帖子: Nginx + Keepalived 实例(测试可行)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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