【 Keepalived 】Nginx or Http 主-主模式

上一篇:【 Keepalived 】Nginx or Http 主-备模式

在此基础上进行修改并实现 Keepalived主-主模式

首先,需要理解的是:主-备模式是一个VIP在工作,主-主模式则需要两个VIP来工作。一旦其中一台主机出现问题,则两个VIP都会绑定到同一台主机上,待到故障解除,重新启动httpd服务,恢复正常。

ka1 配置文件:

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server localhost
   smtp_connect_timeout 30
   router_id NodeA
}

vrrp_script check_nginx {
   script "/etc/keepalived/bash/check_nginx.sh"
   interval 5
   weight -10
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    check_nginx
    }
    virtual_ipaddress {
        192.168.2.200/24
    }
}
vrrp_instance VI_2 {
    state BUCKUP
    interface eth0
    virtual_router_id 52
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        check_nginx
    }
    virtual_ipaddress {
        192.168.2.201/24
    }
}

ka2 配置文件:

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server localhost
   smtp_connect_timeout 30
   router_id NodeB
}

vrrp_script check_nginx {
   script "/etc/keepalived/bash/check_nginx.sh"
   interval 5
   weight -10
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    check_nginx
    }
    virtual_ipaddress {
        192.168.2.200/24
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface eth1
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        check_nginx
    }
    virtual_ipaddress {
        192.168.2.201/24
    }
}

测试:

正常情况下:

ka1:

[ka1 [email protected]192.168.2.10 ~]#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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:39:92:4f brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.10/24 brd 192.168.2.255 scope global eth0
    inet 192.168.2.200/24 scope global secondary eth0
    inet6 fe80::20c:29ff:fe39:924f/64 scope link
       valid_lft forever preferred_lft forever

ka2:

[ka2 [email protected]192.168.2.11 ~]#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:7b:9f:8c brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.11/24 brd 192.168.2.255 scope global eth1
    inet 192.168.2.201/24 scope global secondary eth1
    inet6 fe80::20c:29ff:fe7b:9f8c/64 scope link
       valid_lft forever preferred_lft forever

当ka1 httpd出现错误时:

ka1 VIP被转移

[ka1 [email protected]192.168.2.10 ~]#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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:39:92:4f brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.10/24 brd 192.168.2.255 scope global eth0
    inet6 fe80::20c:29ff:fe39:924f/64 scope link
       valid_lft forever preferred_lft forever

ka2 被绑定了两个VIP
[ka2 [email protected]192.168.2.11 ~]#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:7b:9f:8c brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.11/24 brd 192.168.2.255 scope global eth1
    inet 192.168.2.201/24 scope global secondary eth1
    inet 192.168.2.200/24 scope global secondary eth1
    inet6 fe80::20c:29ff:fe7b:9f8c/64 scope link
       valid_lft forever preferred_lft forever

再三强调:在keepalived使用中,脚本很重要很重要。要经过严格的测试调试,才能使用,我这里在贴一遍脚本,上个主-备中也有脚本,需要灵活修改的地方我红色标识出来:

#!/bin/bash

pidfile=/var/lock/subsys/`basename $0`.pid
if [ -f $pidfile ] && [ -e /proc/`cat $pidfile` ] ; then
    exit 1
fi
trap "rm -rf $pidfile ; exit 0" 1 2 3 15
echo $$ > $pidfile
maxfails=3
fails=0
success=0

while [ 1 ]
do

    /usr/bin/wget --timeout=3 --tries=1 http://192.168.2.11/ -q -O /dev/null && ping -c1 192.168.2.1 &> /dev/null
    if [ $? -ne 0 ] ; then
        let fails=$[$fails+1]
        success=0
    else
        fails=0
        let success=$[$success+1]
    fi

    if [ $fails -ge $maxfails ] ; then
        fails=0
        success=0
        #check keepalived is running ? try to stop it
        /etc/init.d/keepalived status | grep 正在运行
        if [ $? -eq 0 ] ; then
            /usr/bin/logger -is "local service fails $maxfails times ... try to stop keepalived."
            /etc/init.d/keepalived stop 2>&1 | /usr/bin/logger
        fi

    fi

    if [ $success -gt $maxfails ] ; then
        #check keepalived is stopped ? try to start it
        /etc/init.d/keepalived status | grep 已停
        if [ $? -eq 0 ] ; then
            /usr/bin/logger -is "service changes normal, try to start keepalived ."
            /etc/init.d/keepalived start
        fi
        success=0
    fi
    sleep 3

