CenOS7下Haproxy1.7.8负载均衡搭建

28 篇文章 0 订阅
1 篇文章 0 订阅

1.首先放置jar包(源代码haproxy-1.7.8.tar.gz

目录 /usr/local/haproxy

2.解压目录        

tar -xvf haproxy-1.7.8.tar.gz

进入 haproxy-1.7.8

         编译

     make TARGET=linux2628 PREFIX=/usr/local/haproxy(需安装gcc不然会报错,目录需存在)

     Make install PREFIX=/usr/local/haproxy

     确认目录下存在编译后的文件目录

3.编译目录

将haproxy-start.sh放入sbin文件夹中

创建 conf文件夹,将配置文件放入其中

创建logs文件夹

覆盖 /etc/rsyslog.conf(rsyslog需安装)

4.运行haproxy-start.sh

查看是否已经在运行

 

完成后删除源码文件

操作系统一样的话

只需要编译,剩下可以复制编译后的程序目录到相应位置,不需要再一台台编译

附录:

1.haproxy-start.sh

/usr/local/haproxy/sbin/haproxy -sf $(cat /usr/local/haproxy/logs/haproxy.pid) -f /usr/local/haproxy/conf/http_proxy.cfg

2.http_proxy.cfg

#i
# demo config for Proxy mode
#
##################全局配置###################
global
        #maxconn         20000 默认设置为 DEFUALT_MAXCONN 根据内存弹性计算
    #ulimit-n    16384 自动计算,不推荐设置
        log 127.0.0.1:514 local0 info
        uid             0
        gid             0
        chroot          /usr/local/haproxy/haproxy/ #chroot运行路径 保证系统安全性
    nbproc        1 #进程数量,建议1
        daemon            #后台运行
        external-check #健康检查
        hard-stop-after 10s
        pidfile /usr/local/haproxy/logs/haproxy.pid
        stats socket /usr/local/haproxy/logs/haproxy.sock mode 600 level admin
        stats timeout 2m
        maxconn 500000
        max-spread-checks 500
###########################################
defaults
        log global
        mode http
        option httplog
        retries 5
        option forwardfor
        timeout connect 50s
        timeout client 50s
        timeout server 50s
        maxconn 500000

##################前端配置##################
frontend front-proxy
    bind        *:6868
        stats enable
        log             global
        option          httplog
        option          dontlognull
        option          nolinger
        option          http_proxy
        maxconn         500000
        timeout client  50s

    default_backend end-proxy
        
#################后端配置###################
backend end-proxy
        balance leastconn
    timeout connect 50s
    timeout server  50s
    retries         5
    option          nolinger
    option          http_proxy
    server web1 172.26.177.92:8079 rise 3 fall 5
    server web2 172.26.177.94:8079 rise 3 fall 5
    server web3 172.26.177.92:8078 rise 3 fall 5
    server web4 172.26.177.94:8078 rise 3 fall 5

    # layer7: Only GET method is valid
    #acl valid_method        method GET
    #http-request deny if !valid_method

    # layer7: protect bad reply
    http-response deny if { res.hdr(content-type) audio/mp3 }
####################listen####################

listen admin_stats
    stats enable
    bind *:8085
    mode http
    option httplog
    log global
    maxconn 500000
    stats refresh 15s
    stats uri /admin
    stats realm haproxy
    stats auth admin:admin
    stats hide-version
    stats admin if TRUE
 

3.rsyslog.conf

# rsyslog configuration file

# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html

#### MODULES ####

# The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark  # provides --MARK-- message capability

# Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514

# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514


#### GLOBAL DIRECTIVES ####

# Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog

# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

# File syncing capability is disabled by default. This feature is usually not required,
# not useful and an extreme performance hit
#$ActionFileEnableSync on

# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf

# Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
$OmitLocalLogging on

# File to store the position in the journal
$IMJournalStateFile imjournal.state


#### RULES ####

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
#*.info;mail.none;authpriv.none;cron.none                /var/log/sysmessages
*.debug;mail.none;authpriv.none;cron.none                /var/log/sysmessages

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

# Log all the mail messages in one place.
mail.*                                                  -/var/log/maillog


# Log cron stuff
cron.*                                                  /var/log/cron

# Everybody gets emergency messages
*.emerg                                                 :omusrmsg:*

# Save news errors of level crit and higher in a special file.
uucp,news.crit                                          /var/log/spooler

# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log

*.* /var/log/all.log

$ModLoad imudp
$UDPServerAddress *
$UDPServerRun 514
# ### begin forwarding rule ###
# The statement between the begin ... end define a SINGLE forwarding
# rule. They belong together, do NOT split them. If you create multiple
# forwarding rules, duplicate the whole block!
# Remote Logging (we use TCP for reliable delivery)
#
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g   # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList   # run asynchronously
#$ActionResumeRetryCount -1    # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @@remote-host:514
# ### end of the forwarding rule ###
 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值