【haproxy】反向代理
安装
[root@15 ~]# tar xf haproxy-1.9-dev0.tar.gz
[root@15 ~]# cd haproxy-1.9-dev0
uname -r
[root@xuegod63 haproxy-1.9-dev0]#make TARGET=linux2628 PREFIX=/usr/local/haproxy?
make install PREFIX=/usr/local/haproxy
[root@15 haproxy-1.9-dev0]# ls /usr/local/haproxy/
mkdir /usr/local/haproxy/etc
vim /usr/local/haproxy/etc/haproxy.cfg
global
log 127.0.0.1 local0
#log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
chroot /usr/local/haproxy
uid 99 #所属运行的用户uid
gid 99 #所属运行的用户组
daemon #以后台形式运行haproxy
nbproc 1 #启动1个haproxy实例。# #工作进程数量(CPU数量) ,实际工作中,应该设置成和CPU核心数一样。 这样可以发挥出最大的性能。
pidfile /usr/local/haproxy/run/haproxy.pid #将所有进程写入pid文件
#debug #调试错误时用
#quiet #安静
defaults
log global
log 127.0.0.1 local3 #日志文件的输出定向。产生的日志级别为local3. 系统中local1-7,用户自己定义
mode http #工作模式,所处理的类别,默认采用http模式,可配置成tcp作4层消息转发
option httplog #日志类别,记载http日志
option httpclose #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现
option dontlognull #不记录空连接,产生的日志
option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option redispatch #当serverid对应的服务器挂掉后,强制定向到其他健康服务器
retries 2 #2次连接失败就认为服务器不可用,主要通过后面的check检查
maxconn 2000 #最大连接数
balance roundrobin #负载均衡算法
stats uri /haproxy-stats #haproxy 监控页面的访问地址 # 可通过 http://localhost:80/haproxy-stats 访问
timeout connect 5000 #连接超时时间。 单位:ms 毫秒
timeout client 50000 #客户端连接超时时间
timeout server 50000 #服务器端连接超时时间
mode http
option httpchk GET /index.html #健康检测#注意实际工作中测试时,应该下载某一个页面来进行测试,因此这个页面应该是个小页面,而不要用首页面。这里是每隔一秒检查一次页面。
frontend http #前端配置,http名称可自定义
bind 0.0.0.0:80 #发起http请求80端口,会被转发到设置的ip及端口
default_backend http_back #转发到后端 写上后端名称
backend http_back #后端配置,名称上下关联
server s1 192.168.1.2:80 weight 3 check #后端的主机 IP &权衡
server s2 192.168.1.3:80 weight 3 check #后端的主机 IP &权衡
#server node1 192.168.179.131:8081 check inter 2000 rise 3 fall 3 weight 30
# inter 2000 健康检查时间间隔2秒
# rise 3 检测多少次才认为是正常的
# fall 3 失败多少次才认为是不可用的
# weight 30 权重
[root@15 haproxy-1.9-dev0]# id nobody
复制haproxy启动脚本,到/etc/init.d下
[root@15 haproxy-1.9-dev0]# pwd
/root/haproxy-1.9-dev0
[root@xuegod63 ~]# cp ./examples/haproxy.init /etc/init.d/haproxy
[root@xuegod63 ~]# chmod 755 /etc/init.d/haproxy
[root@xuegod63 ~]# vim /etc/init.d/haproxy #此脚本需修改地方较多
#!/bin/sh
# chkconfig: - 85 15
# description: HA-Proxy server
# processname: haproxy
# config: /usr/local/haproxy/etc/haproxy.cfg
# pidfile: /usr/local/haproxy/run/haproxy.pid
# Source function library.
if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
else
exit 0
fi
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
# This is our service name
BASENAME=`haproxy`
BIN=/usr/sbin/haproxy
CFG=/usr/local/haproxy/etc/haproxy.cfg
[ -f $CFG ] || exit 1
PIDFILE=/usr/local/haproxy/run/haproxy.pid
LOCKFILE=/usr/local/haproxy/run/haproxy
RETVAL=0
start() {
quiet_check
if [ $? -ne 0 ]; then
echo "Errors found in configuration file, check it with '$BASENAME check'."
return 1
fi
echo -n "Starting $BASENAME: "
daemon $BIN -D -f $CFG -p $PIDFILE
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCKFILE
return $RETVAL
}
stop() {
echo -n "Shutting down $BASENAME: "
killproc $BASENAME -USR1
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
[ $RETVAL -eq 0 ] && rm -f $PIDFILE
return $RETVAL
}
restart() {
quiet_check
if [ $? -ne 0 ]; then
echo "Errors found in configuration file, check it with '$BASENAME check'."
return 1
fi
stop
start
}
reload() {
if ! [ -s $PIDFILE ]; then
return 0
fi
quiet_check
if [ $? -ne 0 ]; then
echo "Errors found in configuration file, check it with '$BASENAME check'."
return 1
fi
$BIN -D -f $CFG -p $PIDFILE -sf $(cat $PIDFILE)
}
check() {
$BIN -c -q -V -f $CFG
}
quiet_check() {
$BIN -c -q -f $CFG
}
rhstatus() {
status $BASENAME
}
condrestart() {
[ -e $LOCKFILE ] && restart || :
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
condrestart)
condrestart
;;
status)
rhstatus
;;
check)
check
;;
*)
echo $"Usage: $BASENAME {start|stop|restart|reload|condrestart|status|check}"
exit 1
esac
exit $?
[root@xuegod63 ~]#cp /usr/local/haproxy/sbin/haproxy /usr/sbin/
创建目录和权限
[root@xuegod63 ~]# mkdir -p /usr/local/haproxy/run
[root@xuegod63 ~]# chown nobody /usr/local/haproxy/ -R
配置日志收集
[root@xuegod63 ~]# vim /etc/rsyslog.conf #打开以下两行的注释,不打开收不到日志
$ModLoad imudp #取消注释
$UDPServerRun 514 #取消注释
local7.* /var/log/boot.log #下面添加两行
local3.* /var/log/haproxy.log
local0.* /var/log/haproxy.log
[root@xuegod63 ~]# systemctl restart rsyslog
3. 启动和停止服务
特殊启动方法1 开始无法启动是 haproxy.cfg配置文件34 转行没有注释掉
[root@xuegod63 etc]#haproxy -f /usr/local/haproxy/etc/haproxy.cfg
ps -axu | grep haproxy
netstat -antup | grep 80
killall haproxy
[root@xuegod63 ~]#/etc/init.d/haproxy start 或 systemctl restart haproxy
http://192.168.1.5/haproxy-stats
相关配置文件和启动脚本可以从这个配置模版中获得
[root@xuegod63 haproxy-1.7.9]# cd /root/haproxy-1.7.9/examples/?
[root@xuegod63 examples]# ls
配置随机启动
[root@xuegod63 examples]# chkconfig --add haproxy
[root@xuegod63 examples]# chkconfig haproxy on
[root@xuegod63 examples]# chkconfig --list haproxy
yum安装简单配置
yum -y install haproxy
只修改最下面的ip地址就可以简单ha代理
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 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 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 backend static balance roundrobin server static 127.0.0.1:4331 check 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 192.168.1.62:80 check
systemctl restart haproxy
参考链接
https://www.cnblogs.com/ilanni/p/4750081.html
https://baike.baidu.com/item/haproxy/5825820?fr=aladdin
官网文档
https://www.haproxy.com/documentation/hapee/1-8r1/onepage/#2

浙公网安备 33010602011771号