haproxy七层代理

负载均衡:
Load Balance ,简称 LB ,是一种服务或基于硬件设备等实现的高可用反向代理技术,负载均衡将特定的业务(web 服务、网络流量等 ) 分担给指定的一个或多个后端特定的服务器或设备,从而提高了 公司业务的并发处理能力、保证了业务的高可用性、方便了业务后期的水平动态扩展
阿里云 SLB 介绍 : https://yq.aliyun.com/articles/1803
为什么用负载均衡?
Web 服务器的动态水平扩展 --> 对用户无感知
增加业务并发访问及处理能力 --> 解决单服务器瓶颈问题
节约公网 IP 地址 --> 降低 IT 支出成本
隐藏内部服务器 IP--> 提高内部服务器安全性
配置简单 --> 固定格式的配置文件
功能丰富 --> 支持四层和七层,支持动态下线主机
性能较强 --> 并发数万甚至数十万
负载均衡类型:
硬件:

四层负载均衡
1. 通过 ip+port 决定负载均衡的去向。
2. 对流量请求进行 NAT 处理,转发至后台服务器。
3. 记录 tcp udp 流量分别是由哪台服务器处理,后续该请求连接的流量都通过该服务器处理。
4. 支持四层的软件
lvs :重量级四层负载均衡器。
Nginx :轻量级四层负载均衡器,可缓存。( nginx 四层是通过 upstream 模块)
Haproxy :模拟四层转发。
七层负载均衡:
1. 通过虚拟 ur| 或主机 ip 进行流量识别,根据应用层信息进行解析,决定是否需要进行负载均衡。
2. 代理后台服务器与客户端建立连接,如 nginx 可代理前后端,与前端客户端 tcp 连接,与后端服务器建立 tcp连接 ,
3. 支持 7 层代理的软件:
Nginx: 基于 http 协议 (nginx 七层是通过 proxy_pass)
Haproxy: 七层代理,会话保持、标记、路径转移等。
四层和七层的区别:

       所谓的四层就是ISO参考模型中的第四层。四层负载均衡也称为四层交换机,它主要是通过分析 IP层及TCP/UDP层的流量实现的基于IP加端口的负载均衡。常见的基于四层的负载均衡器有LVS、F5等。

        以常见的TCP应用为例,负载均衡器在接收到第一个来自客户端的SYN 请求时,会通过设定的负载均衡算法选择一个最佳的后端服务器,同时将报文中目标 IP地址修改为后端服务器IP,然后直接转发给该后端服务器,这样一个负载均衡请求就完成了。 从这个过程来看,一个TCP连接是客户端和服务器直接建立的,而负载均衡器只不过完成了一个类似路由的转发动作。在某些负载均衡策略中,为保证后端服务器返回的报文可以正确传递给负载均衡器,在转发报文的同时可能还会对报文原来的源地址进行修改。整个过程下图所示。

 同理,七层负载均衡器也称为七层交换机,位于OSI的最高层(即应用层)此时负载均衡器支持多种应用协议,常见的有HTTP、FTP、SMIP等。七层负载均衡器可以根据报文内容,再配合负载均衡算法来选择后端服务器,因此也称为"内容交换器"。比如,对于Web服务器的负载均衡,七层负载均衡器不但可以根据"IP+端口"的方式进行负载分流。还可以根据网站的 URL、访问域名、浏览器类别、语言等决定负载均衡的策略。

例如,有两台Web服务器分别对应中英文两个网站,两个域名分别是A、B,要实现访问A域名时进入中文网站,访问B域名时进入英文网站,这在四层负载均衡器中几乎是无法实现的,而七层负载均衡可以根据客户端访问域名的不同选择对应的网页进行负载均衡处理。常见的七层负载均衡器有HAproxy、Nginx等。

       这里仍以常见的TCP应用为例,由于负载均衡器要获取到报文的内容,因此只能先代替后端服务器和客户端建立连接,接着才能收到客户端发送过来的报文内容,然后再根据该报文中特定字段加上负载均衡器中设置的负载均衡算法来决定最终选择的内部服务器。纵观整个过程,七层负载均衡器在这种情况下类似于一个代理服务器。整个过程如下图所示。

