haproxy的官方地址:http://www.haproxy.org
HAproxy的官方学习文档:http://cbonte.github.io/haproxy-dconv/1.7/intro.html
下载:http://www.haproxy.org/download/1.7/src/haproxy-1.7.2.tar.gz
# tar xzf haproxy-1.7.2.tar.gz # cd haproxy-1.7.2 # make PREFIX=/usr/local/haproxy/ TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 # make install PREFIX=/usr/local/haproxy/
安装可参考:https://github.com/haproxy/haproxy-1.5
Tips 1:为什么目录中没有configure?先来熟悉下一般软件的安装过程
- 使用configure配置软件(例如:prefix=/usr/local/xxx),会生成 Makefile
- make,开始编译,把源代码编译成可执行二进制代码
- make install,把可执行程序移动到指定位置,权限赋值等操作
而在HAproxy中已经存在配置好的 Makefile 文件,所以直接执行 make 命令即可。
Tips 2:为什么在make 和 make install 中,可以需要指定PREFIX 、TARGET呢?
从 1 中的安装过程可以看出来,安装的重要过程是make 编译,只要有Makefile 文件,就可以执行make,而Makefile 其实是一个 shell 脚本,从HAproxy的Makefile的shell脚本中,可以看到是允许传入prefix和Target参数的,打开Makefile的脚本,也可以看到默认的PREFIX=/usr/local ,而此处指定为/usr/local/haproxy/。至于TARGET参数,在Makefile的文件中,也可以看到,类似于
ifeq ($(TARGET),generic) # generic system target has nothing specific USE_POLL = implicit USE_TPROXY = implicit else ifeq ($(TARGET),haiku) # For Haiku TARGET_LDFLAGS = -lnetwork USE_POLL = implicit USE_TPROXY = implicit else ifeq ($(TARGET),linux22) # This is for Linux 2.2 USE_GETSOCKNAME = implicit USE_POLL = implicit USE_TPROXY = implicit USE_LIBCRYPT = implicit USE_DL = implicit else ifeq ($(TARGET),linux24) # This is for standard Linux 2.4 with netfilter but without epoll() USE_GETSOCKNAME = implicit USE_NETFILTER = implicit USE_POLL = implicit USE_TPROXY = implicit USE_LIBCRYPT = implicit USE_DL = implicit else ifeq ($(TARGET),linux24e) # This is for enhanced Linux 2.4 with netfilter and epoll() patch > 0.21 USE_GETSOCKNAME = implicit USE_NETFILTER = implicit USE_POLL = implicit USE_EPOLL = implicit USE_MY_EPOLL = implicit USE_TPROXY = implicit USE_LIBCRYPT = implicit USE_DL = implicit else ifeq ($(TARGET),linux26) # This is for standard Linux 2.6 with netfilter and standard epoll() USE_GETSOCKNAME = implicit USE_NETFILTER = implicit USE_POLL = implicit USE_EPOLL = implicit USE_TPROXY = implicit USE_LIBCRYPT = implicit USE_FUTEX = implicit EXTRA += haproxy-systemd-wrapper USE_DL = implicit else ifeq ($(TARGET),linux2628) # This is for standard Linux >= 2.6.28 with netfilter, epoll, tproxy and splice USE_GETSOCKNAME = implicit USE_NETFILTER = implicit USE_POLL = implicit USE_EPOLL = implicit USE_TPROXY = implicit USE_LIBCRYPT = implicit USE_LINUX_SPLICE= implicit USE_LINUX_TPROXY= implicit USE_ACCEPT4 = implicit USE_FUTEX = implicit USE_CPU_AFFINITY= implicit ASSUME_SPLICE_WORKS= implicit EXTRA += haproxy-systemd-wrapper USE_DL = implicit else ifeq ($(TARGET),solaris) # This is for Solaris 8 # We also enable getaddrinfo() which works since solaris 8. USE_POLL = implicit TARGET_CFLAGS = -fomit-frame-pointer -DFD_SETSIZE=65536 -D_REENTRANT TARGET_LDFLAGS = -lnsl -lsocket USE_TPROXY = implicit USE_LIBCRYPT = implicit USE_CRYPT_H = implicit USE_GETADDRINFO = implicit else ifeq ($(TARGET),freebsd) # This is for FreeBSD USE_POLL = implicit USE_KQUEUE = implicit USE_TPROXY = implicit USE_LIBCRYPT = implicit else ifeq ($(TARGET),osx) # This is for Mac OS/X USE_POLL = implicit USE_KQUEUE = implicit USE_TPROXY = implicit else ifeq ($(TARGET),openbsd) # This is for OpenBSD >= 5.7 USE_POLL = implicit USE_KQUEUE = implicit USE_TPROXY = implicit USE_ACCEPT4 = implicit else ifeq ($(TARGET),netbsd) # This is for NetBSD USE_POLL = implicit USE_KQUEUE = implicit USE_TPROXY = implicit else ifeq ($(TARGET),aix51) # This is for AIX 5.1 USE_POLL = implicit USE_LIBCRYPT = implicit TARGET_CFLAGS = -Dss_family=__ss_family DEBUG_CFLAGS = else ifeq ($(TARGET),aix52) # This is for AIX 5.2 and later USE_POLL = implicit USE_LIBCRYPT = implicit TARGET_CFLAGS = -D_MSGQSUPPORT DEBUG_CFLAGS = else ifeq ($(TARGET),cygwin) USE_POLL = implicit USE_TPROXY = implicit TARGET_CFLAGS = $(if $(filter 1.5.%, $(shell uname -r)), -DUSE_IPV6 -DAF_INET6=23 -DINET6_ADDRSTRLEN=46, ) endif # cygwin endif # aix52 endif # aix51 endif # netbsd endif # openbsd endif # osx endif # freebsd endif # solaris endif # linux2628 endif # linux26 endif # linux24e endif # linux24 endif # linux22 endif # haiku endif # generic
从以上的代码中,可以看到,根据系统的内核不同,配置会不一样,编译会不一样。这里附上一个查看Linux版本的文章:[centos7]查看系统版本、CPU的方法
启动haproxy
# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
重启haproxy
# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg -st `cat /run/haproxy.pid`
停止haproxy
# kill all haproxy
制作 haproxy启动/关闭/重启SHELL脚本
复制 /haproxy-1.7.2/examples/haproxy.init 到/etc/init.d/haproxy,并相应的更改
vim /etc/init.d/haproxy #更改BIN和CFG的路径 #BIN=/usr/sbin/$BASENAME BIN=/usr/local/bin/haproxy #CFG=/etc/$BASENAME/$BASENAME.cfg CFG=/usr/local/haproxy/conf/haproxy.cfg
错误:haproxy: 第 26 行:[: =: 期待一元表达式
[ ${NETWORKING} = "no" ] && exit 0 [ "$NETWORKING" = "no" ] && exit 0
这样就可以使用:
service start haporxy service stop haproxy service restart haproxy service reload haproxy