存档

2017年8月 的存档

使用 HAProxy 做负载均衡(转)

2017年8月7日 没有评论

HAProxy 表示 High Availability Proxy ,是开源的负载均衡与代理软件。GitHub,Imgur,Instagram,Twitter 都在用它,阿里云的 SLB 服务也是基于 HAProxy 做的。

虚拟机

balancer:192.168.33.60
web1:192.168.33.61
web2:192.168.33.62
database:192.168.33.63
安装 HAProxy

yum install haproxy -y
systemctl start haproxy
systemctl enable haproxy
HAProxy 配置

HAProxy 的配置文件分成了两大部分:

Global:设置进程范围的参数。
Proxies:defaults,listen,frontend,backend …
HAProxy 配置:Global

复制默认的 haproxy.cfg 文件:

cd /etc/haproxy; sudo cp haproxy.cfg haproxy.cfg.bak
打开 haproxy.cfg :

vi /etc/haproxy/haproxy.cfg
你会看到这里已经定义了两个部分,global 还有 defaults 。

在 defaults 里面,查找:

mode http
option httplog
把 http 替换成 tcp :

mode tcp
option tcplog
HAProxy 配置:Proxies

frontend www
bind 192.168.33.60:80
default_backend web-backend
然后继续添加:

backend web-backend
balance roundrobin
mode tcp
server web1 192.168.33.61:80 check
server web2 192.168.33.62:80 check
重启 HAProxy

systemctl restart haproxy