Haproxy介绍及安装

Haproxy 是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。

参考网址

global
    chroot /var/lib/haproxy
    user haproxy
    group haproxy
    daemon
    maxconn 4000
    ulimit-n 16384
    log 127.0.0.1 local0 err
    stats timeout 30s

defaults
    log  global
    mode  http
    option  httplog
    timeout connect 5000
    timeout client  50000
    timeout server  50000
	timeout http-request 15s
    timeout http-keep-alive 15s

frontend master1
    mode    tcp
    bind    *:6444
    option    tcplog
	tcp-request inspect-delay 5s
    default_backend    master1

backend master1
    mode    tcp
    option    tcplog
    option    tcp-check
    balance    roundrobin
	default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100
    server  master1 192.168.2.201:6443  check
    server  master2 192.168.2.202:6443  check
	server  master3 192.168.2.203:6443  check

# 追加-后台管理的配置
#访问地址:http://192.168.2.200:7777/admin
frontend admin_stats
  #后台管理端口7777
  bind :7777
     mode http
     stats enable
     option httplog
     maxconn 10
     #设定自动刷新时间间隔
     stats refresh 30s
     #地址ip:端口/admin
     stats uri /admin
     #用户名:admin 密码:123456
     stats auth admin:123456
     #隐藏版本
     stats hide-version
     stats admin if TRUE
inter 10s:定义了两个连续健康检查之间的间隔时间为10秒。
downinter 5s:在服务器被标记为宕机(down)之后,定义了两次连续健康检查之间的间隔时间为5秒。
rise 2:定义了服务器从宕机状态恢复正常所需的连续成功健康检查次数。只有当服务器经过2次连续成功的健康检查后,该服务器才会被标记为上线(up)。
fall 2:定义了服务器在被标记为宕机之前所需的连续失败健康检查次数。如果服务器在2次连续失败的健康检查后仍未恢复,该服务器将被标记为宕机。
slowstart 60s:在服务器被标记为上线后,定义了在新连接到达之前延迟启动的时间。这允许服务器有足够的时间来初始化。
maxconn 900:限制了与该服务器建立的最大并发连接数为900。
maxqueue 256:定义了在服务器上排队等待服务的最大请求数为256。
weight 100:指定了服务器的权重,用于负载均衡算法。如果所有服务器的权重相等,则流量将平均分配给它们。
 1 $ vi /usr/local/haproxy/haproxy.cfg     #新建配置文件,添加以下内容
 2 global
 3 log 127.0.0.1 local1
 4 maxconn 65000                            #最大连接数
 5 # chroot /usr/local/haproxy              #安装目录
 6 # uid haproxy                            #用户haproxy
 7 # gid haproxy                            #组haproxy
 8 daemon                                   #守护进程运行
 9 nbproc 1                                 #进程数量
10 # pidfile /usr/local/haproxy/logs/haproxy.pid #haproxy pid
11 
12 defaults
13 log global
14 mode http   #7层#默认的模式mode {tcp|http|health},tcp是4层,http是7层,health只会返回OK
15 option httplog                 #http 日志格式
16 option httpclose               #主动关闭http通道,HA-Proxy不支持keep-alive模式
17 option redispatch              #serverId对应的服务器挂掉后,强制定向到其他健康的服务器
18 option forwardfor except 127.0.0.1/8
19 #后端服务器需要获得客户端的真实IP,将从Http Header中获得客户端IP
20 option dontlognull #来防止记录 Alteo(4层负载均衡)发出的健康检测,如果一个 session 交互没有数据,这个 session就不会被记录
21 retries 3
22 maxconn 50000        #最大连接数
23 timeout http-request 10s
24 timeout queue 1m
25 timeout connect 10s
26 timeout client 1m
27 timeout server 1m
28 timeout http-keep-alive 10s
29 timeout check 10s
30 
31 #errorfile 502 /usr/local/haproxy/html/maintain.html
32 #errorfile 503 /usr/local/haproxy/html/maintain.html
33 #errorfile 504 /usr/local/haproxy/html/maintain.html
34 
35 frontend main
36 bind *:80        #监听地址
37 acl url_static path_beg -i /static /images /javascript /stylesheets
38 acl url_static path_end -i .jpg .gif .png .css .js
39 use_backend static if url_static
40 default_backend my_webserver
41 #定义一个名为my_app前端部分。此处将对于的请求转发给后端
42 backend static
43 #使用了静态动态分离(如果url_path匹配 .jpg .gif .png .css .js静态文件则访问此后端)
44 balance roundrobin      #负载均衡算法(#banlance roundrobin 轮询)
45 server static 127.0.0.1:80 check
46 #静态文件部署在本机(也可以部署在其他机器或者squid缓存服务器
47 acl host_www hdr_reg(host) -i ^(www.haproxy.test|haproxy.test)   #测试用的域名
48 
49 backend my_webserver
50 mode http
51 option forwardfor
52 balance roundrobin
53 cookie SERVERID
54 option httpchk HEAD /index.html
55 server web01 10.8.8.51:80 cookie web01 check inter 2000 rise 3 fall 3 weight 3
56 server web02 10.8.8.52:80 cookie web02 check inter 2000 rise 3 fall 3 weight 3
57 stats enable
58 #启用统计页;基于默认的参数启用页面信息,通过访问此页面能显示华丽的web状态界面 
59 stats uri /stats     #默认uri页面信息,不安全,可以自己定义页面的uri
posted @ 2023-06-28 08:43  kht  阅读(165)  评论(0编辑  收藏  举报