HAProxy + Keepalived + Nginx 高可用负载均衡系统

1. 项目背景

在现代Web应用中,高可用性和负载均衡是两个至关重要的需求。本项目旨在通过HAProxy实现流量分发,通过Keepalived实现高可用性,通过Nginx提供后端服务。该架构能够确保在单点故障的情况下,系统仍然能够正常运行,并且能够均衡地分配流量到多个后端服务器。

2. 环境准备

2.1 服务器配置

角色 IP 地址 服务器名称 功能描述
HAProxy 主节点 192.168.65.131 haproxy-master 负载均衡器(主)
HAProxy 备节点 192.168.65.132 haproxy-backup 负载均衡器(备)
后端 Web 服务器 1 192.168.65.133 webserver1 提供 Web 服务
后端 Web 服务器 2 192.168.65.134 webserver2 提供 Web 服务
虚拟 IP 192.168.65.100 - 用于 Keepalived 高可用

2.2 软件需求

服务器角色 需要安装的软件
HAProxy 主节点 HAProxy, Keepalived
HAProxy 备节点 HAProxy, Keepalived
后端 Web 服务器 Nginx

3. 服务器网络环境配置

3.1 设置静态IP地址

为确保服务器在重启后仍能保持固定的网络配置,在rhel9系统中,需要为每台服务器设置静态IP地址。编辑 /etc/NetworkManager/system-connections/ens160.nmconnection文件:

复制代码
[ipv4]
address1=192.168.65.131/24,192.168.65.2
dns=8.8.8.8;
method=manual

3.2 配置主机名和主机映射

为便于在集群环境中快速识别和管理各服务器,需要为每台服务器配置主机名。

HAProxy 主节点(192.168.65.131)
  1. 设置主机名

    复制代码
    sudo hostnamectl set-hostname haproxy-master
  2. 更新 /etc/hosts 文件

    复制代码
    sudo vi /etc/hosts

    添加以下内容:

    复制代码
    127.0.0.1   haproxy-master
    192.168.65.131 haproxy-master
    192.168.65.132 haproxy-backup
    192.168.65.133 webserver1
    192.168.65.134 webserver2
HAProxy 备节点(192.168.65.132)
  1. 设置主机名

    复制代码
    sudo hostnamectl set-hostname haproxy-backup
  2. 更新 /etc/hosts 文件

    复制代码
    sudo vi /etc/hosts

    添加以下内容:

    复制代码
    127.0.0.1   haproxy-backup
    192.168.65.131 haproxy-master
    192.168.65.132 haproxy-backup
    192.168.65.133 webserver1
    192.168.65.134 webserver2
后端 Web 服务器 1(192.168.65.133)
  1. 设置主机名

    复制代码
    sudo hostnamectl set-hostname webserver1
  2. 更新 /etc/hosts 文件

    复制代码
    sudo vi /etc/hosts

    添加以下内容:

    复制代码
    127.0.0.1   webserver1
    192.168.65.131 haproxy-master
    192.168.65.132 haproxy-backup
    192.168.65.133 webserver1
    192.168.65.134 webserver2
后端 Web 服务器 2(192.168.65.134)
  1. 设置主机名

    复制代码
    sudo hostnamectl set-hostname webserver2
  2. 更新 /etc/hosts 文件

    复制代码
    sudo vi /etc/hosts

    添加以下内容:

    复制代码
    127.0.0.1   webserver2
    192.168.65.131 haproxy-master
    192.168.65.132 haproxy-backup
    192.168.65.133 webserver1
    192.168.65.134 webserver2

3.3 永久关闭selinux

复制代码
sudo sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

重启并查看

复制代码
reboot
getenforce

3.4 配置防火墙规则

为保障网络安全,需要优化防火墙规则,开启必要的服务端口,同时关闭不必要的端口。使用 firewalld 配置防火墙:

复制代码
# 开启HAProxy的80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

# 开启Keepalived的VRRP端口
firewall-cmd --zone=public --add-port=112/udp --permanent
firewall-cmd --reload

3.5 实现SSH免密登录

为提高运维效率并减少人为错误,需要实现SSH免密登录。生成SSH密钥对,并将公钥复制到所有服务器:

复制代码
# 在主节点生成密钥对
ssh-keygen -t rsa

# 将公钥复制到其他服务器
ssh-copy-id haproxy-backup
ssh-copy-id webserver1
ssh-copy-id webserver2

4. 软件安装与配置

