Nginx+keepalived主从双机热备自动切换架构

Nginx+keepalived主从双机热备自动切换架构

1:安装 keepalived


安装 keepalived 非常的简单和容易,这跟安装其他 GNU 源码软件步骤是以模一样的。下面
给出其安装过程

下载最新稳定版  wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz
解包    tar zxvf keepalived-1.1.17.tar.gz
切换目录  cd keepalived-1.1.17
配置  ./configure –prefix=/usr/local/keepalive
编译和安装  make ; make install  

Keepalived 安装完成后,会在安装目录/usr/local/keepalived 生成  bin,etc,man,sbin 这 4 个目录。
其中 etc 为配置文件所在的目录.

值得注意的是,keepalived 的启动过程并不会对配置文件进行语法检查,就算没有配置文件,
keepalived的守护进程照样能够被运行起来.在默认状态下– 即不指定配置文件的位置,
keepalived先查找文件  /etc/keepalived/keepalived.conf,如果为了省事,
可以手动创建这个文件,然后在这个文件里书写规则,来达到控制keepalived 运行的目的。  

2:配置 keepalived.conf

一个功能比较完整的keepalived 的配置文件,其配置文件keepalived.conf可以包含三个文本
块:全局定义块、VRRP 实例定义块及虚拟服务器定义块.全局定义块和虚拟服务器定义块
是必须的,如果在只有一个负载均衡器的场合,就不须VRRP实例定义块.  
 
接下来,我们以一个配置文件模版为例,有选择的说明其中一些重要项的功能或作用.

#全局定义块
global_defs {
   notification_email {
     sujie.zhang@xtgxiso.cn
   }
   notification_email_from root@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   #以上是email通知,作用:有故障,发邮件报警。这是可选项目,建议不用
   router_id LVS_DEVEL
   #Lvs负载均衡器标识(lvs_id),在一个网络内,它应该是唯一的
}

#VRRP定义块
vrrp_sync_group VG_1 {
    group {
       VI_1
    }
}
vrrp_instance VI_1 {
    state MASTER    #实例状态state.只有 MASTER 和 BACKUP 两种状态,并且需要大写这些单词
    interface eth0    #通信接口 interface 。对外提供服务的网络接口,如 eth0,eth1
    virtual_router_id 86    #虚拟路由标识,这个标识是一个数字,并且同一个vrrp实例使用唯一的标识
    priority 188            #优先级priority.这是一个数字,数值愈大,优先级越高
    advert_int 1            #同步通知间隔,单位为秒
    authentication {
        auth_type PASS        #验证类型
        auth_pass 1234        #验证密码
    }
    virtual_ipaddress {
        192.168.1.123 dev eth0    #虚拟ip地址
    }
}

#虚拟服务器定义块,是keepalived最重要的项目了,是keepalived.conf必不可少的部分
virtual_server 192.168.1.123 80 {
    delay_loop 6    #健康检查时间间隔,单位是秒
    lb_kind DR        #负载均衡转发规则,一般包括 DR,NAT,TUN3种,在我的方案中,都使用DR的方式
    persistence_timeout 50    #会话保持时间,单位是秒
    protocol TCP            #转发协议,一般有tcp和udp两种
    
    #真实服务器
    real_server 192.168.1.100 80 {
        weight 1    #权重weight.权重值是一个数字,数值越大,权重越高
        
        notify_down “/root/service_down.sh”    #检测到真实服务down后执行的脚本
        
        #下面检查任意一种检查方式
        
        #http或ssl检查
        HTTP_GET|SSL_GET {
            url {
              path /
              digest ff20ad2481f97b1754ef3e12ecd3a9cc
            }
            connect_port    444
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
        
        #Tcp检查
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
        
        #脚本检查
        MISC_CHECK {
              misc_path /usr/local/bin/script.sh!    #外部程序检查
              misc_timeout    10                    #脚本执行超时时间                        
        }
        
    }
    
}

3:nginx+keepalived主从自动切换示例


有两台机器:
192.168.1.6        主
192.168.1.7        从
虚ip    192.168.1.8     最好保局域网内无此IP

前提是两台主机已经安装好了nginx和keepalived.假设nginx的虚拟主机是test.xtgxiso.cn

主(192.168.1.6)keepalived配置文件

vrrp_instance VI_1 {
    state MASTER    
    interface eth0    
    virtual_router_id 86    
    priority 188            
    advert_int 1            
    authentication {
        auth_type PASS        
        auth_pass 1234        
    }
    virtual_ipaddress {
        192.168.1.8 dev eth0
    }
}

virtual_server 192.168.1.8 80 {
    delay_loop 6    
    lb_kind DR        
    persistence_timeout 50    
    protocol TCP            
    
    
    real_server 192.168.1.6     80 {
    
        weight 1    
        notify_down “/sbin/service keepalived stop”
        
        HTTP_GET{
        
            url {
              path “/index.php”
            }
            
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
        
    }
    
}

为了检查的正确性,最好在hosts将域名指向本机(192.168.1.6)

从(192.168.1.7)keepalived配置文件

vrrp_instance VI_1 {
    state BACKUP    
    interface eth0    
    virtual_router_id 86    
    priority 100            
    advert_int 1            
    authentication {
        auth_type PASS        
        auth_pass 1234        
    }
    virtual_ipaddress {
        192.168.1.8 dev eth0
    }
}

virtual_server 192.168.1.8 80 {
    delay_loop 6    
    lb_kind DR        
    persistence_timeout 50    
    protocol TCP            
    
    
    real_server 192.168.1.7     80 {
    
        weight 1    
        notify_down “/sbin/service keepalived stop”
        
        HTTP_GET{
        
            url {
              path “/index.php”
            }
            
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
        
    }
    
}

同样为了检查的正确性,最好在hosts将域名指向本机(192.168.1.7)

4:测试


分别在2台nginx上启动nginx和keepalived服务,然后分别用ip a 查看ip
也可以通过tail -f /var/log/message 查看日志信息.
通过杀掉主从的nginx看到切换情况

总结:

有的时候会出现主从不正常或其他异常情况,一般是keepalived不能正常通信造成的
keepalived不能正常通信,除了配置错误之外,通常是由于防火墙的原因,很多资料都没有提及这点

请检查防火墙规则符合下面的条件:
1:keepalived 默认需要使用D类多播地址224.0.0.18进行心跳通信
2:keepalived 使用vrp协议进行通信(端口号为112)

检测两个keepalived主机之间是否能通信的办法有
停掉一个keepalived,看另外一个keepalived的日志/var/log/messages 里是否有新的日志

There’s a saying which warns against that
weight loss tips Trendsetting Styles With Elegant Fashion

In response to civil unrest and
christina aguilera weight lossHow to Convert Temperature Measurements
Cold Calling Means More for Canada
rastreamento correios speculating what to do about countries wind up material closing

Wholesale jewelry trend 2011 guide
jogos da barbie According to Milinda Martin

Ideal Clothes for Bike Commuting
movie2k tips do design core techniques

How to Raise Money for a College Fashion Show
ebay kleinanzeigen the common thread is always planning and forethought

a Commander of the British Empire
rape porn and pottery student Ayumi

Making A UK Comeback after years out of fashion
anime porn This might be that models with sharp

Up Games for Teens and Girls
large porn tube Courses include online study of fashion design and illustration

Getting rid of smoke smell in a carpet
miranda lambert weight loss and it has little to do with fashion
free porn sites
此条目发表在 网站架构 分类目录,贴了 , 标签。将固定链接加入收藏夹。

评论功能已关闭。