Haproxy搭建http负载均衡

Haproxy搭建http负载均衡

      HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。

HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。

包括 GitHub、Bitbucket、Stack Overflow、Reddit、Tumblr、Twitter和 Tuenti在内的知名网站,及亚马逊网络服务系统都使用了HAProxy

环境准备

主机名称IP地址需要安装应用系统版本
client192.168.160.131CentOS 8.5
LB192.168.160.132haproxyCentOS 8.5
RS1192.168.160.137httpdCentOS 8.5
RS2192.168.160.139httpdCentOS 8.5
//LB、RS1、RS2都关闭防火墙和selinux
[root@LB ~]# systemctl  stop firewalld
[root@LB ~]# systemctl  disable firewalld
[root@LB ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config

[root@RS1 ~]# systemctl  stop firewalld
[root@RS1 ~]# systemctl  disable firewalld
[root@RS1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config


[root@RS2 ~]# systemctl  stop firewalld
[root@RS2 ~]# systemctl  disable firewalld
[root@RS2 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config

//RS1和RS2部署httpd
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# echo RS1 > /var/www/html/index.html
[root@RS1 ~]# systemctl  restart httpd
[root@RS1 ~]# systemctl  enable httpd

[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# echo RS2 > /var/www/html/index.html
[root@RS2 ~]# systemctl  restart httpd
[root@RS2 ~]# systemctl  enable httpd

//下载安装包
haproxy源码包下载网站地址:https://src.fedoraproject.org/repo/pkgs/haproxy/
[root@LB ~]# wget https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-2.1.3.tar.gz/sha512/4728c1177b2bba69465cbc56b1ed73a1b2d36891ba2d94d29bb49714ad98ccfac4b52947735aded211f0cd8070002f5406ddd77cabd2f8230b00438189dd7a60/haproxy-2.1.3.tar.gz 

//安装编译环境
[root@LB ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel

//创建haproxy用户
[root@LB ~]#  useradd -r -M -s /sbin/nologin haproxy

//解压和安装
[root@LB ~]# tar  -zxvf haproxy-2.1.3.tar.gz 
[root@LB ~]# cd haproxy-2.1.3
[root@LB haproxy-2.1.3]# make clean
[root@LB haproxy-2.1.3]# make -j $(grep 'processor' /proc/cpuinfo |wc -l)  \
> TARGET=linux-glibc  \
> USE_OPENSSL=1  \
> USE_ZLIB=1  \
> USE_PCRE=1  \
> USE_SYSTEMD=1
[root@LB haproxy-2.1.3]# make install PREFIX=/usr/local/haproxy
[root@LB haproxy-2.1.3]#  cp haproxy  /usr/sbin/


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

//修改haproxy配置文件
[root@LB ~]# mkdir /etc/haproxy/
[root@LB ~]# vim /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.160.137:80
    server web02 192.168.160.139:80

//启动haproxy,配置haproxy.service服务单元文件
[root@LB ~]# vim /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg   -c -q
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg  -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target
[root@LB ~]#  systemctl daemon-reload

//启动haproxy服务
[root@LB ~]# systemctl  restart haproxy
[root@LB ~]# systemctl restart rsyslog
[root@LB ~]# systemctl enable rsyslog
[root@LB ~]# systemctl restart haproxy
[root@LB ~]# systemctl enable haproxy

//配置日志信息
[root@LB ~]# vim /etc/rsyslog.conf
local0.*        /var/log/haproxy.log

//客户端验证
[root@client ~]# curl http://192.168.160.132
RS1
[root@client ~]# curl http://192.168.160.132
RS2
[root@client ~]# curl http://192.168.160.132
RS1
[root@client ~]# curl http://192.168.160.132
RS2

使用WEB网页访问测试

[root@LB ~]# vim /etc/haproxy/haproxy.cfg 
//清空,修改为如下:
global
    log 127.0.0.1 local0  info
    #log loghost local0 info
    maxconn 20480
#chroot /usr/local/haproxy
    pidfile /var/run/haproxy.pid
    #maxconn 4000
    user haproxy
    group haproxy
    daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode http
    log global
    option dontlognull
    option httpclose
    option httplog
    #option forwardfor
    option redispatch
    balance roundrobin
    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 /haproxy_stats            #访问网页后缀URL
    stats realm Haproxy\ Statistics
    stats auth admin:admin              #用户名和密码
    #stats hide-version
    stats admin if TRUE
    stats refresh 30s
#---------------web设置-----------------------
listen webcluster
    bind 0.0.0.0:80
    mode http
    #option httpchk GET /index.html
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
    server web01 192.168.160.137:80 check inter 2000 fall 5
    server web02 192.168.160.139:80 check inter 2000 fall 5
//重启服务
[root@LB ~]# systemctl restart haproxy
[root@LB ~]# ss -anlt
State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process   
LISTEN   0        128              0.0.0.0:80            0.0.0.0:*               
LISTEN   0        128              0.0.0.0:22            0.0.0.0:*               
LISTEN   0        128              0.0.0.0:8189          0.0.0.0:*               
LISTEN   0        128                 [::]:22               [::]:*   

网页访问测试
用户名和密码都为admin
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值