HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点, 这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
server2:172.25.13.2安装haproxy,调度server3,server4
server3: 172.25.13.3服务器
server4 :172.25.13.4服务器
[root@server2 etc]# yum install haproxy -y
[root@server2 haproxy]# ls
haproxy.cfg
[root@server2 haproxy]# vim haproxy.cfg
[root@server2 haproxy]# systemctl start haproxy
[root@server2 haproxy]# vim haproxy.cfg
[root@server2 haproxy]# systemctl reload haproxy
采用roundrobin轮询法,轮流访问两台主机
[root@server2 haproxy]# vim /etc/rsyslog.conf
[root@server2 haproxy]# systemctl restart rsyslog.service
[root@server2 haproxy]# vim haproxy.cfg
[root@server2 haproxy]# systemctl reload haproxy
权重算法测试结果server4权重为2,server3权重为1
[root@server2 haproxy]# vim haproxy.cfg
[root@server2 haproxy]# systemctl reload haproxy
[root@server2 haproxy]# echo "sorry, please try again later" > /var/www/html/index.html
[root@server3 ~]# systemctl stop httpd
[root@server3 ~]# ssh server4 systemctl stop httpd
[root@server2 haproxy]# vim haproxy.cfg
[root@server2 haproxy]# systemctl reload haproxy
[root@server2 haproxy]# vim haproxy.cfg
[root@server2 haproxy]# systemctl reload haproxy
server3发布静态页面
server4发布php动态页面
[root@server3 ~]# mkdir /var/www/html/imgaes
[root@server3 ~]# cd /var/www/html/images
[root@server3 images]# ls
redhat.jpg
[root@server3 images]# systemctl start httpd
[root@server3 images]# ssh server4 systemctl start httpd
[root@server4 ~]# vim /var/www/html/index.php
[root@server4 ~]# cat /var/www/html/index.php
测试:分别访问172.25.13.3/images/redhat.jpg
172.25.13.4/index.php
[root@server2 haproxy]# vim haproxy.cfg
[root@server2 haproxy]# systemctl reload haproxy
修改配置文件,设置默认后端172.25.13.4,发布静态页面的后端172.25.13.3,当用户访问的网址符合设置的开头和结尾,则使用静态发布页面的后端,否则使用默认后端
server3执行读操作,server4执行写操作
[root@server2 haproxy]# vim haproxy.cfg
[root@server2 haproxy]# systemctl reload haproxy
[root@server3 html]# ls
imgaes index.html index.php test.html upload upload_file.php
[root@server3 html]# chmod 777 upload/
[root@server4 html]# ls
index.html index.php test.html upload upload_file.php
[root@server4 html]# chmod 777 upload/
[root@server4 html]# ls upload/
dcb2a08d513f6616854cd96ead4746ba.gif
index.php
upload_file.php
上传图片
[root@server1 ~]# pcs resource create vip ocf:heartbeat:IPaddr2 ip=172.25.13.100 op monitor interval=30s
[root@server1 ~]# pcs resource create haproxy systemd:haproxy op monitor interval=30s
[root@server1 ~]# pcs resource group add hagroup vip haproxy