【haproxy初始化】init_haproxy.sh
#!/bin/bash # # 2015/4/29 # init haproxy cfg/rsyslog/logrotatelog() {echo "[-] install"rpm -qa |grep haproxy && [ $? = 0 ][ $? = 0 ] && which haproxy || yum -y install haproxyecho "[-] configure rsyslog and logrotate"# rsyslogcat >/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.* ~ _CONFservice rsyslog restart# logrotate[ -f /etc/logrotate.d/haproxy ] || cat > /etc/logrotate.d/haproxy <<_CONF /var/log/haproxy/haproxy.log {dailyrotate 10missingoknotifemptycompresssharedscriptspostrotate/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 || trueendscript } _CONFecho "[*] 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 confcat >/etc/haproxy/haproxy.cfg <<_CONF #--------------------------------------------------------------------- # HAProxy 配置 ##--------------------------------------------------------------------- # 全局设置 #--------------------------------------------------------------------- global# # 使用系统的rsyslog记录日志#log 127.0.0.1 local2chroot /var/lib/haproxypidfile /var/run/haproxy.pidmaxconn 4000user haproxygroup haproxydaemon# turn on stats unix socketstats socket /var/lib/haproxy/stats#--------------------------------------------------------------------- # 通用设置, 'listen' 和 'backend' 部分会用到,如果没单独指定的话 #--------------------------------------------------------------------- defaultsmode httplog globaloption httplogoption dontlognull # 不记录空连接option http-server-closeoption forwardfor except 127.0.0.0/8option redispatchretries 3timeout http-request 1mtimeout queue 1mtimeout connect 10stimeout client 1mtimeout server 1mtimeout http-keep-alive 10stimeout check 10smaxconn 3000balance roundrobin # lb算法#--------------------------------------------------------------------- # 配置 stat #--------------------------------------------------------------------- listen admin_statbind 127.0.0.1:12202mode httpoption httploglog globalstats refresh 30s # 统计页面自动刷新时间stats uri /status # 统计页面URLstats 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 3listen p80bind *:80mode tcpoption tcplogserver app1 192.168.1.240:80 check#--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend main *:5000acl url_static path_beg -i /static /p_w_picpaths /javascript /stylesheetsacl url_static path_end -i .jpg .gif .png .css .jsuse_backend static if url_staticdefault_backend app#--------------------------------------------------------------------- # static backend for serving up p_w_picpaths, stylesheets and such #--------------------------------------------------------------------- backend staticbalance roundrobinserver static 127.0.0.1:4331 check#--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- backend appbalance roundrobinserver app1 127.0.0.1:5001 checkserver app2 127.0.0.1:5002 checkserver app3 127.0.0.1:5003 checkserver app4 127.0.0.1:5004 check_CONFecho "[*] done."service haproxy check}usage() {cat <<_USAGEinitialize haproxy log and configUsage:$0 [log|cfg]_USAGE }########## case $1 inlog|cfg)$1;;*)usage;; esac