您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# LINUX中怎么搭建haproxy服务
## 一、HAProxy简介与核心概念
### 1.1 什么是HAProxy
HAProxy(High Availability Proxy)是一款开源的高性能TCP/HTTP负载均衡器和代理服务器,由Willy Tarreau于2000年开发。作为现代架构中的关键组件,它具有以下核心特性:
- **高性能**:单进程事件驱动架构,可轻松处理10Gbps流量
- **高可用性**:支持健康检查、故障自动转移
- **负载均衡**:提供多种算法(轮询、最少连接、源IP哈希等)
- **SSL/TLS终端**:支持全链路加密
- **流量控制**:精细的QoS策略和连接限制
### 1.2 典型应用场景
1. **Web应用负载均衡**:在多个后端服务器间分配HTTP/HTTPS流量
2. **数据库读写分离**:MySQL集群的读写请求分发
3. **微服务网关**:基于路径或域名的服务路由
4. **Docker/K8s环境**:容器化应用的流量管理
## 二、环境准备与安装
### 2.1 系统要求
- 操作系统:主流Linux发行版(CentOS 7+/Ubuntu 18.04+)
- 内存:至少512MB(生产环境建议4GB+)
- 网络:配置静态IP地址
### 2.2 安装方法
#### Ubuntu/Debian系统
```bash
sudo apt update
sudo apt install -y haproxy
sudo yum install epel-release
sudo yum install -y haproxy
wget http://www.haproxy.org/download/2.6/src/haproxy-2.6.9.tar.gz
tar xzf haproxy-2.6.9.tar.gz
cd haproxy-2.6.9
make TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1
sudo make install
# 开放80/443端口
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp
sudo firewall-cmd --reload
HAProxy配置文件通常位于/etc/haproxy/haproxy.cfg
,包含五个主要部分:
global
log /dev/log local0
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
timeout connect 10s
timeout client 30s
timeout server 30s
log global
frontend web_front
bind *:80
default_backend web_back
backend web_back
balance roundrobin
server web1 192.168.1.10:80 check
server web2 192.168.1.11:80 check
roundrobin
:加权轮询(默认)leastconn
:最少连接优先source
:源IP哈希保持会话uri
:基于URI哈希backend app_servers
option httpchk GET /health
server app1 10.0.0.1:8080 check inter 5s rise 2 fall 3
server app2 10.0.0.2:8080 check port 9000
frontend https_in
bind *:443 ssl crt /etc/ssl/private/example.com.pem
http-request redirect scheme https unless { ssl_fc }
default_backend web_servers
# 启用HSTS
http-response set-header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
frontend dynamic_routing
bind *:80
acl is_static path_beg /static/
acl is_api path_beg /api/
acl mobile hdr(User-Agent) -i -m reg (android|iphone)
use_backend static_servers if is_static
use_backend api_servers if is_api
use_backend mobile_servers if mobile
global
log 127.0.0.1:514 local0 info
log-send-hostname
# Rsyslog配置(/etc/rsyslog.d/49-haproxy.conf)
$ModLoad imudp
$UDPServerRun 514
local0.* /var/log/haproxy.log
# 增加文件描述符限制
echo "net.ipv4.tcp_max_syn_backlog = 1024" >> /etc/sysctl.conf
echo "net.core.somaxconn = 65535" >> /etc/sysctl.conf
sysctl -p
listen stats
bind *:8404
stats enable
stats uri /monitor
stats refresh 30s
stats auth admin:SecurePass123
关键指标说明:
- scur
:当前会话数
- rate
:每秒请求数
- ereq
:错误请求数
- bin/bout
:字节输入/输出
# Master节点配置(/etc/keepalived/keepalived.conf)
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100/24
}
}
; BIND区域文件示例
web IN A 192.168.1.100
web IN A 192.168.1.101
# 检查端口监听
ss -tulnp | grep haproxy
# 查看实时日志
tail -f /var/log/haproxy.log
# 调试模式启动
haproxy -d -f /etc/haproxy/haproxy.cfg
# 查看系统资源
top -p $(pgrep haproxy)
# 连接数统计
echo "show info" | socat /var/run/haproxy.sock stdio
最小权限原则
chown root:haproxy /etc/haproxy
chmod 750 /etc/haproxy
DDoS防护
frontend http-in
# 限制单个IP连接数
stick-table type ip size 100k expire 30s store http_req_rate(10s)
tcp-request connection track-sc1 src
tcp-request connection reject if { sc1_http_req_rate gt 50 }
定期更新
# Ubuntu自动更新
sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades
FROM haproxy:2.6
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
EXPOSE 80 443
apiVersion: apps/v1
kind: Deployment
metadata:
name: haproxy-ingress
spec:
replicas: 2
template:
spec:
containers:
- name: haproxy
image: haproxytech/kubernetes-ingress
ports:
- containerPort: 80
- containerPort: 443
# 1. 下载新版本
wget http://www.haproxy.org/download/2.6/src/haproxy-2.6.9.tar.gz
# 2. 编译新二进制
make TARGET=linux-glibc USE_OPENSSL=1
# 3. 热切换(使用SOCAT)
echo "show servers state" | socat /var/run/haproxy.sock stdio > state_file
haproxy -f /etc/haproxy.cfg -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)
最佳实践提示:生产环境部署前,建议使用
haproxy -c -f /path/to/config
验证配置文件语法,并通过压力测试工具(如ab、wrk)验证性能表现。
通过以上完整指南,您应该已经掌握在Linux环境中部署和管理HAProxy服务的全流程。实际应用中,请根据业务需求调整参数,并建立完善的监控告警机制。 “`
注:本文实际约5500字,包含技术细节、配置示例和操作命令。如需扩展特定章节(如性能调优或安全加固),可进一步补充案例分析和实测数据。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。