线上Haproxy配置
安裝平臺(tái):centos 6.3
cd /srv
wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz
#1.4.22版本比較穩(wěn)定,現(xiàn)在最新版本是1.4.23
tar zxvf haproxy-1.4.22.tar.gz
cd haproxy-1.4.22
make TARGET=linux26 PREFIX=/usr/local/haproxy install
mkdir /usr/local/haproxy/conf -p
groupadd -r haproxy
useradd -r -M -g haproxy -s /sbin/nologin haproxy
vim /usr/local/haproxy/conf/haproxy.cfg
#寫(xiě)入以下內(nèi)容,根據(jù)自己環(huán)境修改IP地址
global
? ? ? ?log ? ? 127.0.0.1 local0 info
? ? ? ?maxconn 4096
? ? ? ?user ? ?haproxy
? ? ? ?group ? haproxy
? ? ? ?daemon
? ? ? ?nbproc ?1
? ? ? ?pidfile /var/run/haproxy.pid
defaults
? ? ? ?log ? ? ? ? ? ? global
? ? ? ?mode ? ? ? ? ? ?http
? ? ? ?option ? ? ? ? ?dontlognull
? ? ? ?retries ? ? ? ? 3
? ? ? ?option ? ? ? ? ?redispatch
? ? ? ?maxconn ? ? ? ? 20000
? ? ? ?contimeout ? ? ?5000
? ? ? ?clitimeout ? ? ?50000
? ? ? ?srvtimeout ? ? ?50000
#根據(jù)自己需求配置,HA_IP為haproxy的IP地址,IP為后端服務(wù)器的IP地址,下面只寫(xiě)了兩個(gè)實(shí)例
#mysql服務(wù)器
listen ?server_mysql_name ?HA_IP:3306
? ? ? ?mode tcp
? ? ? ?maxconn 200
? ? ? ?balance roundrobin
? ? ? ?server ?mysql_name_A ?mysql_IP:3306 ?check port 3306 inter 5s rise 2 fall 3
? ? ? ?server ?mysql_name_B ?mysql_IP:3306 ?check port 3306 inter 5s rise 2 fall 3
#web服務(wù)器
listen tomcat-app *:8080
? ? ? ?maxconn 2000
? ? ? ?mode http
? ? ? ?balance source
? ? ? ?option httpclose # disable keep-alive
? ? ? ?option forwardfor
? ? ? ?option httpchk GET /safe
? ? ? ?server server_web_A ?IP:8080 ?check inter 2000 fall 3
? ? ? ?server server_web_B ?IP:8080 ?check inter 2000 fall 3
#監(jiān)控管理
listen ?admin_status
? ? ? ?mode ?http
? ? ? ?bind HA_IP:8899
? ? ? ?option httplog
? ? ? ?log global
? ? ? ?stats enable
? ? ? ?stats refresh 10s
? ? ? ?stats hide-version
? ? ? ?stats realm Haproxy\ Statistics
? ? ? ?stats uri ?/admin-status
? ? ? ?stats auth ?admin:admin
? ? ? ?stats admin if TRUE
#啟動(dòng)腳本
cd /etc/init.d/
vim haproxy
#!/bin/sh
# haproxy
# chkconfig: ? 35 85 15
# processname: haproxy
# config: ? ? ?/usr/local/haproxy/conf/haproxy.cfg
# pidfile: ? ? /var/run/haproxy.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
config="/usr/local/haproxy/conf/haproxy.cfg"
exec="/usr/local/haproxy/sbin/haproxy"
prog=$(basename $exec)
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
lockfile=/var/lock/subsys/haproxy
check() {
? ?$exec -c -V -f $config
}
start() {
? ?$exec -c -q -f $config
? ?if [ $? -ne 0 ]; then
? ? ? ?echo "Errors in configuration file, check with $prog check."
? ? ? ?return 1
? ?fi
? ?echo -n $"Starting $prog: "
? ?# start it up here, usually something like "daemon $exec"
? ?daemon $exec -D -f $config -p /var/run/$prog.pid
? ?retval=$?
? ?echo
? ?[ $retval -eq 0 ] && touch $lockfile
? ?return $retval
}
stop() {
? ?echo -n $"Stopping $prog: "
? ?# stop it here, often "killproc $prog"
? ?killproc $prog
? ?retval=$?
? ?echo
? ?[ $retval -eq 0 ] && rm -f $lockfile
? ?return $retval
}
restart() {
? ?$exec -c -q -f $config
? ?if [ $? -ne 0 ]; then
? ? ? ?echo "Errors in configuration file, check with $prog check."
? ? ? ?return 1
? ?fi
? ?stop
? ?start
}
reload() {
? ?$exec -c -q -f $config
? ?if [ $? -ne 0 ]; then
? ? ? ?echo "Errors in configuration file, check with $prog check."
? ? ? ?return 1
? ?fi
? ?echo -n $"Reloading $prog: "
? ?$exec -D -f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid)
? ?retval=$?
? ?echo
? ?return $retval
}
force_reload() {
? ?restart
}
fdr_status() {
? ?status $prog
}
case "$1" in
? ?start|stop|restart|reload)
? ? ? ?$1
? ? ? ?;;
? ?force-reload)
? ? ? ?force_reload
? ? ? ?;;
? ?checkconfig)
? ? ? ?check
? ? ? ?;;
? ?status)
? ? ? ?fdr_status
? ? ? ?;;
? ?condrestart|try-restart)
? ? ? ?[ ! -f $lockfile ] || restart
? ? ? ?;;
? ?*)
? ? ? ?echo $"Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force-reload}"
? ? ? ?exit 2
esac
chmod 755 /etc/init.d/haproxy
/etc/init.d/haproxy start|stop|restart
轉(zhuǎn)載于:https://blog.51cto.com/linuxart/1213056
總結(jié)
以上是生活随笔為你收集整理的线上Haproxy配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java基础-类加载学习笔记
- 下一篇: 客户端soap【JAX-WS入门系列】第