1,安装HAProxy
1
2
3
4
5
6
7
|
wget http:
//haproxy
.1wt.eu
/download/1
.4
/src/haproxy-1
.4.21.
tar
.gz
tar
zxf haproxy-1.4.21.
tar
.gz
cd
haproxy-1.4.21
#发现里面没有.configure文件
uname
-r
#查看内核版本,下面的TARGET参数要用到
make
TARGET=linux26 PREFIX=
/usr/local/haproxy
make
install
PREFIX=
/usr/local/haproxy
useradd
-r haproxy
#增加执行用户
|
2,配置与测试haproxy
在编辑配置文件之前当然要了解清楚主要的配置项啦,可以参考这里:http://cbonte.github.com/haproxy-dconv/configuration-1.4.html。
下面来实现一个简单的1+2负载均衡
如图:
参考最简单的一个配置来新建配置文件 vim /etc/haproxy.conf ,然后加入下面的内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# Simple configuration for an HTTP proxy listening on port 80 on all
# interfaces and forwarding requests to a single backend "servers" with
# two servers listening on 127.0.0.1:8000 and 127.0.0.1:8081
global
daemon
group haproxy
user haproxy
maxconn 256
defaults
mode http
timeout connect 1000ms
timeout client 10000ms
timeout server 10000ms
frontend http-in
bind *:80
default_backend servers
backend servers
server server1 127.0.0.1:8080 maxconn 32
server server2 127.0.0.1:8081 maxconn 32
|
其中127.0.0.1:8080和127.0.0.1:8081可以简单配置nginx或者httpd,这里不展开说明。
1
2
3
4
|
#启动haproxy
/usr/local/haproxy/sbin/haproxy
-f
/etc/haproxy
.conf
#停止haproxy
kill
[haproxy的pid]
|
最后通过浏览器访问 http://192.168.1.27 查看效果。