HAProxy 配置负载均衡

 

准备条件
 
1. 安装好HAProxy
 
2. 启动两个web应用服务器,这里启动了两个nginx
 
nginx1 端口 8080
 
nginx2 端口 8081
 
都有一个页面 test.html,显示的内容不同,nginx1 里的test.html显示 1,nginx2 里的test.html显示 2
 
目标
 
配置HAProxy做为前端的负载均衡服务器,通过HAProxy访问test.html,动态指向nginx1或nginx2,页面显示 1 或 2,多次刷新页面,应看到不同输出
 
配置文件
 
在HAProxy的安装目录下,新建 conf 目录,在conf目录下新建文件 haproxy.cfg ,内容:
 
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:7000
default_backend neo4j
backend neo4j
server s1 127.0.0.1:8080 maxconn 32 
server s2 127.0.0.1:8081 maxconn 32 
listen admin
bind *:7080
stats enable
 
说明:
 
frontend http-in
bind *:7000
 
这两行指定了HAProxy监听 7000端口
 
server s1 127.0.0.1:8080 maxconn 32 
server s2 127.0.0.1:8081 maxconn 32 
 
这两行指定了两个应用服务器的地址和端口
 
 
启动HAProxy
 
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
 
访问测试
 
http://服务器地址:7000/test.html
 
多次刷新页面,可以看到交替输出 1 和 2,说明HAProxy已经配置成功,把请求动态分配给了不同的应用服务器
posted @ 2018-08-12 00:50  micwin  阅读(551)  评论(0编辑  收藏  举报