[root@localhost haproxy]# cp /tmp/install/haproxy-1.7.8/examples/haproxy.init /etc/init.d/haproxy ##从安装包里拷贝服务启动脚本
[root@localhost haproxy]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy]# chkconfig --add haproxy
[root@localhost haproxy]# chkconfig haproxy on
[root@localhost haproxy]# chkconfig --list|grep haproxy
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
haproxy 0:off 1:off 2:on 3:on 4:on 5:on 6:off (5)配置haproxy.cfg参数
[root@localhost haproxy]# vim /usr/local/haproxy/conf/haproxy.cfg
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
log 127.0.0.1 local2 ###[err warning info debug]
chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid ###haproxy的pid存放路径,启动进程的用户必须有权限访问此文件
maxconn 4000 ###最大连接数,默认4000
user haproxy
group haproxy
daemon ###创建1个进程进入deamon模式运行。此参数要求将运行模式设置为"daemon"
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http ###默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
log global ###采用全局定义的日志
option dontlognull ###不记录健康检查的日志信息
option httpclose ###每次请求完毕后主动关闭http通道
option httplog ###日志类别http日志格式
option forwardfor ###如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option redispatch ###serverId对应的服务器挂掉后,强制定向到其他健康的服务器
timeout connect 10000 #default 10 second timeout if a backend is not found
timeout client 300000 ###客户端连接超时
timeout server 300000 ###服务器连接超时
maxconn 60000 ###最大连接数
retries 3 ###3次连接失败就认为服务不可用,也可以通过后面设置
####################################################################
listen stats
bind 0.0.0.0:1080 #监听端口
stats refresh 30s #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm Haproxy Manager #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
#stats hide-version #隐藏统计页面上HAProxy的版本信息
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
bind 0.0.0.0:80
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend static if url_static ###满足策略要求,则响应策略定义的backend页面
default_backend dynamic ###不满足则响应backend的默认页面
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin ###负载均衡模式轮询
server static 127.0.0.1:80 check ###后端服务器定义
backend dynamic
balance roundrobin
server websrv1 192.168.180.9:80 check maxconn 2000
server websrv2 192.168.180.4:80 check maxconn 2000
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
errorfile 403 /etc/haproxy/errorfiles/403.http
errorfile 500 /etc/haproxy/errorfiles/500.http
errorfile 502 /etc/haproxy/errorfiles/502.http
errorfile 503 /etc/haproxy/errorfiles/503.http (6)通过/etc/init.d/haproxy start|stop|restart|status等命令控制haproxy服务
[root@localhost conf]# /etc/init.d/haproxy start
Starting haproxy (via systemctl): [ OK ]
[root@localhost conf]# ps -ef|grep haproxy
haproxy 4693 1 0 14:29 ? 00:00:00 /usr/local/haproxy/sbin/haproxy -D -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
root 4696 2767 0 14:29 pts/0 00:00:00 grep --color=auto haproxy
[root@localhost conf]# netstat -lntp|grep haproxy
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4693/haproxy
tcp 0 0 0.0.0.0:1080 0.0.0.0:* LISTEN 4693/haproxy
[root@localhost conf]# /etc/init.d/haproxy restart
Restarting haproxy (via systemctl): [ OK ]
[root@localhost conf]# /etc/init.d/haproxy status
/etc/init.d/haproxy: line 26: [: =: unary operator expected
● haproxy.service - SYSV: HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited for high availability environments.
Loaded: loaded (/etc/rc.d/init.d/haproxy)
Active: active (running) since Wed 2017-07-19 14:29:36 CST; 5s ago
Docs: man:systemd-sysv-generator(8)
Process: 4712 ExecStop=/etc/rc.d/init.d/haproxy stop (code=exited, status=0/SUCCESS)
Process: 4718 ExecStart=/etc/rc.d/init.d/haproxy start (code=exited, status=0/SUCCESS)
Main PID: 4723 (haproxy)
CGroup: /system.slice/haproxy.service
└─4723 /usr/local/haproxy/sbin/haproxy -D -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
Jul 19 14:29:36 localhost.localdomain systemd[1]: Starting SYSV: HA-Proxy is a TCP/HTTP reverse proxy which is particularly...ts....
Jul 19 14:29:36 localhost.localdomain haproxy[4718]: /etc/rc.d/init.d/haproxy: line 26: [: =: unary operator expected
Jul 19 14:29:36 localhost.localdomain haproxy[4718]: Starting haproxy: [ OK ]
Jul 19 14:29:36 localhost.localdomain systemd[1]: Started SYSV: HA-Proxy is a TCP/HTTP reverse proxy which is particularly ...ents..
Hint: Some lines were ellipsized, use -l to show in full. (7)设置haproxy日志。编辑/etc/rsyslog.conf 取消注释:
$ModLoad imudp
$UDPServerRun 514
$ModLoad imtcp
$InputTCPServerRun 514
添加
local2.* /var/log/haproxy.log
[root@localhost rsyslog.d]# vim /etc/rsyslog.conf
# rsyslog configuration file
# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html
#### MODULES ####
# The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark # provides --MARK-- message capability
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514
*.info;mail.none;authpriv.none;cron.none;local2.none /var/log/messages
# The authpriv file has restricted access.
authpriv.* /var/log/secure
# Log all the mail messages in one place.
mail.* -/var/log/maillog
# Log cron stuff
cron.* /var/log/cron
# Everybody gets emergency messages
*.emerg :omusrmsg:*
# Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler
# Save boot messages also to boot.log
local7.* /var/log/boot.log
local2.* /var/log/haproxy.log ########备注:重要以上修改haproxy日志会同时写入到/var/log/haproxy.log和/var/log/message两个文件里,由于haproxy日志量很多所以只让其写入到/var/log/haproxy.log文件需要修改/etc/rsyslog.conf 文件这个配置
*.info;mail.none;authpriv.none;cron.none;local3.none /var/log/messages
这样只会写入到/var/log/haproxy.log
修改/etc/sysconfig/rsyslog。#修改”SYSLOGD_OPTIONS”参数,-c 2 使用兼容模式,默认是 -c 5;-r 开启远程日志;-m 0 标记时间戳,单位是分钟,0表示禁用该功能。
[root@localhost etc]# vim /etc/sysconfig/rsyslog
# Options for rsyslogd
# Syslogd options are deprecated since rsyslog v3.
# If you want to use them, switch to compatibility mode 2 by "-c 2"
# See rsyslogd(8) for more details
#SYSLOGD_OPTIONS=""
SYSLOGD_OPTIONS="-c 2 -r -m 0"
#SYSLOGD_OPTIONS="-c 2 -r -m 0"
&~ 重新启动rsyslog服务
[root@localhost etc]# systemctl restart rsyslog.service
[root@localhost ~]# tail -f /var/log/haproxy.log
Jul 19 16:29:07 localhost haproxy[5341]: 192.168.181.231:56192 [19/Jul/2017:16:29:07.430] stats stats/ 0/0/0/0/0 200 23388 - - LR-- 0/0/0/0/0 0/0 "GET /stats HTTP/1.1"