编译安装HAproxy
具体的安装方法和注意事项请参考源码包中自带的README文件
这里安装的是haproxy-1.6.4
系统为centos6.4 i386
一.安装声明
1.To build haproxy, you will need :
- GNU make. Neither Solaris nor OpenBSD's make work with the GNU Makefile.
If you get many syntax errors when running "make", you may want to retry
with "gmake" which is the name commonly used for GNU make on BSD systems.
- GCC between 2.95 and 4.8. Others may work, but not tested.
- GNU ld
Also, you might want to build with libpcre support, which will provide a very
efficient regex implementation and will also fix some badness on Solaris' one.
2.To build haproxy, you have to choose your target OS amongst the following ones
and assign it to the TARGET variable :
- linux22 for Linux 2.2
- linux24 for Linux 2.4 and above (default)
- linux24e for Linux 2.4 with support for a working epoll (> 0.21)
- linux26 for Linux 2.6 and above
- linux2628 for Linux 2.6.28, 3.x, and above (enables splice and tproxy)
- solaris for Solaris 8 or 10 (others untested)
- freebsd for FreeBSD 5 to 10 (others untested)
- netbsd for NetBSD
- osx for Mac OS/X
- openbsd for OpenBSD 3.1 and above
- aix51 for AIX 5.1
- aix52 for AIX 5.2
- cygwin for Cygwin
- generic for any other OS or version.
- custom to manually adjust every setting
3.You may also choose your CPU to benefit from some optimizations. This is
particularly important on UltraSparc machines. For this, you can assign
one of the following choices to the CPU variable :
- i686 for intel PentiumPro, Pentium 2 and above, AMD Athlon
- i586 for intel Pentium, AMD K6, VIA C3.
- ultrasparc : Sun UltraSparc I/II/III/IV processor
- native : use the build machine's specific processor optimizations. Use with
extreme care, and never in virtualized environments (known to break).
- generic : any other processor or no CPU-specific optimization. (default)
二.编译安装
1.
[root@localhost haproxy-1.6.4]# uname -r
2.6.32-431.el6.i686
2.[root@localhost haproxy-1.6.4]# pwd
/root/haproxy-1.6.4
[root@localhost haproxy-1.6.4]# make TARGET=linux26 ARCH=i386
[root@localhost haproxy-1.6.4]# make install
install -d "/usr/local/sbin"
install haproxy "/usr/local/sbin"
install -d "/usr/local/share/man"/man1
install -m 644 doc/haproxy.1 "/usr/local/share/man"/man1
install -d "/usr/local/doc/haproxy"
for x in architecture close-options configuration cookie-options intro linux-syn-cookies lua management network-namespaces proxy-protocol; do \
install -m 644 doc/$x.txt "/usr/local/doc/haproxy" ; \
done
三.其他事项
1.创建配置文件
[root@localhost haproxy-1.6.4]# pwd
/root/haproxy-1.6.4
[root@localhost haproxy-1.6.4]# cp -p examples/option-http_proxy.cfg /etc/haproxy/haproxy.cfg
或者
将准备好的内容直接覆盖源配置文件,这里我用的是后面的配置(最后面有),未用option-http_proxy.cfg配置;
2.创建启动文件(也可以无需启动文件来启动服务)
[root@localhost haproxy-1.6.4]# cp -p examples/haproxy.init /etc/rc.d/init.d/haproxy
[root@localhost haproxy-1.6.4]# chmod +x /etc/rc.d/init.d/haproxy
[root@localhost haproxy-1.6.4]# chkconfig --add haproxy
3.启动haproxy服务
因为配置文件中说明了运行haproxy的用户和组为haproxy,因此先创建:
[root@localhost haproxy-1.6.4]# useradd haproxy
[root@localhost haproxy-1.6.4]# groupadd haproxy
启动服务时出现以下错误:
[root@localhost haproxy]# service haproxy start
/etc/init.d/haproxy: line 98: /usr/sbin/haproxy: No such file or directory
Errors found in configuration file, check it with 'haproxy check'.
解决办法:
编辑/etc/rc.d/init.d/haproxy,将第98行的
BIN=/usr/sbin/$BASENAME 改为
BIN=/usr/local/sbin/$BASENAME
因为安装时显示install haproxy /usr/local/sbin而不是/usr/sbin
再次启动:
[root@localhost haproxy]# service haproxy start
Starting haproxy: [ALERT] 099/171716 (21186) : [/usr/local/sbin/haproxy.main()] Cannot chroot(/var/lib/haproxy).
[FAILED]
解决办法:将配置文件中的chroot /var/lib/haproxy一行注释掉
再次启动(注意,此时的80端口不要被占用):
[root@localhost haproxy]# service haproxy start
Starting haproxy: [ OK ]
[root@localhost haproxy]# netstat -ntlp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 21201/haproxy
另一种启动方式(如果出现错误请根据提示解决):
[root@localhost haproxy]# haproxy -f /etc/haproxy/haproxy.cfg
[root@localhost haproxy]# netstat -ntlp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 21244/haproxy
4.测试设置的监控页面
浏览器地址栏中输入ip:1080/haproxyadmin?stats显示如下这说明没问题
附:haproxy.cfg配置文件示例
global
log 127.0.0.1 local2
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 30000
listen stats
mode http
bind 0.0.0.0:1080
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
frontend http-in
bind *:80
mode http
log global
option httpclose
option logasap
option dontlognull
capture request header Host len 20
capture request header Referer len 60
default_backend servers
frontend healthcheck
bind :1099
mode http
option httpclose
option forwardfor
default_backend servers
backend servers
balance roundrobin
server websrv1 192.168.85.156:80 check maxconn 2000
server websrv2 192.168.85.157:80 check maxconn 2000