学堂 学堂 学堂公众号手机端

haproxy配置(HAProxy配置中)

lewis 1年前 (2024-04-26) 阅读数 18 #技术

make TARGET=linux26 USE_PCRE=1 USE_OPENSSL=1 ADDLIB=-lz USE_ZLIB=1 USE_CRYPT_H=1 USE_LIBCRYPT=1 PREFIX=/data/haproxy
make install PREFIX=/data/haproxy

cd /data/haproxy/
mkdir log conf


cp -r /root/haproxy-*/examples/errorfiles ./

touch log/haproxy.log

mkdir /etc/haproxy

cd /etc/haproxy

ln -s /data/haproxy/conf/haproxy.cfg ./

ln -s /data/haporxy/sbin/haproxy /usr/sbin/

cp /root/haproxy-1.4.26/examples/haproxy.init /etc/init.d/haproxy

配置文件haproxy.conf 启动log

vim /etc/rsyslog.conf

1.启动日志记录功能
$ModLoad imudp
$UDPServerRun 514
$ModLoad imtcp
$InputTCPServerRun 514

2.
local2.* /data/haproxy/var/log/haproxy.log

vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS="-r -m 0"

vim /etc/rsyslog.conf

[root@test2 ~]# cat haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 65533
chroot /data/haproxy
uid 53
gid 54
daemon
pidfile /data/haproxy/haproxy.pid
#debug
#quiet

defaults
#log global
log 127.0.0.1 local3
mode http
option httplog
option http-pretend-keepalive
option http-server-close
option forceclose
option httpclose
option forwardfor except 127.0.0.0/8
option accept-invalid-http-request
option dontlognull
option redispatch
option abortonclose
option originalto
maxconn 65535
balance source
retries 3
redispatch
contimeout 5000
clitimeout 50000
srvtimeout 50000
timeout check 5s
timeout http-request 5s
timeout queue 30s
timeout http-keep-alive 5s
stats refresh 30s
stats uri /haproxy-status
stats realm haproxy-status
stats auth admin:123456
stats hide-version
frontend web
bind :80

acl mm.cinyi.com hdr(host) -i mm.cinyi.com
use_backend mm.cinyi.com if mm.cinyi.com

acl h5.cinyi.com hdr(host) -i h5.cinyi.com
use_backend h5.cinyi.com if h5.cinyi.com

backend mm.cinyi.com
mode http
balance source
server 192.168.39.99 192.168.39.99:80 check inter 2000 rise 3 fall 3 weight 100
server 192.168.39.109 192.168.39.109:80 check inter 2000 rise 3 fall 3 weight 100

backend h5.cinyi.com
mode http
balance source
server 192.168.39.97 192.168.39.97:80 check inter 2000 rise 3 fall 3 weight 100
server 192.168.39.98 192.168.39.98:80 check inter 2000 rise 3 fall 3 weight 100

errorfile 403 /data/haproxy/errorfiles/403.http

errorfile 500 /data/haproxy/errorfiles/500.http

errorfile 502 /data/haproxy/errorfiles/502.http

errorfile 503 /data/haproxy/errorfiles/503.http

errorfile 504 /data/haproxy/errorfiles/504.http

option forwardfor header X-REAL-IP

tune.ssl.default-dh-param 2048
option forwardfor
option http-server-close

acl url_static path_beg /static /images /img /css

acl url_static paht_end .gif ./pngg .jpg .css .js

acl host_www hdr_beg(host) -i www

acl host_static hdr_beg(host) -i www img. video. download. ftp.

use_backend static if host_static or host_www url_static

use_backend www if host_www

