用Keepalived搭建双Nginx server集群 防止单点故障

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

综述:

浏览器访问虚拟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.

apt-get install keepalived

另外一台Ubuntu server物理IP是192.168.1.59, 也安装Nginx server和Keapalived. 后面称这台为backup server. Nginx的安装以及配置不是本文关注内容,请参考我的其他文章。

现在开始配置master server. 安装完成keepalived后,通过查看脚本/etc/init.d/keepalived里面发现,启动配置文件的路径是

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

# 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进程的脚本

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

!/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    fifi


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

ping 192.168.1.57PING 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 ms64 bytes from 192.168.1.57: icmp_req=2 ttl=64 time=0.020 ms

对192.168.1.59做相同的配置,略有变化的是:

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.csdn.net/zmj_88888888/article/details/8825471




           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值