本文基于阿里云CentOS 7.6测试成功
# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
服务器信息:
192.168.109.101 | TCP转发代理 |
192.168.200.10 | 目的服务器 |
需要转发的端口:
(1)8888
(2)1234
在“TCP转发代理”服务器上执行如下步骤:
1、安装haproxy
# yum install haproxy -y
2、查看haproxy版本信息
# rpm -qi haproxy
Name : haproxy
Version : 1.5.18
Release : 8.el7
Architecture: x86_64
Install Date: Tue 30 Apr 2019 11:32:40 AM CST
Group : System Environment/Daemons
Size : 2690238
License : GPLv2+
Signature : RSA/SHA256, Mon 12 Nov 2018 10:28:49 PM CST, Key ID 24c6a8a7f4a80eb5
Source RPM : haproxy-1.5.18-8.el7.src.rpm
Build Date : Wed 31 Oct 2018 05:39:00 AM CST
Build Host : x86-01.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://www.haproxy.org/
Summary : TCP/HTTP proxy and load balancer for high availability environments
Description :
HAProxy is a TCP/HTTP reverse proxy which is particularly suited for high
availability environments. Indeed, it can:
- route HTTP requests depending on statically assigned cookies
- spread load among several servers while assuring server persistence
through the use of HTTP cookies
- switch to backup servers in the event a main server fails
- accept connections to special ports dedicated to service monitoring
- stop accepting connections without breaking existing ones
- add, modify, and delete HTTP headers in both directions
- block requests matching particular patterns
- report detailed status to authenticated users from a URI
intercepted by the application
3、配置haproxy日志
# vim /etc/rsyslog.d/haproxy.conf
local2.* /var/log/haproxy.log
# systemctl restart rsyslog
4、配置haproxy转发
# vim /etc/haproxy/haproxy.cfg
# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
ulimit-n 800000
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode tcp
log global
option dontlognull
retries 3
maxconn 6000
timeout queue 1m
timeout connect 1000s
timeout client 150000m
timeout server 150000m
timeout check 10s
frontend forward11234
bind *:1234
default_backend server1234
frontend forward8888
bind *:8888
default_backend server8888
backend server1234
server server1 192.168.200.10:11234 maxconn 3000
backend server8888
server server1 192.168.200.10:8888 maxconn 3000
5、启动haproxy
# systemctl enable haproxy
# systemctl start haproxy
6、验证
访问相关端口的应用。