acl url_static url_reg /*.(css|js|jpg|jpeg|gif)
use_backuped backend_www_example_com if url_static

​​/news/upload/ueditor/image/202208/3foiy2hcivg.html global settings: 对haproxy进行自身属性的设定:
porxies: 对代理的设定
defaults
frontend
backend
listen : frontend+backend

定义一个完成的代理方式:
frontend,backend
listen

混合使用2种方式

keyword: bind

第一种用法:用在frontend中
frontend wevsrv
bind *:80
default_backend webserver

backend webserver
balance roundrobin
server srv1 192.168.11.7:80 check
server srv2 192.168.11.7:81 check

第二种用法:用在listen中
listen http_proxy
bind :80,:443
bind 10.0.0.1:10080,10.0.0.2:10081

balance 调度算法
只能用在 defaults listen backend中

balance url_param <param> [check_post[<max_wait>]]
算法有
roundrobin 轮训,新加的server,会自动添加
static-rr 轮训,只能重启进程
leastconn 最小连接,具有较长回话时间的,mysql,ladap
source 源地址hash
uri 基于用户调度,只要访问访问url相同,就会指向一台服务器上
url_param
hdr(<name>)
rdp-cookie
dep-cookie(name)

回话保持机制:
IP层:source
位于同一个NAT服务器背后的多个请求都会定向至同一个updatream server: 不利于均衡'
应用测:cookie
有更好的负载均衡效果;

source: 一般只有不支持使用cookie插入又需要保持回话时使用

uri:用于后端upstream server是缓存的服务器,保证缓存命中率的


总结:

1. 调度众多的mysql服务器,使用 leastconn调度方法
2. 调度web图片服务器组,使用roundrobin调度方法
3. 调度web应用程序服务器组,使用source调度方法
source

source保持的方式:
session绑定
源IP绑定:
nginx:ip_hash
haproxy:source
ipvs:sh
cookie绑定:
session复制
session服务器

4. 调度web缓存服务器组,使用uri (hash-type)调度方法
uri:
hash-type
map-based
consistent





指定haproxy工作模式 mode { tcp | http | health}
http 代理http,对应用层数据做深入分析,因此支持7层的过滤、处理、转换等机制

tcp 代理mysql, SSL,SSH,SMTP ,haproxy 在客户端和upstream server之间
建立一个全双工的连接,不会对应用层协议做任何检查, 默认模式

指定日志:
log global 全局配置中定义的日志服务器
log <address> <facility> [<level> [minlevel]]

capture request header <HEADER> len <LENGTH>

在listen 或者frontend中指定使用的默认后端:
default_backend <backend name>

在listen或者frontend中指定使用的条件式后端
use_backend <backend> if <condition>
use_backend <backend> unless <condition>

server 为backend或者listen
server <name> <address>[:[port]] [param*]

server first 10.1.1.1:1080 cookie first check inter 1000
server second 10.1.1.2:1080 cookie second check inter 1000
server transp ipv4@
server backup "${SRV_BACKUP}:1080" backup
server www1_dc1 "${LAN_DC1}.101:80"
server www1_dc2 "${LAN_DC2}.101:80"


check: 启动对此server执行健康状态检查,其可以借助于额外的其他参数完成更精细的设定
inter <delay>: 设定健康状态检查的时间间隔,单位为毫秒,默认为2000;
rise <count> : 设定健康状态检查中,某离线的server从离线状态转换至正常状态需要成功检查的次数
fall <count> 确认server从正常状态转换为不可用状态需要检查的次数。

cookie <value>
为指定server设定cookie的值,此处指定的值将在请求入站时被检查,

observe 通过观察服务器的通信状态来判定其健康状态

redir <prefix> 启动重定向功能,将发往此服务器的get和HEAD请求均以302状态码响应,需要注意的是,需要注意的是
prefix后面不能使用/,且不能使用相对地址,以免造成循环

server srv1 172.17.10..6:80 redir http://imagesserver.magedu.com check

weight <weight> :权重,默认为1,最大值256,0表示不参与负载均衡

option httpchk 检查方法:
option httpchk
option httpchk <uri>
例如 option httpchk /test1.html

option httpchek <method><uri>
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www


option httpchek <method><uri><version> 不用用于frontend段

stats enable 启动内置状态监控

server srv1 192.168.0.1:80
stats enable

#########################################################
frontend wevsrv
bind *:80
capture request header X-Forwarded-For len 15 捕获请求header
default_backend webserver

backend webserver
cookie SRV insert indirect nocache
#balance uri ###调度算法uri,适用于缓存服务器
#balance source ###适用于web服务器
balance roundrobin ####适用于图片服务器
option httpchk OPTIONS /index.php HTTP/1.0 #状态检查

stats enable ##启动状态统计
option httpchk HEAD /index.php HTTP/1.0
server srv1 192.168.11.251:80 check cookie srv1
server srv2 192.168.11.252:80 check cookie srv2

stats hide-version
stats scope .
stats uri /admin?stats
stats realm Haproxy\ Statistics
stats auth admin1:AdMiN123
stats admin if TRUE 页面管理功能

haproxy故障二:
127.0.0.1 haproxy[31795]: proxy www has no server available!

我出现的这个故障,是由配置文件健康检查时,指定检查的页面不存在造成的,只要修改一个存在的页面就OK了,或者自己建一个专用于健康检查的页面。

option httpchk HEAD /index.html HTTP/1.0

红色字体index.html是我用来健康检查的页面,当然这只是测试,生产环境中,一般新建个专用的页面用于健康检查。



版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门