done
时间: 2024-10-10 04:47:58

【 Keepalived 】Nginx or Http 主-主模式的相关文章

【 Keepalived 】Nginx or Http 主-备模式

一.主-备模式: 操作系统:centos 6.4 x64 ka1: 192.168.2.10 ka2: 192.168.2.11 vip: 192.168.2.200 ka1-master服务器配置 [ka1 [email protected]192.168.2.10 ~]#yum install httpd keepalived -y # 这里使用apache代替nginx,效果是一样的,然后直接yum安装keepalived [ka1 [email protected]192.168.2.1

Nginx + Keepalived(主备模式)实现负载均衡高可用浅析

概述 目前关于负载均衡和高可用的架构方案能找到相当多且详尽的资料,此篇是自己学习相关内容的一个总结,防止将来遗忘再次重新查找资料,也避免踩相同的坑. 此次配置的负载均衡与高可用架构:Nginx + Keepalived(主备模式),Nginx 使用反向代理实现七层负载均衡. 众所周知,Nginx 是一款自由的.开源的.高性能HTTP服务器和反向代理服务器,也是一个IMAP.POP3.SMTP代理服务器. 也就是说Nginx本身就可以托管网站(类似于Tomcat一样),进行HTTP服务处理,也可以

Nginx+keepalived高可用(双主模式)

负载均衡技术对于一个网站尤其是大型网站的web服务器集群来说是至关重要的!做好负载均衡架构,可以实现故障转移和高可用环境,避免单点故障,保证网站健康持续运行.由于业务扩展,网站的访问量不断加大,负载越来越高.现需要在web前端放置nginx负载均衡,同时结合keepalived对前端nginx实现HA高可用.介绍下Nginx和keepalive1.Nginx Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性:Nginx作为负载均衡服务器:Nginx 既可以在内部直

keepAlived+nginx实现高可用双主模型LVS

实验目的: 利用keepalived实现高可用反向代理的nginx.以及双主模型的ipvs 实验环境: node1:在nginx做代理时做反向代理节点,在keepalived实现LVS时做Director.VIP1:172.16.18.22 VIP2:172.16.18.23 node2:在nginx做代理时做反向代理节点,在keepalived实现LVS时做Director.VIP1:172.16.18.22 VIP2:172.16.18.23 node3:在nginx做代理时做web服务器.

Haproxy+Keepalived高可用环境部署梳理(主主和主从模式)

Nginx.LVS.HAProxy 是目前使用最广泛的三种负载均衡软件,本人都在多个项目中实施过,通常会结合Keepalive做健康检查,实现故障转移的高可用功能. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

LVS+Keepalived 高可用环境部署记录(主主和主从模式)

一.LVS+Keepalived主从热备的高可用环境部署 1)环境准备 1 2 3 4 5 6 7 8 9 10 11 12 LVS_Keepalived_Master      182.148.15.237 LVS_Keepalived_Backup      182.148.15.236 Real_Server1               182.148.15.233 Real_Server2               182.148.15.238 VIP                

5 keepalived高可用ipvs(主备模式)

keepalived最初是为了ipvs设计的,实现HA功能.是工作在linux上,实现vrrp协议的软件. vrrp:Virtual Router Redundancy Protocol,虚拟路由冗余协议,解决局域网中配置静态网关出现单点失效现象的路由协议 ipvs实际上是一系列规则,配置即可不需要转移. 轻量级,不需要共享存储时使用. keepalived+nginx keepalived+harproxy ipvs HA 环境:director server :CentOS 6.7 1 yu

03-keepalived实现nginx负载均衡服务的高可用(主主模式)

相比较主备模式,可以设置另一个虚拟路由 此为controller1配置 ! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id controller1 vrrp_mcast_gr

Mogilefs分布式文件系统-Keepalived+Nginx双主模型实现图片分布式存储、访问

一.分布式文件系统: 分布式文件系统(Distributed File System)是指文件系统管理的物理存储资源不一定直接连接在本地节点上,而是通过计算机网络与节点相连.计算机通过文件系统管理.存储数据,单纯通过增加硬盘个数来扩展计算机文件系统的存储容量的方式,在容量大小.容量增长速度.数据备份.数据安全等方面的表现都差强人意. 分布式文件系统可以有效解决数据的存储和管理难题:将固定于某个地点的某个文件系统,扩展到任意多个地点/多个文件系统,众多的节点组成一个文件系统网络.每个节点可以分布在