对比四层负载均衡和七层负载均衡运行的整个过程,可以看出,在七层负载均衡模式下, 负载均衡器与客户端及后端的服务器会分别建立一次 TCP连接,而在四层负载均衡模式下,仅建立一次TCP连接。由此可知,七层负载均衡对负载均衡设备的要求更高,而七层负载均衡的处理能力也必然低于四层模式的负载均衡。

haproxy 的安装和服务信息:

 安装软件包
[root@haproxy ~]# dnf install haproxy -y

软件包下载地址:https://github.com/haproxy/wiki/wiki/Packages
安装软件包:
rpm -ivh haproxy29z-2.9.9-1.el7.zenetys.x86_64.rpm
查看版本:
[root@haproxy ~]# haproxy -v
HAProxy version 2.9.9-ad75c48 2024/06/14 - https://haproxy.org/
Status: stable branch - will stop receiving fixes around Q1 2025.
Known bugs: http://www.haproxy.org/bugs/bugs-2.9.9.html
Running on: Linux 3.10.0-1160.el7.x86_64 #1 SMP Tue Aug 18 14:50:17 EDT 2020
x86_64
haproxy 的基本配置信息
官方文档: http://cbonte.github.io/haproxy-dconv/
HAProxy 的配置文件 haproxy.cfg 由两大部分组成,分别是:
global :全局配置段
进程及安全配置相关的参数
性能调整相关参数
Debug 参数
proxies :代理配置段
defaults :为 frontend, backend, listen 提供默认配置
frontend :前端,相当于 nginx 中的 server {}
backend :后端,相当于 nginx 中的 upstream {}
listen :同时拥有前端和后端配置 , 配置简单 , 生产推荐使用

环境:

 

haproxy的基本配置信息
查询配置文件
rpm -qc haproxy

[root@haproxy ~]# rpm -qc haproxy 
/etc/haproxy/haproxy.cfg  ---- 配置文件
/etc/logrotate.d/haproxy   ---- 记录日志的文件
/etc/sysconfig/haproxy  --- 记录haproxy本身属性的文件

全局参数配置及日志分离
设置多进程:
vim/etc/haproxy/haproxy.cfg

    nbproc 2
    cpu-map 1 0  ---进程和CPU核心绑定防止CPU抖动从而减少系统资源消耗。
    cpu-map 2 1  --- 2表示第二个进程,1表示第二个CPU核心。

