使用haproxy+apache搭建负载均衡
搭建环境:
两台centos。
主机一:192.168.1.6 安装haproxy以及apche
主机二:192.168.1.7 安装apache
主机一配置:
使用本地yum源。
安装haproxy、apache:
[ ~]# yum -y install haproxy httpd
[ conf.d]# vim /etc/httpd/conf.d/web.conf
[ ~]# vim /etc/httpd/conf/httpd.conf
去掉 listen 80
写一个访问页面:
[ ~]# echo "<h1>我是主机一(haproxy)。</h1>" > /var/www/html/index.html
~]# systemctl restart httpd.service
访问:
[ ~]# curl 192.168.1.6:8181
<h1>我是主机一(haproxy)。</h1>
配置haproxy:
[ ~]# vim /etc/haproxy/haproxy.cfg #最后一行、默认使用轮询
[ ~]# systemctl restart haproxy
主机二配置:
[ ~]# yum -y install httpd
[ ~]# vim /etc/httpd/conf.d/web.conf
[ ~]# vim /etc/httpd/conf/httpd.conf
去掉 listen 80
[ ~]# systemctl restart httpd
[ ~]# echo "<h1>我是主机二。(haproxy)</h1>" > /var/www/html/index.html
测试:
[ ~]# curl 192.168.1.7:8181
<h1>我是主机二。(haproxy)</h1>
测试:
刷新: