一、配置haproxy
1.yum install rpm-build -y
2.rpmbuild -tb haproxy-1.6.11.tar.gz
3.cd rpmbuild/RPMS/x86_64.rpm
4.rpm -qpl haproxy-1.6.11-1.x86_64.rpm
5.rpm -ivh haproxy-1.6.11-1.x86_64.rpm
6.tar zxf haproxy-1.6.11.tar.gz
7.cd haproxy-1.6.11/example/
8.cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg
9.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
retries 2
option redispatch
timeout connect 5s
timeout server 30s
timeout queue 30s
stats uri /status
stats auth admin:westos 打开网页需要认证(172.25.39.1/status)
# The public 'www' address in the DMZ
frontend public
bind 172.25.39.1:80 name clear
#use_backend static if { path_beg /img /css }
default_backend dynamic
acl blacklist src 172.25.39.250 不允许某台主机访问
http-request deny if blaclist
errorloc 403 http://172.25.39.1:8080 将拒绝信息重定向到本机8080端口
# the application servers go here
backend dynamic
balance roundrobin
server dynsrv1 172.25.39.2:80 check inter 1000
server dynsrv2 172.25.39.3:80 check inter 1000
10.groupadd -g 200 haproxy
11.useradd -g 200 -u 200 haproxy
12.su - haproxy
ulimit -a
exit
13.vim /etc/security/limits.conf
haproxy - nofile 10000
14./etc/init.d/haproxy start
输入:172.25.39.1 轮询显示
172.25.39.1/admin/status 显示图形
二、八种负载均衡算法:
balance roudrobin 轮询
balance static-rr 根据权重,建议使用
balance leastconn 最少连接着先处理,建议使用
balance source 根据请求源ip,建议使用
balance uri 根据请求的uri
balance url_param 根据参数
balance hdr(name) 根据http请求头来锁定每一次http请求
balance drdp-cookie(name)
三、php页面显示:
1.server2:
yum install php -y
cd /var/www/html
vim index.php
<?php
phpinfo?>
/etc/init.d/httpd restart
2.server1:
vim haproxy.cfg 修改此文件,添加下面这一句
use_backend dynamic if { path_end .php }
/etc/init.d/haproxy reload 重新加载此服务
四、读写分离:
1.server1:
vim haproxy.cfg 修改配置文件内容
acl write method POST
acl write method PUT
use_backend dynamic if write
/etc/init.d/haproxy reload 重新加载
2.server2:
get upload 从老师那下载
mv * /var/www/html/
chmod 777 upload
scp -r * server3:/var/www/html/
3.测试:172.25.39.1/upload
五、修改日志:
vim /etc/rsyslog.conf 修改日志位置
*.info;mail.none;authpriv.none;cron.none;local0.none /var/log/messages
local0.* /var/log/haproxy.log
$ModLoad imudp
$UDPServerRun 514
/etc/init.d/rsyslog restart