HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的 并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。
Haproxy八种负载均衡算法(balance)
1.balance roundrobin ###轮询,软负载均衡基本都具备这种算法
2.balance static-rr ###根据权重
3.balance leastconn ###最少连接数先处理
4.balance source ###分局请求的IP
5.balance uri ###分局请求的uri
6.balance url_param ###根据请求的URL参数
7.banlance hdr(name) ###根据HTTP请求头来锁定每一次HTTP请求
8.balance rbp-cookie(name) ###根据cookie来锁定hash每一次TCP请求
Haproxy的安装过程
tar zxf haproxy-1.6.11.tar.gz #解压tar包
yum install rpm-build -y #安装rpm包制作工具
rpmbuild -tb haproxy-1.6.11.tar.gz #将其制作为rpm包
cd haproxy-1.6.11/examples
cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg 将模板中的配置文件移到对应目录下
修改配置文件
global # 全局定义
maxconn 10000 # 最大连接数
#需要在/etc/security中添加限制
stats socket /var/run/haproxy.stat mode 600 level admin
log 127.0.0.1 local0 # 夲机日志
uid 200 # haproxy用户的uid
gid 200 # haproxy用户的gid
chroot /var/empty
daemon
defaults # 默认条件
mode http
log global
option httplog
option dontlognull
monitor-uri /monitoruri
maxconn 8000
timeout client 30s
stats uri /admin/stats
option prefer-last-server
retries 2
option redispatch
timeout connect 5s
timeout server 5s
# The public 'www' address in the DMZ
frontend public
bind *:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#use_backend static if { hdr_beg(host) -i img }
#use_backend static if { path_beg /img /css }
default_backend static # 默认使用static后端服务器集群
# The static backend backend for 'Host: img', /img and /css.
backend static
balance roundrobin # 负载均衡算法rr
server statsrv1 172.25.12.2:80 check inter 1000 # 后端服务器server2
server statsrv2 172.25.12.3:80 check inter 1000 # 后端服务器server3
添加haproxy用户和组
grep 200 /etc/passwd
groupadd -g 200 haproxy 建立一个haproxy组
useradd -u 200 -g 200 -M haproxy
id haproxy
修改好配置文件以后对服务进行启动
测试:
在server2和server3中安装httpd,对默认发布目录进行设置
访问172.25.12.1,server2和server3中的内容会进行轮询
haproxy配置访问网站为动静分离
配置:
frontend public
bind *:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#use_backend static if { hdr_beg(host) -i img }
#use_backend static if { path_beg /img /css }
use_backend static2 if { path_end -i .php } # 访问路径中为.php结尾的页面时候去访问static2
default_backend static1 # 静态页面,采用默认访问路径
# The static backend backend for 'Host: img', /img and /css.
backend static1
balance roundrobin
server statsrv1 172.25.12.2:80 check inter 1000
backend static2
balance roundrobin
server statsrv2 172.25.12.3:80 check inter 1000
测试 在server2和server3中
将server2默认发布目录中文件名写为index.html
server3中默认发布目录中文件名写为index.php(在server3中需要安装php并且将httpd进行重新启动)
当访问172.25.12.1时候会去主动访问server2
访问172.25.12.1/index.php的时候会去访问server3中的php文件
修改haproxy的日志路径:
vim /etc/rsyslog.conf
13 $ModLoad imudp
14 $UDPServerRun 514 #注释去掉
*.info;mail.none;authpriv.none;cron.none;local0.none # /var/log/messages
# The authpriv file has restricted access.
authpriv.* /var/log/secure
# Log all the mail messages in one place.
mail.* -/var/log/maillog
# Log cron stuff
cron.* /var/log/cron
# Everybody gets emergency messages
*.emerg *
# Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler
# Save boot messages also to boot.log
local7.* /var/log/boot.log
local0.* /var/log/haporxy.log # local0的日志放在/var/log/haporxy.log
重新启动 /etc/init.d/rsyslog restart
haproxy的日志将会存放在/var/log/haproxy
haproxy的访问控制
(1)将特定用户进行拒绝,或重新定向
34 frontend public
35 bind *:80 name clear
36 #bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
37
38 #use_backend static if { hdr_beg(host) -i img }
39 #use_backend static if { path_beg /img /css }
40 acl blacklist src 172.25.1.250 #创建访问黑名单
43 http-request deny if blacklist #对黑名单里面的用户进行拒绝
44 #errorloc 403 http://172.25.1.1:8080 #将访问拒绝的信息重定向到172.25.1.1上
48 default_backend static1
这样设置对指定用户访问可以进行拒绝,但是拒绝不能太过去直白,那么我们将所要显示的信息重新定向到一个主机上,显示错误信息,可以定向到本机,然后将起发布信息改为,网站正在维护,显示比较友好
(2)将所有的用户进行重新定向
frontend public
bind *:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#use_backend static if { hdr_beg(host) -i img }
#use_backend static if { path_beg /img /css }
redirect location http://www.baidu.com #将所有的用户访问定向到www.baidu.com
default_backend static1
设置以后那么所有的用户访问static1都会被重新定向到www.baidu.com
haproxy进行读写分离
配置信息
frontend public
bind *:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#use_backend static if { hdr_beg(host) -i img }
#use_backend static if { path_beg /img /css }
#acl blacklist src 172.25.1.250
acl write method POST #写的动作
acl write method PUT
#http-request deny if blacklist
#errorloc 403 http://172.25.1.1:8080
#redirect location http://www.baidu.com
#use_backend static2 if { path_end -i .php }
use_backend static2 if write #如果是写的动作那么在static2上进行
default_backend static1 #默认会去访问static1
这样设定相当于是,在访问的时候默认访问的时static1但是当执行写的动作的时候会在static2上指定
在server3和server2中的httpd默认发布目录,放进去index.php(选择图片的静态页面)和upload_file.php(上传图片的动态页面),存放上传图片upload目录修改权限为777。
index.php:
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
upload_file.php:
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")) # 图片的三种格式(只能上传着三种)
&& ($_FILES["file"]["size"] < 2000000)) # 上传图片的大小(k为单位),可以自己调节大小
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{ # 上传成功显示的信息(所以上传图片是个动态的过程)
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file"; # 不符合上传条件显示Invalid file
}
?>
虽然我们访问的时server2的主页面,但是上传的图片将会在server3的upload目录当中存在