使用haproxy+apache搭建负载均衡

搭建环境:

两台centos。

主机一:192.168.1.6  安装haproxy以及apche

主机二:192.168.1.7 安装apache

主机一配置:

使用本地yum源。

安装haproxy、apache:

[root@6 ~]# yum -y install haproxy  httpd

[root@6 conf.d]# vim /etc/httpd/conf.d/web.conf

扫描二维码关注公众号,回复: 9666002 查看本文章

[root@6 ~]# vim /etc/httpd/conf/httpd.conf

去掉 listen 80 

写一个访问页面:

[root@6 ~]# echo "<h1>我是主机一(haproxy)。</h1>" > /var/www/html/index.html

root@6 ~]# systemctl restart httpd.service

访问:

[root@6 ~]# curl 192.168.1.6:8181
<h1>我是主机一(haproxy)。</h1>

配置haproxy:

[root@6 ~]# vim /etc/haproxy/haproxy.cfg  #最后一行、默认使用轮询

[root@6 ~]# systemctl restart haproxy

主机二配置:

[root@7 ~]# yum -y install httpd

[root@7 ~]# vim /etc/httpd/conf.d/web.conf

[root@7 ~]# vim /etc/httpd/conf/httpd.conf

去掉 listen 80 

[root@7 ~]# systemctl restart httpd

[root@7 ~]# echo "<h1>我是主机二。(haproxy)</h1>" > /var/www/html/index.html

测试:

[root@7 ~]# curl 192.168.1.7:8181
<h1>我是主机二。(haproxy)</h1>

测试:

刷新:

猜你喜欢

转载自www.cnblogs.com/meml/p/12443398.html
今日推荐