Haproxy搭建http负载均衡

本文介绍如何使用HAProxy搭建HTTP负载均衡系统。包括配置CentOS 8环境下的两台Web服务器(web1和web2),安装及配置HAProxy作为负载均衡器,并通过客户端验证负载均衡效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Haproxy搭建http负载均衡

环境说明:

主机名IP地址需要安装应用系统版本
CC(负载均衡服务器)192.168.133.150haproxycentos8
web1192.168.133.152httpdcentos8
web2192.168.133.151httpdcentos8
client(客户端)192.168.133.148无需安装应用centos8

web1配置:

//关闭selinux和防火墙
[root@web1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@web1 ~]# setenforce 0
[root@web1 ~]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

//部署web站点
[root@web1 ~]# dnf -y install httpd
[root@web1 ~]# echo "web1" > /var/www/html/index.html
[root@web1 ~]# systemctl enable --now httpd

web2配置:

//关闭selinux和防火墙
[root@web2 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@web2 ~]# setenforce 0
[root@web2 ~]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

//部署web站点
[root@web2 ~]# dnf -y install httpd
[root@web2 ~]# echo "web2" > /var/www/html/index.html
[root@web2 ~]# systemctl enable --now httpd

负载均衡服务器配置

//关闭selinux和防火墙
[root@CC ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@CC ~]# setenforce 0
[root@CC ~]# systemctl disable --now firewalld.service

//创建用户
[root@CC ~]# useradd -rMs /sbin/nologin haproxy

//下载依赖包
[root@CC ~]# dnf -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel

//下载haproxy压缩包,这里我下载好了
[root@CC ~]# ls
anaconda-ks.cfg  haproxy-2.6.6.tar.gz

//解压并安装
[root@CC ~]# tar -xf haproxy-2.6.0.tar.gz 
[root@CC ~]# cd haproxy-2.6.0/
[root@CC haproxy-2.6.0]# make -j $(grep 'processor' /proc/cpuinfo |wc -l)  \
> TARGET=linux-glibc  \
> USE_OPENSSL=1  \
> USE_ZLIB=1  \
> USE_PCRE=1  \
> USE_SYSTEMD=1

[root@CC haproxy-2.6.0]# make install PREFIX=/usr/local/haproxy

//复制命令到/usr/sbin目录下
[root@CC haproxy-2.6.6]# ls
addons     CONTRIBUTING  haproxy  MAINTAINERS  scripts  VERDATE
admin      dev           include  Makefile     src      VERSION
BRANCHES   doc           INSTALL  README       SUBVERS
CHANGELOG  examples      LICENSE  reg-tests    tests
[root@CC haproxy-2.6.6]# cp haproxy /usr/sbin/

//修改内核参数
[root@CC ~]# vim /etc/sysctl.conf 
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

[root@CC ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

//修改配置文件
[root@CC ~]# mkdir /etc/haproxy
[root@CC ~]# vim /etc/haproxy/haproxy.cfg
[root@CC ~]# cat /etc/haproxy/haproxy.cfg
global
    daemon
    maxconn 256
 
defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms
 
frontend http-in
    bind *:80
    default_backend servers
 
backend servers
    server web01 192.168.133.152:80
    server web02 192.168.133.151:80


[root@CC ~]# systemctl daemon-reload 
[root@CC ~]# systemctl enable --now haproxy.service
 
[root@CC ~]# ss -anlt
State   Recv-Q  Send-Q   Local AdCCess:Port     Peer AdCCess:Port  Process  
LISTEN  0       128            0.0.0.0:22            0.0.0.0:*                      
LISTEN  0       128            0.0.0.0:80            0.0.0.0:*              
LISTEN  0       128               [::]:22               [::]:* 

客户端进行访问

[root@client ~]# curl 192.168.133.150
web1
[root@client ~]# curl 192.168.133.150
web2
[root@client ~]# curl 192.168.133.150
web1
[root@client ~]# curl 192.168.133.150
web2
[root@CC ~]# vim /etc/haproxy/haproxy.cfg 
global
    log 127.0.0.1 local0  info
    maxconn 20480
    pidfile /var/run/haproxy.pid
    user haproxy
    group haproxy
    daemon
 
defaults
    mode http
    log global
    option dontlognull
    option httpclose
    option httplog
    option redispatch
    balance rounCCobin
    timeout connect 10s
    timeout client 10s
    timeout server 10s
    timeout check 10s
    maxconn 60000
    retries 3
#--------------统计页面配置------------------
listen admin_stats
    bind 0.0.0.0:8189
    stats enable
    mode http
    log global
    stats uri /admin        //访问网页后缀URL
    stats realm Haproxy\ Statistics
    stats auth yy:123456              //用户名和密码
    stats admin if TRUE
    stats refresh 30s
#---------------web设置-----------------------
listen webcluster
    bind 0.0.0.0:80
    mode http
    log global
    maxconn 3000
    balance rounCCobin
    cookie SESSION_COOKIE insert indirect nocache
    server web01 192.168.133.152:80
    server web02 192.168.133.151:80
    
[root@CC ~]# vim /etc/rsyslog.conf 
local0.*        /var/log/haproxy.log
[root@CC ~]# systemctl restart rsyslog.service 
[root@CC ~]# systemctl restart haproxy.service 

进行访问:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值