实验环境
| 主机 | 服务 |
|---|---|
| server1:172.25.23.1 | haproxy |
| server2:172.25.23.2 | apache |
| server3:172.25.23.3 | apache |
| 物理机:172.25.23.250 | 测试 |
一、Haproxy实现负载均衡
1.下载并解压haproxy安装包
[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

2.用find . -name *.spec命令查看,有spec才可以搭成rpm包
[root@server1 ~]# cd haproxy-1.7.3
[root@server1 haproxy-1.7.3]# find . -name *.spec
./examples/haproxy.spec

3.安装源码编译软件
[root@server1 ~]#yum install rpm-build -y
[root@server1 ~]# yum install gcc -y
[root@server1 ~]# yum install pcre-devel -y



4.源码编译haproxy软件
[root@server1 ~]# rpmbuild -tb haproxy-1.7.3.tar.gz

5.编译完成后,会生成rpmbuild目录,切换到rpmbuild/RPMS/ x86_64/目录下,安装haproxy-1.7.3-1.x86_64软件
[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

6.切换到examples目录下,找到haproxy的配置文件,复制配置文件到/etc/haproxy/目录下
[root@server1 ~]# cd haproxy-1.7.3
[root@server1 haproxy-1.7.3]# cd examples/
[root@server1 examples]# ls
[root@server1 examples]# cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg
[root@server1 examples]# cd


7.进入/etc/haproxy/目录下,编辑haproxy配置文件
[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
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 dynamic
# the application servers go here
backend dynamic
balance roundrobin
server web1 172.25.23.2:80 check inter 1000
server web2 172.25.23.3:80 check inter 1000

8.在server2和server3上安装httpd,并编写默认发布文件 ,打开httpd服务
[root@server2 ~]# yum install httpd -y
[root@server2 ~]# cat /var/www/html/index.html
server2
[root@server2 ~]# /etc/init.d/httpd start



[root@server3 ~]# yum install httpd -y
[root@server3 ~]# vim /var/www/html/index.html
[root@server3 ~]# cat /var/www/html/index.html
server3
[root@server3 ~]# /etc/init.d/httpd start



9.开启haproxy服务
[root@server1 haproxy]# /etc/init.d/haproxy start

10.测试:curl 172.25.23.1 轮询
[root@foundation23 ~]# curl 172.25.23.1
server2
[root@foundation23 ~]# curl 172.25.23.1
server3
[root@foundation23 ~]# curl 172.25.23.1
server2
[root@foundation23 ~]# curl 172.25.23.1
server3

11.在浏览器中输入172.25.23.1:/admin/stats,可以查看状态

12.在浏览器中输入172.25.23.1:/monitoruri

二、设置用户、密码以及刷次数
1.编辑配置文件
[root@server1 haproxy]# vim haproxy.cfg

设置用户为admin,密码为westos,刷新次数为5秒刷新一次

2.重启服务
[root@server1 haproxy]# /etc/init.d/haproxy restart

3.测试:在浏览器中输入172.25.23.1:/admin/stats,显示要输入密码,页面每隔5秒刷新一次



- 指定日至存放目录
1.修改日志服务配置文件
[root@server1 haproxy]# vim /etc/rsyslog.conf


2.重启日至服务
[root@server1 haproxy]# /etc/init.d/rsyslog restart

tail -f /var/log/可以查看到日至内容,且5秒内刷新一次
- 设置访问黑名单
1.编辑配置文件
[root@server1 haproxy]# vim haproxy.cfg


2.重启服务

3.浏览器中访问172.25.23.1,显示403报错

4.将403报错给客户不太好,编辑配置文件将错误重定向到本机apache服务默认发布文件


5.安装httpd,编辑默认发布文件


6.修改httpd的端口为8080,避免与haproxy服务冲突


7.重启haproxy、httpd服务
[root@server1 haproxy]# /etc/init.d/haproxy restart
[root@server1 ~]# /etc/init.d/httpd restart

8.浏览器中访问172.25.23.1,显示默认发布文件内容

三、动态页面和静态页面分离
1.server3上安装php服务,编辑动态页面
[root@server3 ~]# yum install php -y
[root@server3 ~]# cd /var/www/html
[root@server3 html]# vim index.php
[root@server3 html]# cat index.php
<?php
phpinfo()
?>


2.重启httpd服务
[root@server3 html]# /etc/init.d/httpd restart

3.编辑配置文件
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg


4.重启haproxy服务

5.测试:浏览器中访问172.25.23.1/index.php显示server3的php页面
浏览器中访问172.25.23.1/index.html显示server2的静态页面


四、读写分离
1.在server2中,切换到upload/下,把upload/目录下所有文件移到默认发布目录下,删除index.html文件
[root@server2 html]# ls
index.html upload
[root@server2 html]# cd upload/
[root@server2 upload]# ls
index.php upload_file.php
[root@server2 upload]# mv * ..
[root@server2 upload]# ls
[root@server2 upload]# cd ..
[root@server2 html]# ls
index.html index.php upload upload_file.php
[root@server2 html]# rm -fr index.html
[root@server2 html]# ls

2.给upload目录添加权限,把/var/www/html目录下所有文件复制到server3的/var/www/html目录下,删除server3的index.html文件
[root@server2 html]# chmod 777 upload
[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

3.server2上安装php
[root@server2 html]# yum install php -y

4.server1上编辑配置文件,如果有写入操作,访问动态server2主机
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg



5.重启haproxy服务
[root@server1 ~]# /etc/init.d/haproxy restart

6.编辑upload_file.php文件内容(上传图片的动态页面)
[root@server2 html]# vim upload_file.php

修改上传文件的大小

7.server3上编辑index.php 文件内容(选择图片的静态页面)
[root@server3 html]# vim index.php


8.重启httpd服务
[root@server2 html]# /etc/init.d/httpd restart
[root@server3 html]# /etc/init.d/httpd restart


8.测试:浏览器中输入:172.25.23.1/index.php
(1)出现以下界面,点击Browse

(2)上传一个图片

(3)图片上传成功点击westosSubmit

(4)上传成功显示如下:

(5)切换到/var/www/html/upload目录下,可以看到上传的图片上传到了server2上

(6)而server3上没有

本文详细介绍了如何使用Haproxy实现负载均衡,包括配置用户密码、日志路径设置、访问黑名单及动静态页面分离。同时,通过读写分离确保动态请求和静态资源的有效管理。通过实验步骤,展示了从安装Haproxy到配置负载均衡、用户认证、日志存储、错误处理以及动态页面和静态页面的分离过程。

被折叠的 条评论
为什么被折叠?



