Haproxy学习
1 安装
源代码:
1) 拷贝
2) 解压
3) 运行 make install 即完成安装,安装路径
运行程序:/usr/local/sbin/haproxy
文档:/usr/local/doc/haproxy
Man:/usr/local/share/man/man1
2 初步配置、运行及测试
1) 在 /usr/local/sbin下建配置文件ha.conf,内容如下:
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:80
default_backend servers
backend servers
server server1 127.0.0.1:8080 maxconn 32
2) 启动tomcat服务,服务端口:8080
3) 启动haproxy,命令:haproxy -f ha.conf
4) 验证:在其它机器上访问http://IP:8080可以打开tomcat首页面
同样,通过http://IP也可以打开tomcat首页面,表明安装配置成功。
3 负载均衡简单测试
1) 复制并修改tomcat的端口号为8000,启动,测试http 8000端口
2) 修改8000端口下ROOT下的index.html,Title部分改为Apache Tomcat-8000
3) 修改haproxy配置文件,增加一行server server2 127.0.0.1:8000 maxconn 32
4) 重新启动,命令haproxy –f ha.conf –st [pid],pid为原来haproxy的进程号
5) http://IP多次快速访问验证,两个页面Title会有变化
表明负载均衡在起作用。
4 运行命令
[root@localhost sbin]# haproxy
HA-Proxy version 1.4.15 2011/04/08
Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Usage : haproxy [-f <cfgfile>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ]
[ -p <pidfile> ] [ -m <max megs> ]
-v displays version ; -vv shows known build options.
-d enters debug mode ; -db only disables background mode.
-V enters verbose mode (disables quiet mode)
-D goes daemon
-q quiet mode : don't display messages
-c check mode : only check config files and exit
-n sets the maximum total # of connections (2000)
-m limits the usable amount of memory (in MB)
-N sets the default, per-proxy maximum # of connections (2000)
-p writes pids of all children to this file
-sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.
5 插入cookie持久负载均衡
配置文件如下:
global
daemon
maxconn 256
defaults
mode http
cookie machine insert
balance rdp-cookie(machine)
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:80
default_backend servers
backend servers
server server1 127.0.0.1:8080 cookie first maxconn 32
server server2 127.0.0.1:8000 cookie second maxconn 32
1) Haproxy接收到浏览器请求,如果没有cookie machine,则顺序抽取一个server请求
2) haproxy在回应给浏览器之前,根据内容来自server1还是server2来插入cookie(machine=first 或者machine=second)
3) 浏览器接收到回应的cookie,再次访问的时候会带上cookie
4) Haproxy根据cookie machine的值来判断应该将请求转到对应的server
6 配置详解
6.1 Global参数
* 进程管理及安全Process management and security
- chroot
- daemon
- gid
- group
- log
- log-send-hostname
- nbproc
- pidfile
- uid
- ulimit-n
- user
- stats
- node
- description
* 性能调优Performance tuning
- maxconn
- maxpipes
- noepoll
- nokqueue
- nopoll
- nosepoll
- nosplice
- spread-checks
- tune.bufsize
- tune.chksize
- tune.maxaccept
- tune.maxpollevents
- tune.maxrewrite
- tune.rcvbuf.client
- tune.rcvbuf.server
- tune.sndbuf.client
- tune.sndbuf.server
* 调试Debugging
- debug
- quiet
6.2 Proxies
----------
Proxy configuration can be located in a set of sections :
- defaults <name>
- frontend <name>
- backend <name>
- listen <name>
A "defaults" section sets default parameters for all other sections following
its declaration. Those default parameters are reset by the next "defaults"
section. See below for the list of parameters which can be set in a "defaults"
section. The name is optional but its use is encouraged for better readability.
A "frontend" section describes a set of listening sockets accepting client
connections.
A "backend" section describes a set of servers to which the proxy will connect
to forward incoming connections.
A "listen" section defines a complete proxy with its frontend and backend
parts combined in one section. It is generally useful for TCP-only traffic.