【haproxy初始化】init_haproxy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#!/bin/bash
#
# 2015/4/29
# init haproxy cfg/rsyslog/logrotate
log() {
echo
"[-] install"
rpm -qa |
grep
haproxy && [ $? = 0 ]
[ $? = 0 ] &&
which
haproxy || yum -y
install
haproxy
echo
"[-] configure rsyslog and logrotate"
# rsyslog
cat
>
/etc/rsyslog
.d
/haproxy
.conf <<_CONF
# 启用 UDP port 514
\$ModLoad imudp
\$UDPServerRun 514
local2.=info -
/var/log/haproxy/haproxy
.log
local2.notice -
/var/log/haproxy/haproxy
.admin
# 其他类型的不记录
local2.* ~
_CONF
service rsyslog restart
# logrotate
[ -f
/etc/logrotate
.d
/haproxy
] ||
cat
>
/etc/logrotate
.d
/haproxy
<<_CONF
/var/log/haproxy/haproxy
.log {
daily
rotate 10
missingok
notifempty
compress
sharedscripts
postrotate
/bin/kill
-HUP `
cat
/var/run/syslogd
.pid 2>
/dev/null
` 2>
/dev/null
||
true
/bin/kill
-HUP `
cat
/var/run/rsyslogd
.pid 2>
/dev/null
` 2>
/dev/null
||
true
endscript
}
_CONF
echo
"[*] done."
}
cfg() {
echo
"[-] initialize cfg file, saved to: /etc/haproxy/haproxy.cfg"
mv
/etc/haproxy/haproxy
.cfg
/etc/haproxy/old
.haproxy.cfg
# add haproxy example conf
cat
>
/etc/haproxy/haproxy
.cfg <<_CONF
#---------------------------------------------------------------------
# HAProxy 配置
#
#---------------------------------------------------------------------
# 全局设置
#---------------------------------------------------------------------
global
# # 使用系统的rsyslog记录日志
#
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
#---------------------------------------------------------------------
# 通用设置, 'listen' 和 'backend' 部分会用到,如果没单独指定的话
#---------------------------------------------------------------------
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 1m
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
balance roundrobin
# lb算法
#---------------------------------------------------------------------
# 配置 stat
#---------------------------------------------------------------------
listen admin_stat
bind 127.0.0.1:12202
mode http
option httplog
log global
stats refresh 30s
# 统计页面自动刷新时间
stats uri
/status
# 统计页面URL
stats realm Haproxy\ Statistics
# 统计页面密码框上提示文本
stats auth admin:password
# 统计页面用户名和密码设置
stats hide-version
# 隐藏统计页面上HAProxy的版本信息
#---------------------------------------------------------------------
# 配置 TCP
#---------------------------------------------------------------------
# backend
# check -- 允许对该服务器进行健康检查
# weight -- 设置权重
# inter -- 连续两次健康检查间隔,单位为毫秒(ms),默认值 2000(ms)
# rise -- 指定多少次连续成功的健康检查后,即可认定该服务器处于可操作状态,默认值 2
# fall -- 指定多少次不成功的健康检查后,认为服务器为当掉状态,默认值 3
# server s_name s_ip:port check weight inter 2000 rise 2 fall 3
listen p80
bind *:80
mode tcp
option tcplog
server app1 192.168.1.240:80 check
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:5000
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
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
_CONF
echo
"[*] done."
service haproxy check
}
usage() {
cat
<<_USAGE
initialize haproxy log and config
Usage:
$0 [log|cfg]
_USAGE
}
##########
case
$1
in
log|cfg)
$1
;;
*)
usage
;;
esac
|
本文转自 pcnk 51CTO博客,原文链接:http://blog.51cto.com/nosmoking/1594657,如需转载请自行联系原作者