共计 6295 个字符,预计需要花费 16 分钟才能阅读完成。
一,介绍
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。特别适用于那些负载特别大的web站点, 这些站点通常又需要会话保持或七层处理。HAProxy 运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整 合进您当前的架构中, 同时可以保护你的 web 服务器不被暴露到网络上。
二,安装
从49服务器下载haproxy-1.4.24.tar.gz,制作rpm安装包
yum install rpm-build -y | |
Rpmbuild -tb haproxy-1.4.24.tar.gz |
报错提示需要安装pcre-devel gcc
rpm -ivh /root/rpmbuild/RPMS/x86_64/haproxy-1.4.24-1.x86_64.rpm
即安装成功
三,配置
Server1.example.com
Vim /etc/haproxy/haproxy.cfg
global | |
#log 127.0.0.1 local0 | |
log 127.0.0.1 local1 notice | |
#log loghost local0 info | |
maxconn 4096 ##并发最大连接数量 | |
chroot /usr/share/haproxy ##jail目录 | |
uid 99 ##用户 | |
gid 99 ##组 | |
daemon ##后台运行 | |
#debug | |
#quiet | |
defaults | |
log global | |
mode http ##默认使用http的7层模式,tcp:4层 | |
option httplog #http日志格式 | |
option dontlognull ##禁用空连接日志 | |
retries 3 ##尝试3次失败认为服务器不可用 | |
option redispatch ##当client连接挂掉的主机时,重新分配到健康的主机 | |
maxconn 2000 | |
contimeout 5000 | |
stats uri /status ##HAproxy监控页面 | |
listen westos 0.0.0.0:80 ##监听所有ip的80端口 | |
balance roundrobin ##负载均衡算法 | |
server web1 172.25.49.3:80 check inter 2000 fall 3 | |
server web2 172.25.49.4:80 check inter 2000 fall 3 | |
server back1 127.0.0.1:8080 backup ###back1的8080端口提供服务 | |
#rise 2:表示 2 次正确认为服务器可用 | |
#fall 5:表示 5 次失败认为服务器不可用 | |
#cookie app1inst1:表示 serverid 为 app1inst1 | |
#check inter 2000:检测心跳频率 |
创建目录:mkdir /usr/share/haproxy
注意:因为本机作为被backup,所以本机的httpd服务需要开启,当所有realserver宕机时,本机提供一个错误页面,告知用户正在维护中,但HAProxy默认监听80端口,和httpd冲突,所以需要修改httpd监听端口:
vim /etc/httpd/conf/httpd.conf
136行: Listen 8080
/etc/init.d/haprexy reload
/etc/init.d/httpd restart
四,测试
浏览器输入172.25.49.1/status进行页面监控
输入172.25.49.1进行访问,轮询算法为roundrobin,所以交替出现server3和server4的apache发布页面,关掉server3和server4的httpd服务,显示back1主机也就是本机的apache页面。
五,安全验证
为保证监控页面安全,加密密码认证
listen stats_auth 0.0.0.0:8000 | |
stats enable | |
stats uri /status #监控页面地址 | |
stats auth admin:westos #管理帐号和密码 | |
stats refresh 5s #刷新频率 |
六,HAProxy日志
# vi /etc/rsyslog.conf #接受 haproxy 日志
$ModLoad imudp | |
$UDPServerRun 514 | |
local0.* /var/log/haproxy.log |
# /etc/init.d/rsyslog restart
vim /etc/haproxy/haproxy.cfg
global | |
log 127.0.0.1 local0 notice ##删除后面的notice,不限制日志级别 |
重启服务:/etc/init.d/haproxy reload
tail -f /var/log/haproxy.log
七,ACL
设置拒绝某个IP用户访问:
Vim /etc/haproxy/haproxy.cfg
listen westos 0.0.0.0:80 | |
balance roundrobin | |
acl badguy src 172.25.49.250 ###对来源172.25.49.250设置权限 | |
block if badguy ###拒绝访问 | |
errorloc 403 http://172.25.49.1:8080 ###显示403Forbiden不够人性化,将错误界面进行重定向 | |
server web1 172.25.49.3:80 check inter 2000 fall 3 | |
server web2 172.25.49.4:80 check inter 2000 fall 3 | |
server back1 127.0.0.1:8080 backup |
八,前端和后端配置
vim /etc/haproxy/haproxy.cfg
frontend westos 0.0.0.0:80 | |
default_backend linux ##默认后端服务器 | |
backend linux | |
balance roundrobin | |
server web1 172.25.49.3:80 check inter 2000 fall 3 | |
server web2 172.25.49.4:80 check inter 2000 fall 3 | |
server back1 127.0.0.1:8080 backup |
九,图片和文字分离
Vim /etc/haproxy/haproxy.cfg
frontend westos 0.0.0.0:80 | |
acl badguy src 172.25.49.250 | |
acl url_img path_end -i .jpg ##设置ACL名url_img,路径结尾为.jpg时启用 | |
#block if badguy | |
#errorloc 403 http://172.25.49.1:8080 | |
default_backend app | |
use_backend image if url_img ##当使用url_img时,启动后端image | |
backend app | |
balance roundrobin | |
server web1 172.25.49.3:80 check inter 2000 fall 3 | |
server web2 172.25.49.4:80 check inter 2000 fall 3 | |
server back1 127.0.0.1:8080 backup | |
backend image | |
balance roundrobin | |
server img1 172.25.49.2:80 check inter 2000 fall 3 |
测试:
将redhat.jpg放到server2(172.25.49.2)的http发布目录。使用浏览器访问
http://172.25.49.1/redhat.jpg,检测路径到以.jpg结尾,启用后端image,读取172.25.49.2的80端口提供的数据,即redhat.jpg,实现了图片和文字分离。
十,读写分离
frontend westos 0.0.0.0:80 | |
acl badguy src 172.25.49.250 | |
acl url_img path_end -i .jpg | |
acl upload method POST | |
#block if badguy | |
#errorloc 403 http://172.25.49.1:8080 | |
default_backend app | |
use_backend image if url_img | |
use_backend image if upload | |
backend app | |
balance roundrobin | |
server web1 172.25.49.3:80 check inter 2000 fall 3 | |
server web2 172.25.49.4:80 check inter 2000 fall 3 | |
server back1 127.0.0.1:8080 backup | |
backend image | |
balance roundrobin | |
server img1 172.25.49.2:80 check inter 2000 fall 3 |
在server2,server3,server4的/var/www/html中,从49服务器下载上传文件的php文件和upload目录
Chmod 777 upload/
upload_file.php index.php
安装php:yum install php -y
/etc/init.d/httpd restart
能上传文件到server2的upload即成功。
根据配置文件,默认访问的是server3和server4的apache目录里面的php页面,点击上传文件之后,由HAProxy启用到后端的图片服务器server2,所以会上传到server2的upload目录。
十一,HAProxy+Keepalived
在server1和server2源码安装keepalived
编辑配置文件:
MASTER:
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived | |
vrrp_script check_haproxy { | |
script "/opt/check_haproxy.sh" | |
interval 2 | |
weight 2 | |
} | |
global_defs { | |
notification_email { | |
root | |
} | |
notification_email_from server1.example.com | |
smtp_server 127.0.0.1 | |
smtp_connect_timeout 30 | |
router_id server1.example.com | |
} | |
vrrp_instance VI_1 { | |
state MASTER | |
interface eth0 | |
virtual_router_id 51 | |
priority 100 | |
advert_int 1 | |
authentication { | |
auth_type PASS | |
auth_pass 1111 | |
} | |
virtual_ipaddress { | |
172.25.49.100 | |
} | |
track_script { | |
check_haproxy | |
} | |
} |
BACKUP:
! Configuration File for keepalived | |
vrrp_script check_haproxy { | |
script "/opt/check_haproxy.sh" | |
interval 2 | |
weight 2 | |
} | |
global_defs { | |
notification_email { | |
root | |
} | |
notification_email_from server2.example.com | |
smtp_server 127.0.0.1 | |
smtp_connect_timeout 30 | |
router_id server2.example.com | |
} | |
vrrp_instance VI_1 { | |
state BACKUP | |
interface eth0 | |
virtual_router_id 51 | |
priority 50 | |
advert_int 1 | |
authentication { | |
auth_type PASS | |
auth_pass 1111 | |
} | |
virtual_ipaddress { | |
172.25.49.100 | |
} | |
track_script { | |
check_haproxy | |
} | |
} |
检测脚本:
# cat /opt/check_haproxy.sh
/etc/init.d/haproxy status &> /dev/null || /etc/init.d/haproxy restart &> /dev/null | |
if [ $? -ne 0 ];then | |
/etc/init.d/keepalived stop &> /dev/null | |
fi |
给检测脚本添加执行权限:
chmod +x /opt/check_haproxy.sh
使用浏览器访问vip:172.25.49.100,可以正常上传文件。