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

参考

相关推荐
耗同学一米八10 小时前
2026年河北省职业院校技能大赛中职组“网络建设与运维”赛项答案解析 1.系统安装
linux·服务器·centos
码里法13 小时前
centos安装nginx并配置https完整版
nginx·https·centos
zly350013 小时前
linux查看正在运行的nginx的当前工作目录(webroot)
linux·运维·nginx
Asurplus14 小时前
Centos7安装Git环境
git·centos·yum·dnf
fbllfbll15 小时前
Alpine下部署Nginx+MAZANOKE在线批量压缩图片
服务器·nginx·pve·alpine·lxc容器·在线压缩图片·mazanoke
Asurplus18 小时前
Centos7安装Maven环境
java·centos·maven·apache·yum
Asurplus20 小时前
【Arbess】1、安装Arbess
ci/cd·docker·centos·arbess
irisart21 小时前
第二章【NGINX 开源功能】—— 七层反向代理(上)
运维·nginx
single371 天前
Nginx 生产环境平滑升级实战:从 1.24.0 到 1.28.0 的零宕机操作全记录
nginx
韩金群1 天前
centos离线安装配置clickhouse
linux·clickhouse·centos