Haproxy

下载地址:https://www.haproxy.org/#down

tar -xzf haproxy-1.8.14.tar.gz

cd cd haproxy-1.8.14

查看README:相关的编译参数

make TARGET=linux2628 PREFIX=/main/server/haproxy

mkdir  /main/server/haproxy

make install PREFIX=/main/server/haproxy

创建配置文件:

mkdir /main/server/haproxy/conf

vim /main/srever/haproxy/conf/haproxy.cfg

注:源码安装是没有示例配置文件的,需要自己创建,如果是rpm安装的,会有默认的示例配置文件。

示例配置文件粘贴:

方式一:

global

        daemon

        maxconn 256

    defaults

        mode http

        timeout connect 5000ms

        timeout client 50000ms

        timeout server 50000ms

    

        frontend  main 

         bind  *: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

        #use_backend <服务器组> if <ACL名字>

        #acl <ACL名字> <类型> <大小写> <规则>

        default_backend             app

    backend servers

        server server1 127.0.0.1:8000 maxconn 32

方式二:

    # The same configuration defined with a single listen block. Shorter but

    # less expressive, especially in HTTP mode.

    用单个监听块定义相同的配置,但是较短代表较少的表达,特别是在HTTP模式下。

    global

        daemon

        maxconn 256

    defaults

        mode http

        timeout connect 5000ms

        timeout client 50000ms

        timeout server 50000ms

    listen http-in

        bind *:80

        server server1 127.0.0.1:8000 maxconn 32

注意:haproxy不会修改Client的http请求头信息。当Client拿域名www.thd99.com 访问haproxy时,会访问后端的www.thd99.com虚拟主机;当Client拿Ip 192.168.0.185访问haproxy时,会访问后端的默认虚拟主机。

配置文件:

[root@client conf]# cat haproxy.cfg

#全局

global

    log         127.0.0.1 local2

    chroot      /main/server/haproxy  #chroot改变当前工作目录,增加安全性

    pidfile     /main/server/haproxy.pid

    maxconn     4000

    user        haproxy

    group       haproxy

    nbproc      1    #number of process 进程的数量

    daemon           #以后台方式运行haproxy

    #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

#定义haproxy状态访问

listen stats

    bind 0.0.0.0:8080

    option httplog

    stats enable 

    stats uri /stats            #访问路径

    stats realm "Haproxy Auth"  #提示信息

    stats auth admin:admin      #认证的用户名和密码

    stats admin if TRUE         #通过认证才可管理

    stats refresh 3s            #页面刷新间隔

    stats show-desc demo

    stats hide-version          #隐藏版本号

#定义客户端与haproxy的访问

frontend  main

    bind *:80

    default_backend             www.thd99.com

#定义后端

backend www.thd99.com

    balance     roundrobin

    server      server1 192.168.0.186:80 check inter 1s rise 2 fall 3 cookie A maxconn 1000

    server      server2 192.168.0.146:80 check inter 1s rise 2 fall 3 cookie A maxconn 1000

配置日志输出:

1.

mkdir /var/log/haproxy

chmod a+w /var/log/haproxy

2. 开启rsyslog记录haproxy日志功能

编辑“/etc/rsyslog.conf”打开如下配置项:

# Provides UDP syslog reception

$ModLoad imudp

$UDPServerRun 514

添加如下内容:

# Save haproxy log

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

3. 修改“/etc/sysconfig/rsyslog”文件,内容如下

# Options for rsyslogd

# Syslogd options are deprecated since rsyslog v3.

# If you want to use them, switch to compatibility mode 2 by "-c 2"

# See rsyslogd(8) for more details

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

相关参数说明:

-r:    打开接受外来日志消息的功能,其监控514 UDP端口;

-x:   关闭自动解析对方日志服务器的FQDN信息,这能避免DNS不完整所带来的麻烦;

-m:  修改syslog的内部mark消息写入间隔时间(0为关闭),例如240为每隔240分钟写入一次"--MARK--"信息;

-h:   默认情况下,syslog不会发送从远端接受过来的消息到其他主机,而使用该选项,则把该开关打开,所有接受到的信息都可根据syslog.conf中定义的@主机转发过去.

4. systemctl restart rsyslog

运行服务:/main/server/haproxy/sbin/haproxy -f /main/server/haproxy/conf/haproxy.cfg

访问haproxy状态页面:192.168.0.185:8080/stats

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章