haproxy 配置

1、启停

1.1、注册成服务的启停方式

Haproxy重启

systemctl restart haproxy

Haproxy 启动

systemctl  start  haproxy     

Haproxy  停止

systemctl restart haproxy

 

 

1.2、未注册成服务的启停方式

启动

haproxy -f /etc/haproxy/haproxy.cfg

停止

Ps –ef|grep haproxy 查出进程号

Kill -9 进程号

 

2、配置

HAProxy配置中分成五部分内容,当然这些组件不是必选的,可以根据需要选择部分作为配置。

global:

参数是进程级的,通常和操作系统(OS)相关。这些参数一般只设置一次,如果配置无误,就不需要再次配置进行修改

defaults:

配置默认参数的,这些参数可以被利用配置到frontend,backend,listen组件

frontend:

接收请求的前端虚拟节点,Frontend可以根据规则直接指定具体使用后端的 backend(可动态选择)。

backend:

后端服务集群的配置,是真实的服务器,一个Backend对应一个或者多个实体服务器。

listen:

Frontend和Backend的组合体。

-------------------------------------------------

BALANCE 选项:

balance source :

如果想让HAProxy按照客户端的IP地址进行负载均衡策略,即同一IP地址的所有请求都发送到同一服务器时,需要配置此选项。

balance roundrobin :

HAProxy把请求轮流的转发到每一个服务器上,依据每台服务器的权重,此权重会动态调整。最常见的默认配置。

一个配置示例

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
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/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    #将haproxy的进程号写入文件/var/run/haproxy.pid,启动haproxy进程的用户必须有对haproxy.pid文件的读写权限。
    pidfile     /var/run/haproxy.pid
    #最大连接数
    maxconn     4000
    #用户
    user        haproxy
    #组
    group       haproxy
    #使haproxy进程进入后台运行,这是推荐的运行模式。
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    #默认的模式,可选项tcp和http
    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 tunnel        3600s
    timeout queue           1m
    timeout connect         10s
    timeout client          3m
    timeout server          3m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main 
    bind *:80
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js
    acl oss_url          path_beg       -i /cloud-web/oss/v1
  #  use_backend static          if url_static
    use_backend oss_backend     if oss_url
    default_backend             cloudweb

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend oss_backend
    mode http
    option httpchk HEAD / HTTP/1.0
    reqrep ^([^\ ]*)\ /cloud-web/oss/v1/(.*)     \1\ /v1/\2
    server swift0 10.110.20.147:8081  check inter 2000 fall 3 weight 30
    server swift1 10.110.20.148:8081  check inter 2000 fall 3 weight 30
backend cloudweb
    mode http
    option httpchk HEAD / HTTP/1.0
    balance     roundrobin
    server  app0 10.110.20.147:8080 check  inter 2000 fall 3 weight 30
    server  app1 10.110.20.148:8080 check  inter 2000 fall 3 weight 30
#listen rabbitmq *:5672
#    mode tcp
#    option tcpka
#    balance     roundrobin
#    server mq1 10.110.19.240:5672 check inter 2000 fall 3
listen status
        mode http
        bind 0.0.0.0:50002
        stats enable
        stats hide-version
        stats uri     /state
        stats auth    admin:123456a?
        stats admin if TRUE

 

一篇不错的文章

http://www.cnblogs.com/dkblog/archive/2012/03/13/2393321.html

 

 

 

 

 

 

 

猜你喜欢

转载自huangqiqing123.iteye.com/blog/2330649