haproxy简明配置

简介: 简介介绍haproxy的配置方法

haproxy是一款优秀的负载均衡软件,安装配置也非常简单,通常安装Linux发行版自带的haproxy即可,无需自行编译。

安装方法

ubuntu:apt install haproxy

CentOS:dnf(yum) install haproxy

配置文件

通过apt或者dnf安装的haproxy,配置文件默认在/etc/haproxy下,编译该目录下的haproxy.conf

global                                     # 全局配置。   log         127.0.0.1 local2 debug      # 定义全局的 syslog 服务器,最多可以定义两个。chroot      /var/lib/haproxy            # 更改当前目录并为启动进程设置超级用户权限,从而提高安全性。   pidfile     /var/run/haproxy.pid        # 将 HAProxy 进程的 PID 写入 pidfile。   maxconn     4000# 每个 HAProxy 进程所接受的最大并发连接数。   user        haproxy                     # 同 UID 参数。   group       haproxy                     # 同 GID 参数,建议使用专用用户组。   nbproc      40# 在后台运行时创建的进程数。在启动多个进程转发请求时,确保该值足够大,保证 HAProxy 不会成为瓶颈。   daemon                                  # 让 HAProxy 以守护进程的方式工作于后台,等同于命令行参数“-D”的功能。当然,也可以在命令行中用“-db”参数将其禁用。   stats socket /var/lib/haproxy/stats     # 统计信息保存位置。defaults                                   # 默认配置。   log global                              # 日志继承全局配置段的设置。   retries 2# 向上游服务器尝试连接的最大次数,超过此值便认为后端服务器不可用。   timeout connect  2s                     # HAProxy 与后端服务器连接超时时间。如果在同一个局域网内,可设置成较短的时间。   timeout client 30000s                   # 客户端与 HAProxy 连接后,数据传输完毕,即非活动连接的超时时间。   timeout server 30000s                   # 服务器端非活动连接的超时时间。listen admin_stats                         # frontend 和 backend 的组合体,此监控组的名称可按需进行自定义。   bind 0.0.0.0:3999                       # 监听端口。   mode http                               # 监控运行的模式,此处为 `http` 模式。   option httplog                          # 开始启用记录 HTTP 请求的日志功能。   maxconn 10# 最大并发连接数。   stats refresh 30s                       # 每隔 30 秒自动刷新监控页面。   stats uri /haproxy                      # 监控页面的 URL。   stats realm HAProxy                     # 监控页面的提示信息。   stats auth admin:pingcap123             # 监控页面的用户和密码,可设置多个用户名。   stats hide-version                      # 隐藏监控页面上的 HAProxy 版本信息。   stats  admin if TRUE                    # 手工启用或禁用后端服务器(HAProxy 1.4.9 及之后版本开始支持)。listen db-cluster                        # 配置 database 负载均衡。   bind *:4000                             # 浮动 IP 和 监听端口。   mode tcp                                # HAProxy 要使用第 4 层的传输层。   option tcplog
   log-format "%ci:%cp [%t] %b/%si:%sp %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq"   balance leastconn                       # 连接数最少的服务器优先接收连接。`leastconn` 建议用于长会话服务,例如 LDAP、SQL、TSE  等,而不是短会话协议,如 HTTP。该算法是动态的,对于启动慢的服务器,服务器权重会在运行中作调整。#tcp-request content accept if { src -f /etc/haproxy/white_ip_list }#tcp-request content reject   server db-1 192.168.0.1:4000 check inter 2000 rise 2 fall 3# 检测 4000 端口,检测频率为每 2000 毫秒一次。如果 2 次检测为成功,则认为服务器可用;如果 3 次检测为失败,则认为服务器不可用。   server db-2 192.168.0.2:4000 check inter 2000 rise 2 fall 3   server db-3 192.168.0.3:4000 check inter 2000 rise 2 fall 3listen redis                               # 配置 database 负载均衡。   bind *:6379                             # 浮动 IP 和 监听端口。   mode tcp                                # HAProxy 要使用第 4 层的传输层。   option tcplog
   log-format "%ci:%cp [%t] %b/%si:%sp %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq"   balance leastconn                       # 连接数最少的服务器优先接收连接。`leastconn` 建议用于长会话服务,例如 LDAP、SQL、TSE  等,而不是短会话协议,如 HTTP。该算法是动态的,对于启动慢的服务器,服务器权重会在运行中作调整。#tcp-request content accept if { src -f /etc/haproxy/white_ip_list }#tcp-request content reject   server redis-1 192.168.0.4:6379 check inter 2000 rise 2 fall 3   server redis-2 192.168.0.5:6379 check inter 2000 rise 2 fall 3

