Haproxy简介:
- HAProxy是一个使用C语言编写的自由及开放源代码软件[1],其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
- HAProxy是一个使用C语言编写的自由及开放源代码软件[1],其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
- HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。
实验环境(RHEL6.5)
selinux和iptables状态为disabled
| server1(Haproxy) | 172.25.4.1 |
|---|---|
| server2(apache) | 172.25.4.2 |
| server3(apache) | 172.25.4.3 |
Haproxy相关配置
一.负载均衡:
server1配置:
1.官网下载haproxy软件包,并解压
[[email protected] ~]# ls
haproxy-1.7.3.tar.gz
[[email protected] ~]# tar zxf haproxy-1.7.3.tar.gz
[[email protected] ~]# ls
haproxy-1.7.3 haproxy-1.7.3.tar.gz
2.安装编译依赖包,并进行编译
[[email protected] haproxy-1.7.3]# yum install -y rpm-build pcre-devel gcc
[[email protected] haproxy-1.7.3]# cd -
[[email protected] ~]# rpmbuild -tb haproxy-1.7.3.tar.gz
[[email protected] ~]# ls ##自动生成rpmbuild目录
haproxy-1.7.3 haproxy-1.7.3.tar.gz rpmbuild
3.拷贝配置文件到/etc/haproxy目录下,并进行编辑
[[email protected] x86_64]# pwd
/root/rpmbuild/RPMS/x86_64
[[email protected] x86_64]# ls
haproxy-1.7.3-1.x86_64.rpm
[[email protected] x86_64]# rpm -ivh haproxy-1.7.3-1.x86_64.rpm ##安装
[[email protected] x86_64]# cd /root/haproxy-1.7.3/examples/ ##拷贝配置文件
[[email protected] examples]# cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg
[[email protected] examples]# cd /etc/haproxy/
[[email protected] haproxy]# vim haproxy.cfg ##编辑配置文件
global
maxconn 10000
stats socket /var/run/haproxy.stat mode 600 level admin
log 127.0.0.1 local0
uid 200
gid 200
chroot /var/empty
daemon
defaults
mode http
log global
option httplog
option dontlognull
monitor-uri /monitoruri
maxconn 8000
timeout client 30s
option prefer-last-server
retries 2
option redispatch
timeout connect 5s
timeout server 5s
stats uri /admin/stats 监控
# 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 dynamic
backend dynamic
balance roundrobin
server statsrv1 172.25.4.2:80 check inter 1000
server statsrv2 172.25.4.3:80 check inter 1000
4.创建haproxy用户和组,实现对haproxy服务的控制
[[email protected] haproxy]# groupadd -g 200 haproxy
[[email protected] haproxy]# useradd -u 200 -g 200 -s /sbin/nologin -M haproxy
5.开启haproxy服务
server2和server3配置(安装apache,默认发布目录下编写发布文件):
[[email protected] ~]# yum install -y httpd
[[email protected] ~]# vim /var/www/html/index.html
[[email protected] ~]# cat /var/www/html/index.html
server2
[[email protected] ~]# /etc/init.d/httpd start
[[email protected] ~]# [[email protected] ~]# yum install -y httpd
[[email protected] ~]# vim /var/www/html/index.html
[[email protected] ~]# cat /var/www/html/index.html
server3
[[email protected] ~]# /etc/init.d/httpd start
测试:
真机访问:curl 172.25.4.1,出现server2与server3的轮调
网页输入172.25.4.1/admin/stats,对haproxy负载均衡的后端服务器进行监控
如何对监控页面进行加密呢?
编辑haproxy配置文件:
[[email protected] haproxy]# vim haproxy.cfg ##在指定行写入
33 stats uri /admin/stats
34 stats auth admin:westos ##登陆用户名及密码
35 stats refresh 5s ##5s刷新一次
[[email protected] haproxy]# /etc/init.d/haproxy restart
测试:
二.动静分离
1.在server2中编辑默认发布目录下的index.html文件
[[email protected] html]# vim index.html
[[email protected] html]# cat index.html
server2
2.在server3上编辑默认发布目录下的index.php文件,并安装php
[[email protected] html]# vim index.php
[[email protected] html]# cat index.php
<?php
phpinfo()
?>
[[email protected] html]# yum install -y php
server2与server3编辑完后重起apache服务
3.server1上haproxy配置文件中添加动静分离配置
[[email protected] haproxy]# vim haproxy.cfg
# 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 }
use_backend dynamic if { path_end .php } # 访问路径以.php结尾,认为是动态页面
default_backend static # 静态页面,采用默认访问路径
# the application servers go here
backend static # 静态页面的后端服务器群组
balance roundrobin
server dynsrv1 172.25.254.2:80 check inter 1000
backend dynamic # 动态页面的后端服务器群组
balance roundrobin
server dynsrv2 172.25.254.3:80 check inter 1000
[[email protected] haproxy]# /etc/init.d/haproxy restart
测试:
网页访问,静态访问到server2的页面内容
动态访问到server3的php动态页面
三.修改haproxy日志路径
1.修改日志配置文件(存储位置),并重起
[[email protected] haproxy]# vim /etc/rsyslog.conf
13 $ModLoad imudp ##打开注释
14 $UDPServerRun 514 ##打开注释
42 *.info;mail.none;authpriv.none;cron.none;local0.none /var/log /messages
62 local0.* /var/log/haproxy.log ##将日志放在haproxy日志文件
[[email protected] haproxy]# /etc/init.d/rsyslog restart
测试:
网页访问server1后查看日志:
四.访问控制
1.修改haproxy配置文件
[[email protected] haproxy]# vim haproxy.cfg
acl blacklist src 172.25.254.70
http-request deny if blacklist
errorloc 403 http://172.25.4.1:8080/index.html ##因为拒绝的话太值百,可以定向到某台主机,显示错误页面
2.server1安装apache,修改端口为8080,编辑默认发布文件
[[email protected] haproxy]# yum install httpd -y
[[email protected] haproxy]# vim /etc/httpd/conf/httpd.conf
Listen 8080
[[email protected] haproxy]# cd /var/www/html/
[[email protected] html]# vim index.html 您已被拉黑...
[[email protected] html]# /etc/init.d/httpd start
测试:
五.读写分离
1.编辑haproxy配置文件
[[email protected] haproxy]# vim haproxy.cfg
acl write method POST
acl write method PUT
acl read method GET
acl read metnod HEAD
use_backend static if write
default_backend dynamic
2.在server3中的httpd默认发布目录,放进去index.php(选择图片的静态页面)和upload_file.php(上传图片的动态页面),存放上传图片的目录upload
[[email protected] html]# cd /var/www/html/upload/
[[email protected] upload]# ls index.php upload_file.php
[[email protected] upload]# chmod 644 *
[[email protected] upload]# mv * ..
[[email protected] upload]# cd ..
[[email protected] html]# chmod 777 upload
[[email protected] html]# ls
index.html index.php upload upload_file.php
——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"] < 20000))
{
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";
}
?>
3.修改上传的大小限制
[[email protected] html]# vim upload_file.php
&& ($_FILES["file"]["size"] < 2000000))
4.将编写好的upload目录及相关文件传到server3上
[email protected] html]# scp -rp upload upload_file.php index.php [email protected]:/var/www/html/
5.在server2上安装php,并重启httpd
[[email protected] html]# yum install php -y
[[email protected] html]# /etc/init.d/httpd restart
测试:
浏览器输入:172.25.4.1/index.php
点击Browse,选择上传的图片,在点击Submit提交
查看,上传图片保存在server2上的/var/www/html/upload目录下,server3没有