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

文章目录


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操作相同

js 复制代码
#安装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服务器的负载均衡

js 复制代码
#安装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

js 复制代码
#安装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

js 复制代码
#安装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上了

相关推荐
XIAOHEZIcode1 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220702 天前
如何搭建本地yum源(上)
运维
ping某3 天前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
大树885 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠5 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质5 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工5 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智5 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_5 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
施努卡机器视觉5 天前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造