haproxy

时间:2021-08-08
本文章向大家介绍haproxy,主要包括haproxy使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

  

日志

 

配置rsyslog

 

 

检测配置文件

 

检测编译选项

haproxy -vv

Configuration

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # log是发给server的, 而不是本地的facility
    log         127.0.0.1 local2 
    log         /dev/log  local1 notice
    
    chroot      /var/lib/haproxy  # <jail dir>
    pidfile     /var/run/haproxy.pid
    maxconn     40000
    user        haproxy
    group       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
    mode                    http
    log                     global
    option                  httplog  # mode 为http时, 记录详细日志
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8  # 在发往backend的请求首部插入 X-Forwarded-For 首部
    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                 30000



#---------------------------------------------------------------------
# statistics 
#---------------------------------------------------------------------
listen statistics
  bind *:9000
  stats enable
  #stats hide-version
  #stats scope .
  stats uri /admin
  stats realm Haproxy\ Statistics
  stats auth admin:admin
  stats admin if TRUE  # web管理



#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  frontend-main *:80
    #use_backend static          if url_static
    #default_backend             app
    default_backend              server
  #errorfile 503 /etc/haproxy/errorpages/503.html  # 自定义 503 code
  errorloc 504 http://baidu.com

#---------------------------------------------------------------------
# apiserver frontend which proxys to the control plane nodes
#---------------------------------------------------------------------
frontend frontend-apiserver
    mode tcp
    bind *:16443
    option tcplog
    default_backend apiserver



#---------------------------------------------------------------------
# round robin balancing for apiserver
#---------------------------------------------------------------------
backend backend-apiserver
    mode tcp
    option httpchk GET /healthz
    http-check expect status 200
    option ssl-hello-chk  # https
    option tcplog
    option tcp-check

    balance     roundrobin
    #server ${HOST1_ID} ${HOST1_ADDRESS}:${APISERVER_SRC_PORT} check
    server gale1 192.168.8.11:6443 check
    server gale2 192.168.8.12:6443 check
    server gale3 192.168.8.13:6443 check

#---------------------------------------------------------------------
# backend server
#---------------------------------------------------------------------

backend backend-server
    balance     roundrobin
    cookie ServerID insert
    server      gale2 192.168.8.12:80 check weight 10 cookie gale2
    server      gale3 192.168.8.13:80 check weight 20 cookie gale3



#---------------------------------------------------------------------
# frontend demo
#---------------------------------------------------------------------

# bind
frontend  main *:80

frontend main
  bind *:80  # bind 用于frontend, listen
  bind *:8080
  bind *:900-910

# balance
balance roundrobin, static-rr, leastconn, source, uri,

# server
server first 172.16.1.7:8080 cookie first check inter 1s port 80
server second 172.16.1.7:8080 cookie second check inter 1s port 80

原文地址:https://www.cnblogs.com/dissipate/p/15115076.html