haproxy是安装在172.20.1.44上的,用于负载源站。
系统是centos7.5。
安装前也在网上查了一些资料,yum安装的haproxy版本比较低,不过足够用来负载源站了,暂时不考虑make&install更高版本,图方便😊。
1.yum查询版本
yum list |grep haproxy
#查询下可用版本
#果然 haproxy.x86_64 1.5.18-8.el7 base
#对付用先
2.yum安装
yum -y install haproxy
#已安装:
# haproxy.x86_64 0:1.5.18-9.el7_9.1
#完毕!
3.配置配置文件
#配置文件位置
cd /etc/haproxy/haproxy.cfg
#其实就是注释掉了我不需要的,主配置没动,添加了源站转发
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
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 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend main *:5000
# acl url_static path_end -i .jpg .gif .png .css .js
# use_backend static if url_static
# default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
# balance roundrobin
# server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
#backend app
# balance roundrobin
# server app1 127.0.0.1:5001 check
# server app2 127.0.0.1:5002 check
# server app3 127.0.0.1:5003 check
# server app4 127.0.0.1:5004 check
listen srs-cluster
bind *:1935
mode tcp
balance roundrobin
server master1 172.20.1.46:1935
server master2 172.20.1.47:1935
关键点就是最后两台源站服务器的地址和端口,因为我是单机部署,所以端口一致,如果想一台服务器部署多个SRS也是可以的,最好用Docker,可以参考官文。
4.验证配置文件
haproxy -f /etc/haproxy/haproxy.cfg -c
发现有警告
#返回
[WARNING] 207/225605 (31197) : parsing [/etc/haproxy/haproxy.cfg:45] : 'option httplog' not usable with proxy 'srs-cluster' (needs 'mode http'). Falling back to 'option tcplog'.
[WARNING] 207/225605 (31197) : config : 'option forwardfor' ignored for proxy 'srs-cluster' as it requires HTTP mode.
Configuration file is valid
默认的mode和log忘记改了,改成tcp和tcplog。验证还是有用的。
?另一个警告,意思就是 forwardfor需要http模式,所以忽略了(咱用了tcp呀)。
5.启动haproxy
systemctl start haproxy
6.重启haproxy
systemctl restart haproxy
接下来部署两台源站。
|