haproxy(使用RPM工具创建安装包、动态轮询、网页自动更新、添加日志、限制访问、动静分离、读写分离)
一、实验环境:
server1:172.25.55.1(rhel7.3虚拟机,关闭防火墙与内核级防火墙)
server2:172.25.55.2(rhel7.3虚拟机,关闭防火墙与内核级防火墙,下载httpd便编辑好网页文件)
server3:172.25.55.3(rhel7.3虚拟机,关闭防火墙与内核级防火墙,下载httpd便编辑好网页文件)
二、使用RPM工具创建haprox安装包
[[email protected] ~]# ls
haproxy-1.7.3.tar.gz
[[email protected] ~]# tar zxf haproxy-1.7.3.tar.gz
[[email protected] ~]# yum install rpm-build -y(下载创建rpm包的软件)
[[email protected] ~]# rpmbuild -tb haproxy-1.7.3.tar.gz (创建haproxy安装包)
[[email protected] ~]# cd rpmbuild/
[[email protected] rpmbuild]# cd RPMS/x86_64/
[[email protected] x86_64]# ls
haproxy-1.7.3-1.x86_64.rpm(查看到创建的haproxy)
三、安装haproxy
[[email protected] x86_64]# yum install haproxy-1.7.3-1.x86_64.rpm -y
[[email protected] x86_64]# cd
[[email protected] ~]# cd haproxy-1.7.3
[[email protected] haproxy-1.7.3]# cd examples/
[[email protected] examples]# cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg(复制haproxy的配置文件)
四、设置动态轮询
-
编辑haproxy的配置文件:
[[email protected] examples]# vim /etc/haproxy/haproxy.cfg 10 global 11 maxconn 10000 12 stats socket /var/run/haproxy.stat mode 600 level admin 13 log 127.0.0.1 local0 14 uid 200(与创建的用户、组的id号对应) 15 gid 200 16 chroot /var/empty 17 daemon 18 defaults 19 20 mode http 21 log global 22 option httplog 23 option dontlognull 24 monitor-uri /monitoruri 25 maxconn 8000 26 timeout client 30s 27 28 stats uri /admin/stats 29 option prefer-last-server 30 retries 2 31 option redispatch 32 timeout connect 5s 33 timeout server 5s 34 35 # The public 'www' address in the DMZ 36 frontend public 37 bind *:80 name clear 38 #bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem 39 40 41 # use_backend static if { hdr_beg(host) -i img } 42 # use_backend static if { path_beg /img /css } 43 default_backend dynamic 44 45 # the application servers go here 46 backend dynamic 47 balance roundrobin 48 server dynsrv1 172.25.55.2:80 check inter 1000 49 server dynsrv2 172.25.55.3:80 check inter 1000
-
创建用户、组
[[email protected] examples]# groupadd -g 200 haproxy(创建用户) [[email protected] examples]# useradd -u 200 -g 200 haproxy(创建组)
-
开启服务
[[email protected] examples]# /etc/init.d/haproxy start
-
服务端测试
五、设置网页五秒更新
我们访问http://172.25.55.1/admin/stats网页数据是不会自动刷新的。(如下图)
编辑配置文件
[[email protected] examples]# vim /etc/haproxy/haproxy.cfg
28 stats uri /admin/stats
29 stats auth admin:westos(认证的用户和密码)
30 stats refresh 5s(刷新的时间)
[[email protected] examples]# /etc/init.d/haproxy restart(重启服务)
访问网页要通过认证
此时数据可以五秒进行一次刷新。
六、添加日志
[[email protected] haproxy]# vim /etc/rsyslog.conf
13 $ModLoad imudp
14 $UDPServerRun 514
61 local0.* /var/log/haproxy.log
[[email protected] haproxy]# /etc/init.d/rsyslog restart
可查看日志
七、限制访问
[[email protected] examples]# vim /etc/haproxy/haproxy.cfg
43 acl blacklist src 172.25.55.250(黑名单是ip为172.25.55.250的主机)
44
45 http-request deny if blacklist(如果是黑名单中的ip访问被拒绝)
46
47 errorloc 403 http://172.25.55.1:8080/index.html(此时显示的网页是172.25.55.1端口为8080的服务的编辑的index.html网页)
48
49 default_backend dynamic
[[email protected] html]# /etc/init.d/haproxy restart(重启服务)
[[email protected] examples]# yum install httpd -y(安装httpd)
[[email protected] examples]# vim /etc/httpd/conf/httpd.conf (修改端口号)
Listen 8080
[[email protected] examples]# cd /var/www/html/
[[email protected] html]# vim index.html(编辑网页)
你被拉黑
用172.25.55.250访问网页:
八、动静分离
[[email protected] html]# vim /etc/haproxy/haproxy.cfg
43 #acl blacklist src 172.25.55.250(为检测新实验方便注释掉拉黑)
44
45 #http-request deny if blacklist
46
47 #errorloc 403 http://172.25.55.1:8080/index.html
48 use_backend dynamic if { path_end .php }
49 default_backend static
52 backend static
53 balance roundrobin
54 server dynsrv1 172.25.55.2:80 check inter 1000
55
56 backend dynamic
57 balance roundrobin
58 server dynsrv2 172.25.55.3:80 check inter 1000
[[email protected] html]# /etc/init.d/haproxy restart
访问网页:
九、读写分离
[[email protected] ~]# vim /etc/haproxy/haproxy.cfg
48 acl write method POST
49 acl write method PUT
50
51 use_backend static if write
52
53
54 default_backend static(默认后端是静态)
55
56 # the application servers go here
57 backend static(后端是静态)
58 balance roundrobin
59 server dynsrv1 172.25.55.2:80 check inter 1000
60
61
62 backend dynamic
63 balance roundrobin
64 server dynsrv2 172.25.55.3:80 check inter 1000
[[email protected] ~]# /etc/init.d/haproxy restart
要下载一个upload包,
[[email protected] upload]# cd -
/var/www/html
[[email protected] html]# chmod 777 upload
[[email protected] html]# cd upload/
[[email protected] upload]# mv * ..
[[email protected] upload]# cd -
/var/www/html
[[email protected] html]# ls
index.html index.php upload upload_file.php
[[email protected] html]# vim upload_file.php
5 && ($_FILES["file"]["size"] < 2000000))(扩大文件接收的数据)
[[email protected] html]# scp -rp upload upload_file.php index.php [email protected]:/var/www/html/
浏览网页、上传图片。
上传成功
在server2下查看到上传的文件。
在添加一个虚拟机server4,编辑配置文件。
[[email protected] x86_64]# scp haproxy-1.7.3-1.x86_64.rpm [email protected]:
[[email protected] ~]# cd /etc/haproxy/
[[email protected] haproxy]# ls
haproxy.cfg
[[email protected] haproxy]# scp haproxy.cfg [email protected]:/etc/haproxy
[[email protected] haproxy]# vim /etc/haproxy/haproxy.cfg
47 acl read method GET
48 acl read method HEAD
49
50 use_backend dynamic if read
51
52
53 default_backend static(默认后端是静态)
54
55 # the application servers go here
56 backend dynamic
57 balance roundrobin
58 server dynsrv1 172.25.55.2:80 check inter 1000
59 backend static(后端是静态)
60 balance roundrobin
61 server dynsrv1 172.25.55.3:80 check inter 1000
上传图片后:
在server3可查看到上传的文件。