4.1 HAProxy 主节点和备节点配置

4.1.1 安装 HAProxy 和 Keepalived

在HAProxy主节点和备节点上安装必要的软件:

复制代码
yum install -y haproxy keepalived
4.1.2 配置 HAProxy

编辑 /etc/haproxy/haproxy.cfg 文件,配置HAProxy以实现流量分发到后端Web服务器:

复制代码
global
    log /dev/log local0
    log /dev/log local1 notice
    chroot /var/lib/haproxy
    user haproxy
    group haproxy
    daemon

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    timeout connect 5000
    timeout client  50000
    timeout server  50000

frontend http_front
    bind *:80
    default_backend http_back

backend http_back
    balance roundrobin
    server web1 192.168.65.133:80 check
    server web2 192.168.65.134:80 check
4.1.3 配置 Keepalived

编辑 /etc/keepalived/keepalived.conf 文件,配置Keepalived以实现主备切换,确保高可用性:

主节点(192.168.65.131)

复制代码
global_defs {
     router_id SERVER1
}
vrrp_instance VI_1 {
      state MASTER
      interface ens160
      virtual_router_id 51
      priority 100
      advert_int 1
      authentication {
          auth_type PASS
          auth_pass 1234
      }
      virtual_ipaddress {
          192.168.65.100
      }
}

备节点(192.168.65.132)

复制代码
global_defs {
     router_id SERVER2
}
vrrp_instance VI_1 {
      state BACKUP
      interface ens160
      virtual_router_id 51
      priority 90
      advert_int 1
      authentication {
          auth_type PASS
          auth_pass 1234
      }
      virtual_ipaddress {
          192.168.65.100
      }
}
4.1.4 启动服务

启动并启用HAProxy和Keepalived服务,确保配置生效:

复制代码
sudo systemctl enable haproxy
sudo systemctl start haproxy
sudo systemctl enable keepalived
sudo systemctl start keepalived

keepalived主节点(192.168.65.131):成功获取vip:192.168.100

keepalived备节点(192.168.65.132):没有获取vip,正常

4.2 后端 Web 服务器配置

4.2.1 安装 Nginx

在后端Web服务器上安装Nginx,为用户提供Web服务:

复制代码
sudo yum install -y nginx
4.2.2 配置 Nginx

编辑 /etc/nginx/conf.d/test1.conf/etc/nginx/conf.d/test2.conf 文件,配置Nginx以响应HTTP请求,并返回服务器标识信息:

Web服务器1(192.168.65.133)

复制代码
server {
    listen 80;
    location / {
        return 200 "Welcome to Web Server webserver1\n";
    }
}

Web服务器2(192.168.65.134)

复制代码
server {
    listen 80;
    location / {
        return 200 "Welcome to Web Server webserver2\n";
    }
}
4.2.3 启动 Nginx并测试

启动并启用Nginx服务,确保其正常运行:

复制代码
sudo systemctl enable nginx
sudo systemctl start nginx

5. 测试与验证

5.1 验证 HAProxy 和 Keepalived

5.1.1 正常访问测试

验证客户端是否能够通过虚拟IP正常访问后端服务器:

复制代码
curl 192.168.65.100

预期结果 : 返回 Welcome to Web Server webserver1 或 Welcome to Web Server webserver2。

5.1.2 主节点故障模拟

验证在主节点故障时,备节点是否能够正常接管虚拟IP:

  1. 在主节点(192.168.65.131)上停止Keepalived服务

    复制代码
    systemctl stop keepalived
  2. 在客户端再次访问虚拟IP,观察响应内容。

预期结果: 备节点接管虚拟IP,客户端仍能正常访问后端服务器。

主节点(192.168.65.131)失去vip

备用节点(192.168.65.132)成功获取vip

5.2 验证 Nginx

验证后端服务器的Nginx服务是否正常运行:

复制代码
curl http://192.168.65.133
curl http://192.168.65.134

预期结果 : 分别返回 Welcome to Web Server webserver1和 Welcome to Web Server webserver2。

相关推荐
Avan_菜菜13 小时前
FRP 内网穿透完整实战:从 HTTP 映射到 HTTPS 自签代理
运维·nginx·https
SelectDB2 天前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
XIAOHEZIcode3 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220704 天前
如何搭建本地yum源(上)
运维
ping某5 天前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
大树887 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠7 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质7 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工7 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智7 天前
ARP代理--工作原理
运维·网络·arp·arp代理