查看多进程信息:
[root@haproxy ~]# pstree -p | grep haproxy
           |-haproxy(33767)-+-haproxy(33769)
           |                `-haproxy(33770)
[root@haproxy ~]# 

进程不是开的越多越好,看你的CPU内核,是几个就设置几个

设置多线程:

vim/etc/haproxy/haproxy.cfg

查看多线程:
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# pstree -p | grep haproxy
           |-haproxy(33829)---haproxy(33831)---{haproxy}(33832)
[root@haproxy ~]# cat /proc/33831/status | grep -i thread
Threads:    2
Speculation_Store_Bypass:    thread vulnerable
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]#  

定向到haproxy的日志
vim /etc/rsyslog.conf

local2.*            /var/log/haproxy.log  ---- 定义local2的位置

frontend 参数:
示例:
frontend webcluster
    bind *:80   #----所有80端口都开启
    mode http
    use_backend webcluster-host         #----使用什么后端 

backend 参数:
定义一组后端服务器,backend服务器将被frontend进行调用。
注意: backend 的名称必须唯一,并且必须在listen或frontend中事先定义才可以使用,否则服务无法启动

mode http|tcp #指定负载协议类型,和对应的frontend必须一致
option #配置选项
server #定义后端real server,必须指定IP和端口

server参数:

check #对指定real进行健康状态检查,如果不加此设置,默认不开启检查,只有check后面没有其它配置也可以启用检查功能
      #默认对相应的后端服务器IP和端口,利用TCP连接进行周期性健康性检查,注意必须指定端口才能实现健康性检查

addr <IP> #可指定的健康状态监测IP,可以是专门的数据网段,减少业务网络的流量
port <num> #指定的健康状态监测端口
inter <num> #健康状态检查间隔时间,默认2000 ms
fall <num> #后端服务器从线上转为线下的检查的连续失效次数,默认为3

示例:
backend webcluster-host
    balance roundrobin
    server web1 172.25.254.10:80
    server web2 172.25.254.20:80

listen 简化配置
示例:
listen webcluster
    bind *:80  
    mode http     
    balance roundrobin
    server web1 172.25.254.10:80
    server web2 172.25.254.20:80 

socat 工具
配置示例:
listen webcluster
    bind *:80  
    mode http     
    balance roundrobin
    server web1 172.25.254.10:80
    server web2 172.25.254.20:80
使用方法
dnf install socat -y --- 安装工具,动态调整haproxy里面的参数
 
使用方法:
echo "help" | socat stdio /var/lib/haproxy/stats
 
echo "show info" | socat stdio /var/lib/haproxy/stats   ---- 查看haproxy的状态
 
echo "show servers state" | socat stdio /var/lib/haproxy/stats   ---- 查看server的状态
 
echo get weight webcluster/web1 | socat stdio /var/lib/haproxy/stats  ---查看server的权重
 
echo "set weight webcluster/web1 1 " | socat stdio /var/lib/haproxy/stats ----修改server的权重
 
echo "disable server webcluster/web1" | socat stdio /var/lib/haproxy/stats  ----指定下线server
启用backup
启用backup   ---- 两台服务器状态OK的情况下,不会访问这个
 vim /etc/httpd/conf/httpd.conf --------- 里面修改sorry server的端口
vim/etc/haproxy/haproxy.cfg
listen webcluster
    bind *:80
    mode http
    balance roundrobin    ---- 动态算法
    server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
    server web_sorry 172.25.254.100:8080 backup
针对多线程处理方法,haproxy热处理
设置多进程,用多个文件来处理多个要处理的事  haproxy热处理
vim/etc/haproxy/haproxy.cfg
 
    stats socket /var/lib/haproxy/stats1 mode 600 level admin process 1
    stats socket /var/lib/haproxy/stats2 mode 600 level admin process 2

roundrobin动态算法:

vim/etc/haproxy/haproxy.cfg
 
listen webcluster
    bind *:80
    mode http
    balance roundrobin    ---- 动态算法
    server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1

leastconn 动态算法:

listen webcluster
    bind *:80
    mode http
    balance leastconn   ---- 动态算法
    server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1

一致性hash:

一致性哈希,当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变动hash mode该hash算法是动态的,支持使用 socat等工具进行在线权重调整,支持慢启动

算法:

1、后端服务器哈希环点keyA=hash(后端服务器虚拟ip)%(2^32)

2、客户机哈希环点key1=hash(client_ip)%(2^32) 得到的值在[0---4294967295]之间,

3、将keyA和key1都放在hash环上,将用户请求调度到离key1最近的keyA对应的后端服务器

算法总结:

 #静态
static-rr--------->tcp/http
first------------->tcp/http
#动态
roundrobin-------->tcp/http
leastconn--------->tcp/http
#以下静态和动态取决于hash_type是否consistent
source------------>tcp/http
Uri--------------->http
url_param--------->http
hdr--------------->http

各算法的运用场景
first #使用较少
static-rr #做了session共享的web集群
roundrobin
leastconn #数据库
source
#基于客户端公网IP的会话保持
Uri--------------->http #缓存服务器,CDN服务商,蓝汛、百度、阿里云、腾讯
url_param--------->http #可以实现session保持
hdr #基于客户端请求报文头部做下一步处理

基于cookie会话保持
cookie value:为当前server指定cookie值,实现基于cookie的会话黏性,相对于基于 source 地址hash调度算法对客户端的粒度更精准,但同时也加大了haproxy负载,目前此模式使用较少, 已经被session共享服务器代替

选项:

name: #cookie 的 key名称,用于实现持久连接
insert: #插入新的cookie,默认不插入cookie
indirect: #如果客户端已经有cookie,则不会再发送cookie信息
nocache: #当client和hapoxy之间有缓存服务器(如:CDN)时,不允许中间缓存器缓存cookie,因为这会导致很多经过同一个CDN的请求都            发送到同一台后端服务器

配置示例 
vim /etc/haproxy/haproxy.cfg
 
listen webcluster
    bind *:80
    mode http
    balance roundrobin
    cookie WEBCOOKIE insert nocache indirect
    server web1 172.25.254.10:80 cookie ding1 check inter 2 fall 3 rise 5 weight 1
    server web2 172.25.254.20:80 cookie ding2 check inter 2 fall 3 rise 5 weight 1

网页访问测试:172.25.254.100  访问到之后按F12 选到网络,看cookie值是否改变。

curl访问时指定cookie:

RS主机访问:
curl -b WEBCOOKIE=pu1 172.25.254.100
curl -b WEBCOOKIE=pu2 172.25.254.100

haproxy的状态页面监控
配置选项:

 stats enable #基于默认的参数启用stats page
stats hide-version #将状态页中haproxy版本隐藏
stats refresh <delay> #设定自动刷新时间间隔,默认不自动刷新
stats uri <prefix> #自定义stats page uri,默认值:/haproxy?stats
stats auth <user>:<passwd> #认证时的账号和密码,可定义多个用户,每行指定一个用户
#默认:no authentication
stats admin { if | unless } <cond> #启用stats page中的管理功能

5.2.1 配置示例
vim/etc/haproxy/haproxy.cfg
 
listen stats
    mode http     ---模式
    bind *:443    ----- 监听端口
    stats enable  ---- 打开状态页
    stats refresh 5   ---- 设置刷新页面的时间
    stats uri /status  
    stats auth pupu:pu  ---- 访问页面认证

 IP透传:

四层IP透传
配置示例:
haproxy主机上:
vim/etc/haproxy/haproxy.cfg
 
listen webcluster
    bind *:80
    mode tcp #----四层
    balance roundrobin
    server web1 172.25.254.10:80 send-proxy check inter 2 fall 3 rise 5 weight 1   #send-proxy 代理
    server web2 172.25.254.20:80 send-proxy check inter 2 fall 3 rise 5 weight 1
 
RS主机上:
[root@werserver2 ~]# vim /etc/nginx/nginx.conf
[root@werserver1 ~]# vim /etc/nginx/nginx.conf
 
http {
    log_format  main  '$remote_user [$time_local] "$request" '
                      '"$proxy_protocol_addr"'   ----- 加上这个参数才能进行透传
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
 
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
 
 server {
        listen       80 proxy_protocol;----加上这个参数,不加访问会被拒绝
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;
 
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
 
        error_page 404 /404.html;
        location = /404.html {
        }
 
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
 
systemctl restart nginx.service

查看日志:
[root@werserver2 ~]# cat /var/log/nginx/access.log 

七层IP透传:

配置:

vim/etc/haproxy/haproxy.cfg
defaults
    mode                    http
    log                     global
    #option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8    ----- IP透传用到的参数 forwardfor
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 1000

listen webcluster
    bind *:80
    mode http #----七层
    balance roundrobin
    server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 1
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1

RS主机测试:
cat /var/log/nginx/access.log 

 haproxy中访问控制列表(ACL,全称Access Control Lists)

示例:
haproxy主机上:
vim/etc/haproxy/haproxy.cfg
 
frontend webcluster
    bind *:80
    mode http
    acl test hdr_dom(host) -i www.timingding.org
    use_backend webcluster-host if test  ----- 访问主机IP时,访问到RS2,访问域名时,访问到RS1
    default_backend default-host
 
backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
 
backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
 
 
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl www.timingding.org
webserver1 - 172.25.254.10
[root@haproxy ~]# 
 
注:要做解析。
Windows下hosts的路径:  C:\Windows\System32\drivers\etc\
Windows下:win+R  然后再ctrl+shift+回车,进入超级管理终端,输入notepad.exe,到hosts下添加域名
Linux下:vim /etc/hosts   中添加域名。

vim/etc/haproxy/haproxy.cfg
 
frontend webcluster
    bind *:80
    mode http
    acl test base_sub -m ding  ------ 包含ding(域名或目录都行)的都可以调度到RS1,不包含的调度到RS2
    use_backend webcluster-host if test 
    default_backend default-host
 
backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
 
backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5

创建子目录测试:
[root@webserver1 ~]# systemctl restart nginx.service 
[root@webserver1 ~]# mkdir /usr/share/nginx/html/ding -p
[root@webserver1 ~]# echo 172.25.254.10 ding > /usr/share/nginx/html/ding/index.html

利用ACL做动静分离等访问控制:

基于域名做动静分离:

示例:
vim/etc/haproxy/haproxy.cfg
 
frontend webcluster
    bind *:80
    mode http
    acl domain hdr_dom(host) -i www.timingding.org
    use_backend webcluster-host if  domain
    default_backend default-host
 
backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
 
backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5

测试:
[root@haproxy ~]# curl www.timingding.org
webserver1 - 172.25.254.10
[root@haproxy ~]# curl www.test.com
webserver2 - 172.25.254.20

基于源IP或子网调度访问:

示例:
vim/etc/haproxy/haproxy.cfg
 
frontend webcluster
    bind *:80
    mode http
    acl ctrl_ip src 172.25.254.1 172.25.254.20 192.168.0.0/24  -----符合条件的访问RS1
    use_backend webcluster-host if  ctrl_ip
    default_backend default-host
 
backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
 
backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5

测试:
[root@werserver2 ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
 
[root@haproxy ~]# curl www.test.com
webserver2 - 172.25.254.20

frontend webcluster
    bind *:80
    mode http
    acl ctrl_ip src 172.25.254.1 172.25.254.20 192.168.0.0/24
    http-request deny if ctrl_ip    ------ 符合条件的拒绝访问
    default_backend default-host
 
backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
 
backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5

测试:
[root@werserver2 ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@werserver2 ~]# curl 172.25.254.100
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>

基于浏览器的访问控制:

示例:
vim/etc/haproxy/haproxy.cfg
 
frontend webcluster
    bind *:80
    mode http
    acl badwebrowers hdr_sub(User-Agent) -i wget
    http-request deny if badwebrowers
    default_backend default-host
 
backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
 
backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5

 基于文件后缀名实现动静分离:

RS主机上:
[root@webserver1 ~]# install php -y
[root@webserver1 ~]# systemctl restart nginx.service 
[root@webserver1 ~]# 
[root@webserver1 ~]# vim /usr/share/nginx/html/index.php
<?php   
        phpinfo();
?>
 
查看是否能访问到php

vim/etc/haproxy/haproxy.cfg
 
frontend webcluster
    bind *:80
    mode http
    acl static path_end -i .html .jpg .png .css .js  # ---静态
    acl php path_end -i .php                         ---- 动态
    use_backend webcluster-host if  php
    default_backend default-host
 
backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
 
backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5

基于访问路径实现动静分离:

示例:
提前建立好测试目录:
[root@werserver2 ~]# mkdir /usr/share/nginx/html/static -p
[root@werserver2 ~]# 
[root@werserver2 ~]# echo ststic - 172.25.254.20 > /usr/share/nginx/html/static/index.html
[root@werserver2 ~]# curl 172.25.254.20/static/
ststic - 172.25.254.20
[root@werserver2 ~]# 
 
[root@webserver1 ~]# mkdir /usr/share/nginx/html/php
[root@webserver1 ~]# cp /usr/share/nginx/html/index.php /usr/share/nginx/html/php/
 
vim/etc/haproxy/haproxy.cfg
 
frontend webcluster
    bind *:80
    mode http
    acl static path_sub -m sub static 
    acl php path_sub -m sub  php
    use_backend webcluster-host if  php
    default_backend default-host
 
backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
 
backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
 
[root@haproxy ~]# systemctl restart haproxy.service 

自定义haproxy的错误界面:

两台web服务都给他挂掉
[root@werserver1 ~]# systemctl stop nginx.service 
[root@werserver2 ~]# systemctl stop nginx.service
 
此时访问不到

查看错误页面文件路径:
[root@haproxy ~]# rpm -ql haproxy | grep http
/usr/share/doc/haproxy/design-thoughts/http2.txt
/usr/share/doc/haproxy/design-thoughts/http_load_time.url
/usr/share/doc/haproxy/internals/http-cookies.txt
/usr/share/doc/haproxy/internals/http-docs.txt
/usr/share/doc/haproxy/internals/http-parsing.txt
/usr/share/doc/haproxy/option-http_proxy.cfg
/usr/share/haproxy/400.http
/usr/share/haproxy/403.http
/usr/share/haproxy/408.http
/usr/share/haproxy/500.http
/usr/share/haproxy/502.http
/usr/share/haproxy/503.http
/usr/share/haproxy/504.http

[root@haproxy ~]# mkdir /etc/haproxy/errorpages/ 
[root@haproxy ~]# vim /etc/haproxy/errorpages/503page.http 
 
HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html
 
<html><body><h1>shenmdongwu</h1>
puhailing
</body></html>

重定向错误文件:

root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
 
defaults
    mode                    http
    log                     global
    #option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 1000
 
    #errorfile 503  /etc/haproxy/errorpages/503page.http
    errorloc 503 https://www.baidu.com

haproxy的四层负载:

示例:
使用mariadb来示例:
两台RS主机安装mariadb:
[root@webserver1 ~]# yum install mariadb-server -y
[root@webserver2 ~]# yum install mariadb-server -y
 
改配置便于区分:
[root@webserver1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server_id=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
 
systemctl start mariadb
 
[root@webserver1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server_id=2
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
 
systemctl start mariadb

RS主机上创建mariadb远程登录用户
[root@webserver1 ~]# mysql
[root@webserver2 ~]# mysql    ----- 两台RS主机都一样 

查看端口是否开启:
netstat  -atnlupe | grep 3306 

haproxy主机:
[root@haproxy ~]# yum install mariadb-server -y
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen dbserver
    bind *:3306
    mode tcp
    balance static-rr   ----轮询
    server db1 172.25.254.10:3306 check inter 2 fall 2 rise 5
    server db2 172.25.254.20:3306 check inter 2 fall 2 rise 5
    
[root@haproxy ~]# systemctl restart haproxy.service

haproxy中https加密访问:

[root@haproxy ~]# mkdir -p /etc/haproxy/certs
制作证书:
[root@haproxy ~]#openssl req -newkey rsa:2048 -nodes -sha256 –keyout /etc/haproxy/certs/timinglee.org.key 
-x509 -days 365 -out /etc/haproxy/certs/timinglee.org.crt
 
[root@haproxy ~]#cat /etc/haproxy/certs/timingding.org.key /etc/haproxy/certs/timingding.org.crt >
 /etc/haproxy/certs/timingding.pem

haproxy主机上:
vim /etc/haproxy/haproxy.cfg
 
listen web-https
    bind *:443 ssl crt /etc/haproxy/certs/timingding.pem
    mode http
    balance roundrobin
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5

全站加密:

vim /etc/haproxy/haproxy.cfg
 
frontend webcluster
    bind *:80
    mode http
    redirect scheme https if !{ ssl_fc }  ----- 自动跳转https
 
backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
 
backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5

可以把代码都放到子配置文件里面:
查看:
[root@haproxy ~]# vim /lib/systemd/system/haproxy.service
 
子配置文件:
/etc/haproxy/conf.d
 
 
每次打开或修改自配置文件,重启之后都要执行systemctl daemon-reload

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值