HAProxy 1.8.13 编译安装

HAProxy编译安装

首先上官网获取haproxy的tar包

解压至当前目录

    haproxy的配置文件中要使用到haproxy用户

    所以我们先创建该用户

    useradd -r  -s /sbin/nologin haproxy
    tar xf haproxy-1.8.13.tar.gz
    cd haproxy-1.8.13/

    vim README 文档
    其中我们可以查看到关于怎么样安装 

我们使用其中的一项,包括了pcre 和 ssl 以及zlib等功能 

$ make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy

make install的时候要带上PREFIX= ... 不然它会直接在/usr/local/目录下安装相关的文件

cp /haproxy-1.8.13/examples/haproxy.init /etc/rc.d/haproxy
vim /etc/rc.d/haproxy

修改其中的一行,这是其中的一个坑


[ ${NETWORKING} = "no" ] && exit 0

[ "${NETWORKING}" = "no" ] && exit 0  给${NETWORKING}加上引号

接下来我们要自行创建haproxy的配置文件

mkdir /etc/haproxy
vim /etc/haproxy/haproxy.cfg

 我在1.5版的haproxy的rpm包版本里取得了配置文件

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
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                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:5000
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  app1 127.0.0.1:5001 check
    server  app2 127.0.0.1:5002 check
    server  app3 127.0.0.1:5003 check
    server  app4 127.0.0.1:5004 check

这是未改动过的版本

扫描二维码关注公众号,回复: 2888260 查看本文章

 其中还是有一个坑

在frontend段,好像1.8.13版本只能支持bind绑定端口号所以应该去掉main后面的*:5000,在其下一行加上bind  *:80

这个时候还不能开启haproxy服务,如果开启,会报一个

cannot bind UNIX socket [/var/lib/haproxy/stats

 的错误。(超级坑!!!)

这个时候需要自行创建/var/lib/haproxy目录,并将权限给haproxy用户

mkdir /var/lib/haproxy
chown haproxy:haproxy /var/lib/haproxy

这个时候就完成了haproxy的简单的编译安装!!

可以再frontend调用backend实现负载均衡了

猜你喜欢

转载自blog.csdn.net/zhangyexinaisurui/article/details/81489007