什么是HAproxy
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。 HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。
HAproxy的特性:
(1)反向代理
(2)读写分离
(3)动静分离
HAproxy与各负载均衡器的区别
(1)与nginx:同样工作在用户空间,nginx是一款轻量级,能实现缓存、webserver、邮件、负载均衡等功能,但nginx的许多功能都需要第三方的模块,而haproxy的转发能力比nginx有更强更灵活的定制性,可以运用splice实现0复制的转发,并且有更直观的图形化管理界面,不过通用性不如nginx,并无缓存功能
(2)与varnish:varnish是一款web缓存系统,
(3)与lvs:lvs是工作在内核空间上直接转发的,无缓存功能
正向代理和反向代理的区别
正向代理是指客户端直接访问服务端
反向代理是指客户端访问调度器,调度器再访问后端服务器将访问到的数据返回给客户端
实验环境:
主机名 | IP | 服务 |
---|---|---|
server1 | 172.25.22.1 | haproxy,httpd |
server2 | 172.25.22.2 | httpd,php |
server3 | 172.25.22.3 | httpd,php |
foundation22 | 172.25.22.250 | 测试端 |
一、HApronxy实现负载均衡的配置过程
1.从官网获得一个HAproxy的安装包,解压安装包
(makefile写各个模块之间是怎么调用的
config make install后生成的文件)
[root@server1 ~]# ls
haproxy-1.7.3.tar.gz
[root@server1 ~]# tar zxf haproxy-1.7.3.tar.gz
[root@server1 ~]# ls
haproxy-1.7.3 haproxy-1.7.3.tar.gz
[root@server1 ~]# cd haproxy-1.7.3
[root@server1 haproxy-1.7.3]# ls
CHANGELOG doc include Makefile scripts tests
contrib ebtree LICENSE README src VERDATE
CONTRIBUTING examples MAINTAINERS ROADMAP SUBVERS VERSION
2.将源码包打包成一个rpm包
(1)查找.spec文件 (能否把一个源码包打成rpm包要看.sepc文件)
[root@server1 haproxy-1.7.3]# find . -name *.spec
./examples/haproxy.spec
[root@server1 haproxy-1.7.3]# cd
(2)安装编译工具rpm-build gcc pcre-devel
[root@server1 ~]# yum install -y rpm-build gcc pcre-devel
[root@server1 ~]# rpmbuild -tb haproxy-1.7.3.tar.gz
编译完成之后会出现一个rpmbuild目录,rpm安装包在这个目录中 (rpm 红帽的包的管理器)
[root@server1 ~]# ls
haproxy-1.7.3 haproxy-1.7.3.tar.gz rpmbuild
[root@server1 ~]# cd rpmbuild/
[root@server1 rpmbuild]# ls
BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
[root@server1 rpmbuild]# cd RPMS/
[root@server1 RPMS]# ls
x86_64
[root@server1 RPMS]# cd x86_64/
[root@server1 x86_64]# ls
haproxy-1.7.3-1.x86_64.rpm
[root@server1 x86_64]# rpm -ivh haproxy-1.7.3-1.x86_64.rpm
Preparing... ########################################### [100%]
1:haproxy ########################################### [100%]
4.编辑haproxy的配置文件
(1)haproxy的配置文件在源码包中
[root@server1 x86_64]# cd
[root@server1 ~]# ls
[root@server1 ~]# cd haproxy-1.7.3
[root@server1 haproxy-1.7.3]# ls
[root@server1 haproxy-1.7.3]# cd examples/
[root@server1 examples]# ls
content-sw-sample.cfg 是haproxy的默认配置文件
(2)将这个文件复制到/etc/haproxy目录下
[root@server1 examples]# cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg
[root@server1 examples]# cd
[root@server1 ~]# cd /etc/haproxy/
[root@server1 haproxy]# ls
haproxy.cfg
[root@server1 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 ##haproxy用户的uid
gid 200 ##haproxy用户的gid
chroot /var/empty
daemon
defaults ##默认条件
mode http ##默认使用http的7层模式 tcp:4层
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 ##默认使用dynamic后端服务器集群
# the application servers go here
backend dynamic
balance roundrobin ##负载均衡算法rr(轮循)
server web1 172.25.22.2:80 check inter 1000 ##后端服务器server2
server web2 172.25.22.3:80 check inter 1000 ##后端服务器server3
5.server2和server3安装httpd,编辑测试页,开启服务
(1)server2
[root@server2 ~]# yum install -y httpd
[root@server2 ~]# cat /var/www/html/index.html
server2
[root@server2 ~]# /etc/init.d/httpd start
[root@server3 ~]# yum install -y httpd
[root@server3 ~]# vim /var/www/html/index.html
[root@server3 ~]# cat /var/www/html/index.html
server3
[root@server3 ~]# /etc/init.d/httpd start
[root@server1 haproxy]# /etc/init.d/haproxy start
[root@foundation22 ~]# curl 172.25.22.1
server2
[root@foundation22 ~]# curl 172.25.22.1
server3
[root@foundation22 ~]# curl 172.25.22.1
server2
[root@foundation22 ~]# curl 172.25.22.1
server3
8.浏览器访问http://172.25.22.1/admin/stats出现监控页面,f5可以刷新,f12可以进入控制台
(1)关闭server2的httpd服务,f5刷新,可以看到监控发生变化
(2)再次打开server2的httpd服务,刷新后监控恢复正常
9.浏览器访问
http://172.25.22.1/monitoruri出现200 OK 说明haproxy服务正常
二、访问控制
1.用户密码访问
(1)编辑配置文件
设置用户和密码以及刷新时间
[root@server1 haproxy]# vim haproxy.cfg
[root@server1 haproxy]# /etc/init.d/rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
添加的内容:
stats auth admin:westos
stats refresh 5s
(2)测试:浏览器访问需要输入密码,密码输入正确后可以访问
(3)查看日志看刷新时间
<1>编辑日志服务的配置文件,打开UDP接口,创建haproxy的日志文件,重启服务
[root@server1 haproxy]# vim /etc/rsyslog.conf
[root@server1 haproxy]# /etc/init.d/rsyslog restart
local0.* /var/log/haproxy.log
<2>查看日志文件,可以看到5秒刷新一次
2.黑名单
(1)编辑配置文件,重启服务
[root@server1 haproxy]# vim haproxy.cfg
[root@server1 haproxy]# /etc/init.d/haproxy reload
acl blacklist src 172.25.22.250
http-request deny if blacklist
[root@foundation22 ~]# curl 172.25.22.1
403 Forbidden
Request forbidden by administrative rules.
[root@server1 haproxy]# vim haproxy.cfg
[root@server1 haproxy]# /etc/init.d/haproxy restart
(2)编辑httpd配置文件修改httpd端口,编辑测试页。开启服务
[root@server1 haproxy]# yum install -y httpd
[root@server1 haproxy]# vim /var/www/html/index.html
[root@server1 haproxy]# cat /var/www/html/index.html
正在维护中,请稍后。。。
[root@server1 haproxy]# /etc/init.d/httpd start
(3)物理机测试:物理机访问没有结果,浏览器访问错误页面重定向到server1
[root@foundation22 ~]# curl 172.25.22.1
[root@foundation22 ~]# curl 172.25.22.1
(1)编辑配置文件
将server3变成静态访问,默认的是dynamic,dynamic下是server2
重启服务,测试访到的是server2
[root@server1 haproxy]# vim haproxy.cfg
[root@server1 haproxy]# /etc/init.d/haproxy restart
[root@server1 haproxy]# curl localhost
server2
[root@server1 haproxy]# vim haproxy.cfg
[root@server1 haproxy]# /etc/init.d/haproxy restart
(3)server3安装php,编辑php默认测试页,重启httpd
[root@server3 ~]# yum install -y php
[root@server3 ~]# cd /var/www/html
[root@server3 html]# ls
index.html
[root@server3 html]# vim index.php
[root@server3 html]# cat index.php
[root@server3 html]# /etc/init.d/httpd restart
(4)打开浏览器测试:
访问172.25.22.1看到的是server2
访问172.25.22.1/index.php看到的是php的默认测试页
四、读写分离
(1)编辑配置文件,部分内容如下:
frontend public
acl read method HEAD
acl read method GET
acl write method POST
acl write method PUT
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.9.250
http-request deny if blacklist
errorloc 403 http://172.25.22.1:8080/index.html if blacklist
use_backend static if write
use_backend dynamic if read
default_backend static
# the application servers go here
backend static
balance roundrobin
server web1 172.25.22.2:80 check inter 1000
backend dynamic
balance roundrobin
server web2 172.25.22.3:80 check inter 1000
[root@server1 haproxy]# vim haproxy.cfg
[root@server1 haproxy]# /etc/init.d/haproxy restart
(3)server2在/var/www/html目录下创建两个文件和一个upload目录,给他们满权限
[root@server2 html]# chmod 777 upload
[root@server2 html]# ls
index.php upload upload_file.php
[root@server2 html]# ll
total 12
-rwxr-xr-x 1 root root 257 Apr 21 14:47 index.php
drwxrwxrwx 2 root root 4096 Apr 21 14:46 upload
-rwxr-xr-x 1 root root 929 Apr 21 14:47 upload_file.php
<1>index.php
<2>upload_file.php
0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "
";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "
";
echo "Type: " . $_FILES["file"]["type"] . "
";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";
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";
}
?>
(4)server2安装php,并重启httpd服务
[root@server2 html]# yum install -y php
[root@server2 html]# /etc/init.d/httpd restart
[root@server2 html]# scp -r * server3:/var/www/html/
[root@server3 html]# ls
index.html index.php upload upload_file.php
[root@server3 html]# rm -fr index.html
[root@server3 html]# ls
index.php upload upload_file.php
(7)编辑index.php文件,方便观察,重启服务
给submit前添加westos
[root@server3 html]# vim index.php
root@server3 html]# /etc/init.d/httpd restart
(8)打开浏览器测试:
访问172.25.22.1出现的页面中submit前有wesos,说明读到的是server3,上传图片,可以在server2的uopload目录下看到。