CentOS 7 下 Keepalived + Nginx 实现双机高可用

CentOS 7 下 Keepalived + Nginx 实现双机高可用

服务器准备

服务信息
主机名IP角色其他
my-web01192.168.157.31nginx
keepalived
master
my-web02192.168.157.32nginx
keepalived
backup
VIP192.168.157.30
服务架构

在这里插入图片描述

服务安装

nginx
# 所有主机
[root@my-web01 ~]$ yum -y install nginx
Keepalived
# 所有主机
[root@my-web01 ~]$ yum -y install keepalived

服务配置

nginx
  • web01
[root@my-web01 ~]$ cat /usr/share/nginx/html/index.html 
<!DOCTYPE html>
<h1>my web01 ~~~</h1>
  • web02
[root@my-web02 ~]$ cat /usr/share/nginx/html/index.html
<!DOCTYPE html>
<h1>my web02 ~~~</h1>
Keepalived
  • web01
[root@my-web01 ~]$ cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_script check_nginx {
    script "killall -0 nginx"
    interval 2
}

vrrp_instance VI_1 {
    interface ens33
    state MASTER
    priority 200

    virtual_router_id 33
    virtual_ipaddress {
        192.168.157.30
    }

    authentication {
        auth_type PASS
        auth_pass password
    }

    track_script {
        check_nginx
    }
}
  • web02
[root@my-web02 ~]$ cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_script check_nginx {
    script "killall -0 nginx"
    interval 2
}

vrrp_instance VI_1 {
    interface ens33
    state BACKUP
    priority 100

    virtual_router_id 33
    virtual_ipaddress {
        192.168.157.30
    }

    authentication {
        auth_type PASS
        auth_pass password
    }

    track_script {
        check_nginx
    }
}

启动服务

nginx
# 所有节点
systemctl start nginx
keepalived
# 所有节点
systemctl start nginx

服务验证

查看 VIP 状态
# web01 -- 获取 VIP
[root@my-web01 ~]$ ip addr | grep '192.168.157'
    inet 192.168.157.31/24 brd 192.168.157.255 scope global ens33
    inet 192.168.157.30/32 scope global ens33

# web01 -- 未获取 VIP
[root@my-web02 ~]$ ip addr | grep '192.168.157'
    inet 192.168.157.32/24 brd 192.168.157.255 scope global ens33
CURL 命令访问
# VIP -- 访问到 web01
[root@my-web01 ~]$ curl 192.168.157.30
<!DOCTYPE html>
<h1>my web01 ~~~</h1>

# web01
[root@my-web01 ~]$ curl 192.168.157.31
<!DOCTYPE html>
<h1>my web01 ~~~</h1>

# web02
[root@my-web01 ~]$ curl 192.168.157.32
<!DOCTYPE html>
<h1>my web02 ~~~</h1>
浏览器访问
  • vip && web01

在这里插入图片描述

  • vip && web02

在这里插入图片描述

高可用验证

停止 web01 下 Nginx
  • 停止服务
# web01
[root@my-web01 ~]$ systemctl stop nginx
  • 验证 VIP
# web01 -- VIP 漂移
[root@my-web01 ~]$ ip addr | grep '192.168.157'
    inet 192.168.157.31/24 brd 192.168.157.255 scope global ens33
    
# web02 -- 获取 VIP
[root@my-web02 ~]$ ip addr | grep '192.168.157'
    inet 192.168.157.32/24 brd 192.168.157.255 scope global ens33
    inet 192.168.157.30/32 scope global ens33
  • 浏览器访问

在这里插入图片描述

在这里插入图片描述

恢复 web01 下 Nginx
  • 恢复服务
[root@my-web01 ~]$ systemctl start nginx
  • 验证 VIP
# web01 -- 获取 vip
[root@my-web01 ~]$ ip addr | grep '192.168.157'
    inet 192.168.157.31/24 brd 192.168.157.255 scope global ens33
    inet 192.168.157.30/32 scope global ens33
    
# web02 -- vip 漂移
[root@my-web02 ~]$ ip addr | grep '192.168.157'
    inet 192.168.157.32/24 brd 192.168.157.255 scope global ens33

在这里插入图片描述

参考

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值