温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

HAproxy编译安装

发布时间:2020-07-19 17:00:36 来源:网络 阅读:1846 作者:ihanxiao2100 栏目:开发技术

https://github.com/haproxy/haproxy


已成功安装HAproxy 1.7.9


yum -y install openssl-devel zlib-devel

tar -zxvf haproxy-1.7.9.tar.gz

cd haproxy-1.7.9

make TARGET=linux2628 PREFIX=/usr/local/haprpxy


Linux >= 2.6.28 with SSL and ZLIB support 

make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 PREFIX=/usr/local/haprpxy;

make install PREFIX=/usr/local/haproxy


TARGET则根据当前操作系统内核版本指定

  • - 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)



可以把 /usr/local/haproxy/sbin 添加到系统环境变量 /etc/profile



启动脚本:

cp path/examples/haproxy.init /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

#######


chmod +x /etc/init.d/haproxy


mkdir -p /var/lib/haproxy; mkdir /usr/local/haproxy/etc

ln -s /usr/local/haproxy/etc /etc/haproxy


useradd haproxy -M -s /sbin/nologin  


cp /usr/local/src/haproxy-1.7.9/haproxy-systemd-wrapper /usr/local/haproxy/sbin


Centos 7 haproxy启动脚本

/usr/lib/systemd/system/haproxy.service

########

[Unit]

Description=HAProxy Load Balancer

After=network.target


[Service]

ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q

ExecStart=/usr/local/haproxy/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid

ExecReload=/bin/kill -USR2 $MAINPID

KillMode=mixed

Restart=always


[Install]

WantedBy=multi-user.target

########

systemctl enable haproxy

systemctl start haproxy


配置文件 /etc/haproxy/haproxy.cfg:

mkdir -p /var/lib/haproxy

#######

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/rsyslog

    #

    # 2) configure local2 events to go to the /var/log/haproxy.log

    #   file. A line like the following can be added to

    #   /etc/sysconfig/rsyslog

    #

    #    local2.*                       /var/log/haproxy.log

    #

    log         127.0.0.1 local2


    chroot      /var/lib/haproxy

    pidfile     /var/run/haproxy.pid

    maxconn     50000

    user        haproxy

    group       haproxy

    daemon


    # turn on stats unix socket

    stats socket /var/lib/haproxy/stats


defaults

    mode                    http

    log                     global

    option                  httplog

    option                  tcplog

    option                  dontlognull

    option http-server-close

    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                 50000


listen UserAPI

    bind 0.0.0.0:6004

    mode http

    balance roundrobin

    option forwardfor       except 127.0.0.0/8

    #balance source

    server   serv1.184 192.168.1.184:6004 check inter 2000 rise 2 fall 3 maxconn 20480

    server   serv1.185 192.168.1.185:6004 check inter 2000 rise 2 fall 3 maxconn 20480



listen admin_status                    #Frontend和Backend的组合体,监控组的名称,按需自定义名称 

        bind 0.0.0.0:17818             #监听端口 

        mode http                      #http的7层模式 

        log 127.0.0.1 local2 err       #错误日志记录 

        stats refresh 5s               #每隔5秒自动刷新监控页面 

        stats uri /admin?admin         #监控页面的url 

        stats realm HAProxy            #监控页面的提示信息 

        stats auth tdman:adminpwd         #监控页面的用户和密码admin,可以设置多个用户名

#        stats hide-version             #隐藏统计页面上的HAproxy版本信息  

        stats admin if TRUE            #手工启用/禁用,后端服务器(haproxy-1.4.9以后版本) 


#######


开启haproxy日志

touch /var/log/haproxy.log; chown haproxy:haproxy /var/log/haproxy.log

修改文件 /etc/sysconfig/rsyslog

SYSLOGD_OPTIONS="-r -m 0 -c 2"



编辑 /etc/rsyslog.conf 开启下面两行

$ModLoad imudp  

  1. $UDPServerRun 514  

在下面内容

  1. # Save boot messages also to boot.log  

  2. local7.*                                                /var/log/boot.log  

之后添加:

# Save haproxy log

local2.*                       /var/log/haproxy.log


改完重启 rsyslog服务

如果开了防火墙,请开启相应的服务端口

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI