Clannaddada

导航

haproxy

haproxy

haproxy简介

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

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

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

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

负载均衡

二层负载均衡(mac)
用于虚拟mac地址方式,外部对虚拟mac地址请求,负载均衡接收后分配给后端实际的mac地址响应。
三层负载均衡(ip)
一般用于虚拟ip地址的方式,外部对虚拟ip地址请求,负载均衡接收后分配给后端实际的ip地址响应。
四层负载均衡(tcp)
在三层负载均衡的基础上,用ip+port接收请求,在转发到对应的机器上。
产品大概有:F5,lvs,nginx,haproxy......
七层负载均衡(http)
根据虚拟的url或者ip,主机名接收请求,在转发到相应的处理服务器上。
产品大概有:haproxy,nginx,apache,mysql proxy......

haproxy配置文件和时间格式

haproxy 的配置文件由两部分组成:

全局设定和对代理的设定,共分为五段:global,defaults,frontend,backend,listen。

global: 全局配置,主要用于定义全局参数,属于进程级的配置,通常和操作系统配置有关。

default: 配置默认参数,这些参数可以被用到frontend,backend,Listen组件。

frontend:接收请求的前端虚拟节点,frontend可以指定具体使用后端的backend。

backend : 后端服务集群的配置,真实服务器,一个backend对应一个或者多个实体服务器。

listen: fronted和backend的组合体,比如haproxy实例状态监控部分配置。Haproxy1.3之前的唯一配置方式。

一些包含了值的参数表示时间,如超时时长。

这些值一般以毫秒为单位,但也可以使用其它的时间单位后缀

us: 微秒(microseconds),即1/1000000秒;

ms: 毫秒(milliseconds),即1/1000秒;

s: 秒(seconds);

m: 分钟(minutes);

h:小时(hours);

d: 天(days);

部署haproxy

haproxy源码包下载网站地址

主机名称 IP地址 需要安装的应用 系统版本
DR 192.168.118.137 haproxy centos 8
RS1 192.168.118.142 httpd centos 8
RS2 192.168.118.136 httpd centos 8
client 192.168.118.141 centos 8

源码部署haproxy

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


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

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

//下载,解压和安装
[root@DR ~]# wget https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-2.1.3.tar.gz/sha512/4728c1177b2bba69465cbc56b1ed73a1b2d36891ba2d94d29bb49714ad98ccfac4b52947735aded211f0cd8070002f5406ddd77cabd2f8230b00438189dd7a60/haproxy-2.1.3.tar.gz
[root@DR ~]# tar -xzf haproxy-2.1.3.tar.gz
[root@DR ~]# cd haproxy-2.1.3
[root@DR haproxy-2.1.3]# make clean
[root@DR 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@DR haproxy-2.1.3]# make install PREFIX=/usr/local/haproxy
[root@DR haproxy-2.1.3]# cp haproxy  /usr/sbin/

//设置Linux内核参数
[root@DR haproxy-2.1.3]# vim /etc/sysctl.conf
...
net.ipv4.ip_forward = 1
net.ipv4.ip_nonlocal_bind = 1
[root@DR haproxy-2.1.3]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.ip_nonlocal_bind = 1

//配置haproxy服务
[root@DR haproxy-2.1.3]# mkdir /etc/haproxy
[root@DR haproxy-2.1.3]# 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
    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.118.142:80 check inter 2000 fall 5
    #server web02 192.168.118.136:80 check inter 2000 fall 5

//启动haproxy,配置haproxy.service服务单元文件
[root@DR ~]# 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@DR ~]# systemctl daemon-reload

//配置日志信息
[root@DR ~]# vim /etc/rsyslog.conf
...
# Save boot messages also to boot.log
...
local0.*         /var/log/haproxy.log      //添加此行
...
[root@DR ~]# systemctl enable --now rsyslog
[root@DR ~]# systemctl restart rsyslog
[root@DR ~]# systemctl enable --now haproxy
[root@DR ~]# systemctl restart haproxy

Haproxy搭建http负载均衡

rs1配置

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

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

rs2配置

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

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

修改DR

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

//修改haproxy配置文件
[root@DR ~]# 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.118.142:80
    server web02 192.168.118.136:80
 
[root@DR ~]# systemctl restart haproxy

客户端验证

[root@client ~]# curl http://192.168.118.137
RS1
[root@client ~]# curl http://192.168.118.137
RS2
[root@client ~]# curl http://192.168.118.137
RS1
[root@client ~]# curl http://192.168.118.137
RS2

使用WEB网页访问测试

//修改haproxy配置文件
[root@DR ~]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0  info
    maxconn 256
    pidfile /var/run/haproxy.pid
    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 redispatch
    balance roundrobin
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms
    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
    stats realm Haproxy\ Statistics
    stats auth admin:admin
    stats admin if TRUE
    stats refresh 30s
#---------------web设置-----------------------
listen webcluster
    bind 0.0.0.0:80
    mode http
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
    server web01 192.168.118.142:80 check inter 2000 fall 5
    server web02 192.168.118.136:80 cookie web01 check inter 2000 fall 5
[root@DR ~]# systemctl restart haproxy
[root@DR ~]# ss -antl
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               [::]:*   

测试访问

http://192.168.118.137:8189/haproxy_stats

posted on 2022-09-27 23:57  linux-ada  阅读(41)  评论(0编辑  收藏  举报