简介
HAProxy是一款提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,HAProxy是完全免费的、借助HAProxy可以快速并且可靠的提供基于TCP和HTTP应用的代理解决方案。
HAproxy工作原理
HAProxy由前端(frontend)和后端(backend),前端和后端都可以有多个。也可以只有一个listen块来同时实现前端和后端。这里主要讲一下frontend和backend工作模式。
前端(frontend)区域可以根据HTTP请求的header信息来定义一些规则,然后将符合某规则的请求转发到相应后端(backend)进行处理。
调度算法
roundrobin |
前段时间太想放弃治疗因此欠下太多作业没做,就不写详细使用方法了,有机会再补充
help
实验环境
CentOS Linux release 7.1.1503 (Core)
SElinux Disable
YUM源: Base,EPEL
IPTABLES: ACCEPT
haproxy |
Host0bash> yum install haproxy
bash> rpm -ql haproxy | less |
配置文件:/etc/haproxy/haproxy.cfg
配置文件首部信息# 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
#
这段英文大概意思就是需要自己修改rsyslog配置文件记录日志
将/etc/rsyslog.conf
文件
如下两行前面#
去掉bash> vim /etc/rsyslog.conf
$ModLoad imudp |
任意位置添加如下# Save haproxy log
local2.* /var/log/haproxy.log
haproxy记录日志时会自动创建/var/log/haproxy.log
修改/etc/sysconfig/rsyslog
文件:这里不改好像也可以bash> vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS="" |
重启rsyslogbash> systemctl restart rsyslog
修改配置文件 /etc/haproxy/haproxy.cfg
如下bash> vim /etc/haproxy/haproxy.cfg
global |
Host1bash> yum install nginx
bash> echo MASTER > /usr/share/nginx/html/index.html |
bash> systemctl start nginx |
Host2bash> yum install nginx
bash> echo BACKUP > /usr/share/nginx/html/index.html |
bash> systemctl start nginx |
Host3bash> yum install httpd php
bash> cat > /var/www/html/index.php <<EOF |
bash> systemctl start httpd |
Host0bash> systemctl start haproxy
查看状态
http://172.18.56.70:9000/haproxy?stats
因为172.18.56.74
根本就没有开机,所以喃~ Status 为 DOWN
–
http://172.18.56.70
测试静态内容
http://172.18.56.70/index.php
测试动态内容
没毛病
闪~