NGINX Keepalived高可用简单使用 之十

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NGINX Keepalived高可用简单使用 之十相关的知识,希望对你有一定的参考价值。

1、Keepalived高可用的工作原理

工作原理:在Keepalived服务正常工作时,主Master节点会不断地向备节点发送(多播的方式)心跳消息,用以告诉备Backup节点自己还活着,当主Master节点发生故障时,就无法发送心跳消息,备节点也就因此无法继续检查到来自主Master节点的心跳了,于是调用自身的接管程序,接管主Master节点的IP资源及服务。而当主Master节点恢复时,备Backup节点又会释放主节点故障时自身接管的IP资源及服务,恢复到原来的备用角色。

2、Keepalived的安装

yum -y install keepalived

配置文件:/etc/keepalived/keepalived.conf

3、配置Keepalived实现单IP自动漂移接管

3.1、实例环境

lb01:10.3.151.183(主配置)
lb02:10.3.151.184(备配置)
VIP: 10.3.151.228 (虚拟IP)

3.2、主配置如下

! Configuration File for keepalived

global_defs {
   notification_email {
   [email protected]
}
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id lb01       #id为lb01,不同的keepalived.conf此ID要唯一
}

vrrp_instance VI_1 {           #实例名字为VI_1,相同实例的备节点名字要和这个相同
    state MASTER                #主状态为MASTER(必须大写),备节点为BACKUP
    interface eth2                  #通信接口网卡
    virtual_router_id 51         #实例ID为51,keepalived.conf里唯一
    priority 150                      #优先级为150,数值越大,优先级越高,与BACKUP对比
    advert_int 1                     #通信检查间隔为1秒
    authentication {
        auth_type PASS         #PASS认证类型
        auth_pass 2222          #密码为2222
    }
    virtual_ipaddress {
        10.3.151.228/24 dev eth2 label eth2:1     #虚拟IP地址
    }
}

3.3、备节点配置文件

! Configuration File for keepalived

global_defs {
   notification_email {
   [email protected]
}
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id lb02
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth2
    virtual_router_id 51
    priority 140
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2222
    }
    virtual_ipaddress {
        10.3.151.228/24 dev eth2 label eth2:1
    }
}

4、检查keepalived是否生效

[[email protected] nginx]# ifconfig | grep "inet addr"
          inet addr:10.3.151.183  Bcast:10.3.151.255  Mask:255.255.255.0
          inet addr:10.3.151.228  Bcast:0.0.0.0  Mask:255.255.255.0   
                    #在主节点,检查VIP是否生效
          inet addr:127.0.0.1  Mask:255.0.0.0
[[email protected] nginx]# /etc/init.d/keepalived stop      #停止主节点的keepalived
Stopping keepalived: [  OK  ]
[[email protected] nginx]# ifconfig | grep "inet addr"        #VIP已飘走
          inet addr:10.3.151.183  Bcast:10.3.151.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0
[[email protected] nginx]# ifconfig | grep "inet addr"       #在备节点上查看VIP是否存在
          inet addr:10.3.151.184  Bcast:10.3.151.255  Mask:255.255.255.0
          inet addr:10.3.151.228  Bcast:0.0.0.0  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0

以上是关于NGINX Keepalived高可用简单使用 之十的主要内容,如果未能解决你的问题,请参考以下文章

NGINX Keepalived高可用简单使用 之十

你需要了解的高可用方案之使用keepalived搭建双机热备一览

微服务实战系列-网关高可用之中间件Keepalived

Linux架构之Nginx 高可用

MySQL之keepalived高可用

keepalived+nginx高可用环境搭建