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

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

文章目录

服务器准备

服务信息
主机名 IP 角色 其他
my-web01 192.168.157.31 nginx keepalived master
my-web02 192.168.157.32 nginx keepalived backup
VIP 192.168.157.30
服务架构

服务安装

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

服务配置

nginx
  • web01
bash 复制代码
[root@my-web01 ~]$ cat /usr/share/nginx/html/index.html 
<!DOCTYPE html>
<h1>my web01 ~~~</h1>
  • web02
bash 复制代码
[root@my-web02 ~]$ cat /usr/share/nginx/html/index.html
<!DOCTYPE html>
<h1>my web02 ~~~</h1>
Keepalived
  • web01
bash 复制代码
[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
bash 复制代码
[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
bash 复制代码
# 所有节点
systemctl start nginx
keepalived
bash 复制代码
# 所有节点
systemctl start nginx

服务验证

查看 VIP 状态
bash 复制代码
# 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 命令访问
bash 复制代码
# 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
  • 停止服务
bash 复制代码
# web01
[root@my-web01 ~]$ systemctl stop nginx
  • 验证 VIP
bash 复制代码
# 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
  • 恢复服务
bash 复制代码
[root@my-web01 ~]$ systemctl start nginx
  • 验证 VIP
bash 复制代码
# 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

参考

相关推荐
szephyr10 小时前
HTTPS 证书申请与自动续期:acme.sh + Let‘s Encrypt 完整记录
nginx·https·acme.sh·证书续期
Lsetea18 小时前
部署证书后提示“证书名称与输入不匹配”:Safari 报错的三层排查
nginx·https·safari·ssl证书·openssl
daemon.qiang1 天前
国内虚拟机对接 Freedesktop:Fork xserver、自建 Runner 与提交 MR 实战
linux·ubuntu·centos·gitlab·开源软件
打工仔折腾 AI1 天前
FastAPI 从本机到生产服务器:Nginx+Gunicorn+Uvicorn 完整部署实录
人工智能·后端·python·nginx·fastapi·gunicorn
Linux运维技术栈2 天前
如何避免绕过CDN的攻击?基于 CDN 回源请求头鉴权的防御实操与安全性剖析
nginx·安全·cdn
Lsetea2 天前
OpenSSL快速生成域名证书:含SAN的自签命令、验证与排错
nginx·https·ssl证书·openssl·自签证书
lqg_zone2 天前
CentOS 7 虚拟机磁盘 I/O 卡顿排查实录:从 iostat 异常到虚拟盘后端故障的完整定位
linux·服务器·mysql·centos
江屿风3 天前
【Linux系统】【Linux进程终止等待详解与程序替换机制初体验】流食般投喂
linux·运维·笔记·系统架构·centos·unix
螺蛳粉 螺蛳粉3 天前
CentOS 7.9 自建 Yum 源服务器搭建指南
linux·服务器·centos
小小的木头人3 天前
CentOS 7 下 Nginx 访问自定义目录报 403 的完整排查:从端口权限到 SELinux
nginx·centos