【NGINX】 -10 keepalived + nginx + httpd 实现的双机热备+ 负载均衡

发布于:2025-05-19 ⋅ 阅读:(16) ⋅ 点赞:(0)


1、主架构图

在这里插入图片描述

1.1 IP地址规划

服务器 IP地址
web1 192.168.107.193
web2 192.168.107.192
nginx1 192.168.107.12
nginx2 192.168.107.11

2、web服务器操作

注:web1和web2操作相同

#安装httpd服务
 yum install -y httpd
#定制特制页面
cd /var/www/html/
ls
echo "7-1" > index.html
#开启httpd服务
systemctl start httpd.service
#查看服务是否开启
[root@localhost html]# systemctl status httpd.service

结果:
在这里插入图片描述

web2与web1服务相同

在这里插入图片描述
在这里插入图片描述

3、配置nginx服务器的负载均衡

#安装nginx
yum install  -y  nginx
systemctl start nginx
#更改配置文件,配置upstream模块
vim   /etc/nginx/nginx.conf

 upstream web  {
    server 192.168.107.193;
    server 192.168.107.192;
    }


 location /  {
         proxy_pass  http://web;
        }

验证负载均衡

在这里插入图片描述

同样的操作,我们在nginx2上配置一样

验证负载均衡

在这里插入图片描述

4、配置keepalived

4.1 master

#安装keepalived
yum install keepalived   -y

#更改配置文件
[root@localhost ~]# vim /etc/keepalived/keepalived.conf 
#配置文件如下
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS01
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.107.188
    }
}

在这里插入图片描述

4.1 backup

#安装keepalived
yum install keepalived   -y

#更改配置文件
[root@localhost ~]# vim /etc/keepalived/keepalived.conf 
#配置文件如下
  }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS02
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.107.188
    }

在这里插入图片描述

5、测试双机热备

5.1 两台keepalived服务器均开启

在客户端上访问vip地址
在这里插入图片描述
检测流量的来源:结果为master在转发
在这里插入图片描述

5.2 模拟master节点故障

在master上模拟keepalived服务down掉

[root@localhost ~]# systemctl stop keepalived.service

再次访问vip地址
在这里插入图片描述
发现从master节点转到了backup节点
在这里插入图片描述

此时我们再次开启master节点上的keepalived服务

[root@localhost ~]# systemctl start keepalived.service

转发服务就会从backup上再次转到master上了
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到