systemd启动脚本

[Unit]
Description=HAProxy Load Balancer
Documentation=man:haproxy(1)
Documentation=file:/usr/share/doc/haproxy/configuration.txt.gz
After=network-online.target rsyslog.service
Wants=network-online.target
[Service]
EnvironmentFile=-/etc/default/haproxy
EnvironmentFile=-/etc/sysconfig/haproxy
Environment="CONFIG=/etc/haproxy/haproxy.cfg""PIDFILE=/run/haproxy.pid""EXTRAOPTS=-S /run/haproxy-master.sock"ExecStartPre=/usr/sbin/haproxy -Ws-f$CONFIG-c-q$EXTRAOPTSExecStart=/usr/sbin/haproxy -Ws-f$CONFIG-p$PIDFILE$EXTRAOPTSExecReload=/usr/sbin/haproxy -Ws-f$CONFIG-c-q$EXTRAOPTSExecReload=/bin/kill -USR2$MAINPIDKillMode=mixed
Restart=always
SuccessExitStatus=143Type=notify
# The following lines leverage SystemD's sandboxing options to provide# defense in depth protection at the expense of restricting some flexibility# in your setup (e.g. placement of your configuration files) or possibly# reduced performance. See systemd.service(5) and systemd.exec(5) for further# information.# NoNewPrivileges=true# ProtectHome=true# If you want to use 'ProtectSystem=strict' you should whitelist the PIDFILE,# any state files and any other files written using 'ReadWritePaths' or# 'RuntimeDirectory'.# ProtectSystem=true# ProtectKernelTunables=true# ProtectKernelModules=true# ProtectControlGroups=true# If your SystemD version supports them, you can add: @reboot, @swap, @sync# SystemCallFilter=~@cpu-emulation @keyring @module @obsolete @raw-io[Install]
WantedBy=multi-user.target

启、停haproxy

# 设置/取消开机自启动systemctl enable/disable haproxy
# 启/停systemctl start/stop haproxy
# 查看状态systemctl status haproxy
相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
3月前
|
负载均衡 监控 算法
HAproxy
HAProxy 是一个高性能的负载均衡软件,可以将客户端的请求均衡地分发给多个后端服务器。HAProxy支持多种负载均衡算法,并提供灵活的配置选项。与LVS相比,HAProxy更加灵活和高级,可以进行更复杂的负载均衡策略和应用层的请求转发。通常,HAProxy可以与Keepalived结合使用,以提供高可用性和负载均衡的解决方案。
75 5
|
8月前
|
应用服务中间件 数据安全/隐私保护 nginx
Haproxy-安装与配置
安装 Haproxy
68 0
|
负载均衡 Linux
HaProxy 安装
HaProxy 安装
|
网络协议 算法 应用服务中间件
haproxy 介绍
HAproxy均衡负载部署和配置文件详解 HAproxy均衡负载部署和配置文件详解 HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。
2306 0
|
缓存 算法 网络协议
haproxy
监听所有80端口转发到8080 global daemon maxconn 25600 defaults mode http timeout connect 5000ms timeout client 50000ms timeout server 50...
1256 0
|
负载均衡 数据安全/隐私保护 网络协议
|
监控 网络协议 JavaScript
|
监控 负载均衡 网络协议
|
监控 前端开发 JavaScript