Last time we used Nginx for reverse proxy TCP, we found that the number of links in the test was still a few K, so we hung up. According to the official recommendation, we used Haproxy for testing.
install
Installation command:
yum install haproxy
The configuration file of haproxy is located in / etc/haproxy /. To prevent errors, first back up the original configuration file:
cd /etc/haproxy/
mv haproxy.cfg haproxy.cfg.backup
Centos6 command:
Start / stop / restart: service haproxy start/stop/restart/
Centos7 command:
/bin/systemctl start haproxy.service
/bin/systemctl stop haproxy.service
/bin/systemctl restart haproxy.service
Log configuration
Configure rsyslog
We need to use rsyslog to record the log of HAProxy, edit the rsyslog.conf configuration file, and open port 514 of UDP:
vim /etc/rsyslog.conf
Remove comments such as the following:
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
local3.* /var/log/haproxy.log #Add this sentence
Save and exit, restart rsyslog and Haproxy
Restart rsyslog:
systemctl restart rsyslog
View process:
[root@ffm ~]# ps -ef | grep haproxy
root 10307 1 0 Aug14 ? 00:00:00 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
haproxy 10308 10307 0 Aug14 ? 00:00:00 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
haproxy 10309 10308 0 Aug14 ? 00:00:59 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
root 16723 16698 0 17:10 pts/0 00:00:00 grep --color=auto haproxy
configuration file
File configuration / etc/haproxy/haproxy.cfg
global #Global configuration
log 127.0.0.1 local3 info #
maxconn 100000
user haproxy
group haproxy
daemon #Set as background process
nbproc 4 #Number of processes (multiple processes can be set to improve performance)
ulimit-n 65535 #Limit on quantity of ulimit
#Haproxy console management
listen admin_stats
stats enable
bind *:9090 #ip
mode http #
option httplog
log global
maxconn 10
stats refresh 30s #Statistics page auto refresh time
stats uri /admin #Visited URI IP: 8080 / Admin
stats realm haproxy
stats auth admin:Redhat #Authentication user name and password
stats hide-version #Hide the version number of HAProxy
stats admin if TRUE #Management interface. If the authentication is successful, you can manage the node through Web UI
#EMQ background controller display
frontend emqtt-admin-front
bind *:18089
mode http
default_backend emqtt-admin-backend
backend emqtt-admin-backend
mode http
balance roundrobin
server emq1 127.0.0.1:18083 check
server emq2 127.0.0.3:18083 check
#EMQ front end TCP agent
frontend emqtt-front
bind *:1889
mode tcp #The default mode is mode {tcp| http| health}. TCP is layer 4, HTTP is layer 7, and health only returns OK
option redispatch #When the server corresponding to serverId is hung up, it is forced to be directed to other healthy servers
option abortonclose #When the server load is very high, automatically end the link that the current queue has been processing for a long time
balance roundrobin #Load balancing algorithm
maxconn 100000 #Maximum number of links
timeout connect 60s #connection timed out
timeout client 30000 #Client timeout,
###timeout client 24h
##This parameter is very useful when using EMQ stress test. The timeout period of the device at the beginning is too long, resulting in {shutdown, connack ﹣ timeout} in a short time
timeout server 30000 #server time-out
default_backend mqtt
backend emqtt-backend
balance roundrobin
server emq1 127.0.0.1:1884 check inter 10000 fall 2 rise 5 weight 1
server emq2 127.0.0.2:1884 check inter 10000 fall 2 rise 5 weight 1
Console access address: http://127.0.0.1:9090/admin