centos6系统HAproxy编译安装

一. Linux编译安装haproxy

部署前说明:

(1)系统版本: centos 6.6(64位)

(2)相关中间件信息

haproxy版本信息: haproxy-1.5.15

编译安装haproxy

1.1 到haproxy官网下载haproxy源码包如下

cd ~
wget http://www.haproxy.org/download/1.5/src/haproxy-1.5.15.tar.gz
1.2 创建haproxy运行用户

groupadd -r haproxy
useradd -g haproxy -M -s /sbin/nologin haproxy
1.3 编译安装haproxy:

cd ~
tar zxvf haproxy-1.5.15.tar.gz -C /usr/local/src
cd /usr/local/src/haproxy-1.5.15
make TARGET=linux26  ARCH=X86_64 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy

注意:TARGET=Linux26 是通过uname -a 来查看Linux内核版本的

1.4 创建haproxy主配置文件:

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

代码内容如下

global
    log         127.0.0.1 local2

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

    stats socket /var/lib/haproxy/stats

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

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

backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

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

1.4 创建haproxy系统服务启动脚本:

vim /etc/init.d/haproxy

代码内容如下

#!/bin/sh
#
# haproxy
#
# chkconfig:   - 85 15
# description:  HAProxy is a free, very fast and reliable solution \
#               offering high availability, load balancing, and \
#               proxying for TCP and  HTTP-based applications
# processname: haproxy
# config:      /etc/haproxy/haproxy.cfg
# pidfile:     /var/run/haproxy.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

exec="/usr/local/haproxy/sbin/haproxy"
prog=$(basename $exec)

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

cfgfile=/etc/haproxy/haproxy.cfg
pidfile=/var/run/haproxy.pid
lockfile=/var/lock/subsys/haproxy

check() {
    $exec -c -V -f $cfgfile $OPTIONS
}

start() {
    $exec -c -q -f $cfgfile $OPTIONS
    if [ $? -ne 0 ]; then
        echo "Errors in configuration file, check with $prog check."
        return 1
    fi
 
    echo -n $"Starting $prog: "
    # start it up here, usually something like "daemon $exec"
    daemon $exec -D -f $cfgfile -p $pidfile $OPTIONS
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    # stop it here, often "killproc $prog"
    killproc $prog 
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    $exec -c -q -f $cfgfile $OPTIONS
    if [ $? -ne 0 ]; then
        echo "Errors in configuration file, check with $prog check."
        return 1
    fi
    stop
    start
}

reload() {
    $exec -c -q -f $cfgfile $OPTIONS
    if [ $? -ne 0 ]; then
        echo "Errors in configuration file, check with $prog check."
        return 1
    fi
    echo -n $"Reloading $prog: "
    $exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile)
    retval=$?
    echo
    return $retval
}

force_reload() {
    restart
}

fdr_status() {
    status $prog
}

case "$1" in
    start|stop|restart|reload)
        $1
        ;;
    force-reload)
        force_reload
        ;;
    check)
        check
        ;;
    status)
        fdr_status
        ;;
    condrestart|try-restart)
      [ ! -f $lockfile ] || restart
    ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
        exit 2
esac

给该脚本授予可以执行的权限并启动haproxy服务:

chmod 777 /etc/init.d/hapoxy
service haproxy start

如果想开机自启动,也可以执行以下命令加入到到开机自启动:

chkconfig --add haproxy 
chkconfig --level 2345  haproxy on

1.5. haproxy服务启动状态查看

当haproxy服务启动后,我们可以通过netstat -ntlp | grep hapoxy 就可以查看到启动状态

    [root@svn haproxy-1.5.15]# netstat -ntlp | grep haproxy
    tcp        0      0 0.0.0.0:5000     0.0.0.0:*   LISTEN     23086/haproxy       

[root@svn haproxy-1.5.15]# 

看到以上信息说明haproxy服务已经成功了!

总结:linxu编译安装haproxy有时需要手动编写启动脚本,在centos环境下可以利用自带的RPM来安装haproxy(默认1.5.4版本),而且有自带的配置文件和服务脚本。
原文链接:https://blog.csdn.net/secsky/article/details/60576455

猜你喜欢

转载自blog.csdn.net/linxi7/article/details/87860283