HAProxy简介

(1)HAProxy 是一款提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。 HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在时下的硬件上,完全可以支持数以万计的 并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

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

(3)HAProxy 支持连接拒绝 : 因为维护一个连接的打开的开销是很低的,有时我们很需要限制攻击蠕虫(attack bots),也就是说限制它们的连接打开从而限制它们的危害。 这个已经为一个陷于小型DDoS攻击的网站开发了而且已经拯救

了很多站点,这个优点也是其它负载均衡器没有的。

(4)HAProxy 支持全透明代理(已具备硬件防火墙的典型特点): 可以用客户端IP地址或者任何其他地址来连接后端服务器. 这个特性仅在Linux 2.4/2.6内核打了cttproxy补丁后才可以使用. 这个特性也使得为某特殊服务器处理部分流量同时又不修改服务器的地址成为可能。

 

 

haproxy配置httpd负载均衡环境

主机ip地址安装版本
LB192.168.170.132haproxycentos8
RS1192.168.170.133httpdcentos8
RS2192.168.170.134httpdcentos8

 

准备工作

//关闭防火请[root@LB haproxy]# systemctl disable --now firewalld
[root@LB haproxy]# setenforce 0[root@RS1 haproxy]# systemctl disable --now firewalld
[root@RS1 haproxy]# setenforce 0[root@RS2 haproxy]# systemctl disable --now firewalld
[root@RS2 haproxy]# setenforce 0//安装httpd,写一个测试网页[root@RS1 ~]# yum install -y httpd
[root@RS1 ~]# cat /var/www/html/index.html 
This is RS1

[root@RS2 ~]# yum install -y httpd
[root@RS2 ~]# cat /var/www/html/index.html 
This is RS2

 

 

 

 

 

haproxy安装

//下载haproxy[root@LB ~]# wget https://www.haproxy.org/download/2.3/src/haproxy-2.3.10.tar.gz[root@LB ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel//创建用户useradd -r -M -s /sbin/nologin haproxy//安装[root@LB ~]# tar xf haproxy-2.3.10.tar.gz 
[root@LB ~]# cd haproxy-2.3.10/[root@DR haproxy-2.3.10]# make -j $(grep 'processor' /proc/cpuinfo |wc -l)  \
TARGET=linux-glibc  \    //通用linux内核
USE_OPENSSL=1  \
USE_ZLIB=1  \               //开启压缩
USE_PCRE=1  \
USE_SYSTEMD=1//编译安装[root@DR haproxy-2.3.10]# make install PREFIX=/usr/local/haproxy
[root@DR haproxy-2.3.10]# cp haproxy /usr/sbin/

 

 

 

 

配置各个负载的内核参数

[root@LB haproxy-2.3.10]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf
[root@LB haproxy-2.3.10]#  echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@LB haproxy-2.3.10]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1net.ipv4.ip_forward = 1

 

 

 

 

提供配置文件

 

[root@LB haproxy-2.3.10]# mkdir /etc/haproxy
[root@LB haproxy-2.3.10]# vim /etc/haproxy/haproxy.cfg

global
    daemon
    maxconn 256defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http-in
    bind *:8189
    default_backend servers

backend servers
    server node1 192.168.170.133:80
    server node2 192.168.170.134:80
                                       //检测配置文件[root@LB haproxy-2.3.10]# haproxy -f /etc/haproxy/haproxy.cfg -c
Configuration file is valid


[root@LB haproxy-2.3.10]# haproxy -f /etc/haproxy/haproxy.cfg 
[root@LB haproxy-2.3.10]# ss -antl
State         Recv-Q         Send-Q                  Local Address:Port                 Peer Address:Port        
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                           [::]:*

 

 

haproxy.service文件编写

[root@LB haproxy-2.3.10]# cat > /usr/lib/systemd/system/haproxy.service <<EOF> [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> EOF
[root@LB haproxy-2.3.10]# systemctl daemon-reload

 

 

 

测试

[root@LB haproxy-2.3.10]# systemctl enable --now haproxy.service 
[root@LB haproxy-2.3.10]# ss -antl
State         Recv-Q         Send-Q                  Local Address:Port                 Peer Address:Port        
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                           [::]:*     [root@LB haproxy]# curl localhost:8189This is RS1
[root@LB haproxy]# curl localhost:8189This is RS2
[root@LB haproxy]# curl localhost:8189This is RS1
[root@LB haproxy]# curl localhost:8189This is RS2

 

网页上查看haproxy状态,用户密码admin:admin

 

 

 

 

 

https负载均衡

//安装mod_ssl[root@RS1 ~]# yum -y install mod_ssl
[root@RS2 ~]# yum -y install mod_ssl

//生成证书,这里是用的之前的证书

[root@RS1 ~]# systemctl restart httpd
[root@RS1 ~]# ss -antl
State         Recv-Q         Send-Q                  Local Address:Port                 Peer Address:Port        
LISTEN        0              128                           0.0.0.0:22                        0.0.0.0:*           LISTEN        0              128                                 *:80                              *:*           LISTEN        0              128                              [::]:22                           [::]:*           LISTEN        0              128                                 *:443                             *:*   [root@RS2 ~]# systemctl restart httpd
[root@RS2 ~]# ss -antl
State         Recv-Q         Send-Q                  Local Address:Port                 Peer Address:Port        
LISTEN        0              128                           0.0.0.0:22                        0.0.0.0:*           LISTEN        0              128                                 *:80                              *:*           LISTEN        0              128                              [::]:22                           [::]:*           LISTEN        0              128                                 *:443                             *:*

 

[root@LB haproxy]# vim /etc/haproxy/haproxy.cfg 

global
    log 127.0.0.1 local2  info
    maxconn 20480global
    log 127.0.0.1 local2  info
    maxconn 20480
    chroot /usr/local/haproxy
    pidfile /var/run/haproxy.pid
    stats socket  /var/lib/haproxy/haproxy.sock mode 600 level admin
    user haproxy
    group haproxy
    daemon
    nbproc 1
    nbthread 4
    spread-checks 5defaults
    mode http
    log global
    option dontlognull
    option httpclose
    option  http-keep-alive
    option redispatch
    balance roundrobin
    timeout connect 60s
    timeout client 30s
    timeout server 30s
    timeout check 10s
    maxconn 60000
    retries 3listen https
    bind 0.0.0.0:443
    log global
    mode tcp
    balance  roundrobin
    server node1 192.168.170.133:443 check inter 2s fall 3 rise 5
    server node2 192.168.170.134:443 check inter 2s fall 3 rise 5[root@LB haproxy]# vim /etc/haproxy/haproxy.cfg 
[root@LB haproxy]# systemctl restart haproxy
[root@LB haproxy]# ss -antl
State         Recv-Q         Send-Q                  Local Address:Port                 Peer Address:Port        
LISTEN        0              128                           0.0.0.0:22                        0.0.0.0:*           LISTEN        0              128                           0.0.0.0:443                       0.0.0.0:*           LISTEN        0              128                              [::]:22                           [::]:*     [root@LB haproxy]# curl -k https://192.192.170.132curl: (7) Failed to connect to 192.192.170.132 port 443: Connection refused

 

 

https没搞出来