首页 > 系统相关 >22-Nginx高可用(基于Keepalived实现双机主备)

22-Nginx高可用(基于Keepalived实现双机主备)

时间:2022-09-06 18:57:47浏览次数:119  
标签:22 Keepalived keepalived 192.168 vrrp MASTER 机主 节点

双机主备

HA其实就是高可用, 现在部署的其实就是一台Nginx, 但凡是单节点, 都会存在宕机的可能性, 所以我们需要一个备用机, 来完成高可用, 解决单点故障问题

Keepalived概念

  • 解决单点故障
  • 组件免费
  • 可以实现高可用HA机制
  • 基于VRRP协议

虚拟理由冗余协议(VRRP)

  • Virtual Router Redundancy Protocol
  • 解决内网单机故障的路由协议
  • 构建多个路由器 MASTER BACKUP
  • 虚拟IP - VIP (Virtual IP Address)

Keepalived双机主备原理

会在主节点和备节点的上方虚拟出一个虚IP, 提供用户访问, 当然, 用户访问的还是域名

安装 Keepalived

我使用的是

主: 192.168.247.136

备: 192.168.247.137

虚拟IP: 192.168.247.140

官网地址

https://www.keepalived.org/

下载

为了防止出错和老师下载一样的版本2.0.18

安装

上传到Linux

解压

tar -zxvf keepalived-2.0.18.tar.gz

进入keepalived文件夹

cd keepalived-2.0.18/

执行配置

./configure \
--prefix=/usr/local/keepalived \
--sysconf=/etc

编译并安装

make && make install

查找安装位置

[root@localhost keepalived-2.0.18]# whereis keepalived
keepalived: /etc/keepalived /usr/local/keepalived

配置Keepalived

进入文件夹

cd /etc/keepalived/
vi keepalived.conf

修改配置

! Configuration File for keepalived

global_defs {
   # 主节点宕机后通知的邮箱
#   notification_email {
#     [email protected]
#     [email protected]
#     [email protected]
#   }
#   notification_email_from [email protected]
#   smtp_server 192.168.200.1
#   smtp_connect_timeout 30
   # 路由ID, 当前安装Keepalived节点主机的标识符, 全局唯一
   router_id keep_136
#   vrrp_skip_check_adv_addr
#   vrrp_strict
#   vrrp_garp_interval 0
#   vrrp_gna_interval 0
}

# 计算机的节点
vrrp_instance VI_1 {
    # 节点状态, MASTER代表主节点 BACKUP代表备用节点
    state MASTER
    # 使用ens33网卡
    interface ens33
    # 虚拟路由ID, 保持MASTER和BACKUP一致
    virtual_router_id 51
    # 优先级/权重 100 谁的优先级高, 在MASTER宕机后, 谁就能成为MASTER
    priority 100
    # 主备之间的心跳周期 1秒
    advert_int 1
    # 认证授权, 防止非法节点的进入
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    # 虚拟IP
    virtual_ipaddress {
        192.168.247.140
#        192.168.200.17
#        192.168.200.18
    }
}

启动Keepalived

进入文件夹

cd /usr/local/keepalived/sbin/

启动

./Keepalived

查看ip

访问

虚拟IP已经是可以访问的了

停止Keepalived

ps -ef|grep keepalived
# 通过kill结束
kill -9 进程号

将Keepalived注册为系统服务

进入文件夹

cd /home/software/keepalived-2.0.18/keepalived/
cd etc/

复制服务到init.d文件夹

cp init.d/keepalived /etc/init.d/

复制配置到sysconfig文件夹

cp sysconfig/keepalived /etc/sysconfig/

如果提示是否覆盖直接 y

重新加载系统服务

systemctl daemon-reload

通过系统命令启动和停止

systemctl start keepalived.service
systemctl stop keepalived.service

查看服务

ps -ef|grep keepalived

Keepalived实现双机主备高可用

137安装Keepalived

配置的时候提示警告了, 原因是缺少库

*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.

安装一下

yum install libnl libnl-devel

安装完成再次执行配置, 成功

修改配置文件

! Configuration File for keepalived

global_defs {
   # 主节点宕机后通知的邮箱
#   notification_email {
#     [email protected]
#     [email protected]
#     [email protected]
#   }
#   notification_email_from [email protected]
#   smtp_server 192.168.200.1
#   smtp_connect_timeout 30
   # 路由ID, 当前安装Keepalived节点主机的标识符, 全局唯一
   router_id keep_137
#   vrrp_skip_check_adv_addr
#   vrrp_strict
#   vrrp_garp_interval 0
#   vrrp_gna_interval 0
}

# 计算机的节点
vrrp_instance VI_1 {
    # 节点状态, MASTER代表主节点 BACKUP代表备用节点
    state BACKUP
    # 使用ens33网卡
    interface ens33
    # 虚拟路由ID, 保持MASTER和BACKUP一致
    virtual_router_id 51
    # 优先级/权重 100 谁的优先级高, 在MASTER宕机后, 谁就能成为MASTER
    priority 80
    # 主备之间的心跳周期 1秒
    advert_int 1
    # 认证授权, 防止非法节点的进入
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    # 虚拟IP
    virtual_ipaddress {
        192.168.247.140
#        192.168.200.17
#        192.168.200.18
    }
}

设置为备用机

启动137的Keepalived

测试136宕机, keepalived挂掉

先访问一下140, MASTER没有问题, 下面停止136

systemctl stop keepalived.service

成功切换到备用机, 成功达成双机主备高可用

此时137已经绑定了140的虚拟IP

这个时候模仿136恢复运转

systemctl start keepalived.service

成功自动切回MASTER, 完成~

标签:22,Keepalived,keepalived,192.168,vrrp,MASTER,机主,节点
From: https://www.cnblogs.com/flower-dance/p/16662955.html

相关文章