|
负载均衡的两种方式:
1,基于操作系统的 (LVS 基于 Linux操作系统实现的一种软负载)
2,基于第三方软件的(HAProxy 开源的基于第三方软件的软负载)
------------------------------------------------------------------------------
haproxy支持两种主要代理模式:
1,tcp(第四层)
在客户端和服务器之间转发双向流量
2,http(第七层)
haproxy会分析协议,并且能通过允许·拒绝·交换·修改或者删除请求(request)或者回应(response)指定内容来控制协议
准备3台机器
haproxy 安装
haproxy:192.168.2.33
client1:192.168.2.35
client2:192.168.2.36
在haproxy(192.168.2.33)机器上安装haproxy
1,#wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz
2,#tar -zcvf haproxy-1.3.20.tar.gz >/dev/null
3,#cd haproxy-1.3.20
4,#make TARGET=linux26 PREFIX=/usr/local/haproxy
5,# make install PREFIX=/usr/local/haproxy
haproxy 配置
1,#cp haproxy.cfg /usr/local/haproxy/ (由于目录下默认没有配置文件,拷贝haproxy-1.3.20/example文件夹下的 haproxy.cfg到 /usr/local/haproxy 目录下)
2,#cd /usr/local/haproxy
haproxy.cfg
| global
maxconn 5120
chroot /usr/local/haproxy
user haproxy
group haproxy
daemon
quiet
nbproc 2
pidfile /usr/local/haproxy/haproxy.pid
defaults
log global
mode http
option httplog
option dontlognull
log 127.0.0.1 local3
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen web :80
mode http
balance roundrobin
option httpclose
option forwardfor
server client1 192.168.2.36:80 check weight 1 minconn 1 maxconn 3 check inter 40000
server client2 192.168.2.35:80 check weight 1 minconn 1 maxconn 3 check inter 40000
listen stats :8888
mode http
transparent
stats uri /haproxy-stats
stats realm Haproxy \ statistic
stats auth haproxy:password
|
启动haproxy
./haproxy ../haproxy.cfg
在client1,client2上启动 apache
#systemctl httpd.service
注意:默认apache是访问/var/www/html/下的index.html文件.
http://192.168.2.33::80
http://192.168.2.33:8888/haproxy-stats
添加VIP(虚拟ip)
#ifconfig eth0:0 192.168.2.40 netmask 255.255.255.0 up
(把这句下载 /etc/rc.local中也可以,如此不怕断电后不能使用)
|
|
|