提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
目录
提示:以下是本篇文章正文内容,下面案例可供参考
一、搭建环境
1)准备三台虚拟机
HAPROXY 192.168.115.212 WEB1 192.168.115.211 WEB2 192.168.115.210
2) 安装haproxy-1.7.2
wget https://www.haproxy.org/download/1.7/src/haproxy-1.7.2.tar.gz
3)解压
tar xf haproxy-1.7.2.tar.gz
4) 进入解压好的文件
cd haproxy-1.7.2
5) 编译安装
6)创建haproxy 配置文件
二、搭建七层负载haproxy
1.修改配置文件
vim /etc/haproxy/haproxy.haproxy.cfg
global
daemon
maxconn 256
pidfile /var/run/haproxy/haproxy.pid
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:80
default_backend servers
backend servers
server server1 192.168.115.211:80 maxconn 32
server server2 192.168.115.210:80 maxconn 32
2 WEB1、2写访问文件
web1
echo web1 > /var/www/html/index.html
web2
echo web1web2 > /var/www/html/index.html
3.启停
systemctl daemon-reload
systemctl restart haproxy
4.验证
三、四层负载haproxy
跟上面做的七层差不多
global
daemon
maxconn 256
pidfile /var/run/haproxy/haproxy.pid
defaults
mode tcp
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:80
default_backend servers
backend servers
server server1 192.168.115.211:80 maxconn 32
server server2 192.168.115.210:80 maxconn 32
balance roundrobin
#balance source
重启haproxy
service haproxy start
service haproxy stop
service haproxy restart | reload
也可以使用systemctl进行启停