溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

HAproxy編譯安裝

發(fā)布時間:2020-07-19 17:00:36 來源:網(wǎng)絡 閱讀:1883 作者:ihanxiao2100 欄目:開發(fā)技術

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則根據(jù)當前操作系統(tǒng)內核版本指定

  • - 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 添加到系統(tǒng)環(huán)境變量 /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的組合體,監(jiān)控組的名稱,按需自定義名稱 

        bind 0.0.0.0:17818             #監(jiān)聽端口 

        mode http                      #http的7層模式 

        log 127.0.0.1 local2 err       #錯誤日志記錄 

        stats refresh 5s               #每隔5秒自動刷新監(jiān)控頁面 

        stats uri /admin?admin         #監(jiān)控頁面的url 

        stats realm HAProxy            #監(jiān)控頁面的提示信息 

        stats auth tdman:adminpwd         #監(jiān)控頁面的用戶和密碼admin,可以設置多個用戶名

#        stats hide-version             #隱藏統(tǒng)計頁面上的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問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI