Haproxy相关配置
Posted Linux日记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Haproxy相关配置相关的知识,希望对你有一定的参考价值。
配置1实现后端服务轮询负载
frontend webserver #定义一个前端为webserver
bind :80 #绑定端口
default_backend web #指定当前定义的frontend调用默认的后端server
backend web 定义一个后端
server web1 192.168.214.187:80 #使用server指令定义一个后端主机
server web2 192.168.214.148:80
测试效果:
[root@client ~]# for i in {1..10};do curl 192.168.214.134;done
web server 2
web server 1
web server 2
web server 1
web server 2
web server 1
web server 2
web server 1
web server 2
web server 1
上面的一段配置是最简单的配置,但并不适用。下面慢慢拓展。
配置2:在配置一的基础上为web1主机添加基于权重的轮询,并且添加监测check,并且连接3次检测结果为成功才标记主机为可用
backend web
balance roundrobin #定义调度算法为 roundrobin
server web1 192.168.214.187:80 check rise 3 weight 3 #权重为3
server web2 192.168.214.148:80 check rise 3 #对后端服务进行检测,检测3次才算服务可用
测试效果:
[root@client ~]# for i in {1..10};do curl 192.168.214.134;done
web server 1
web server 1
web server 1
web server 2
web server 1
web server 1
web server 1
web server 2
web server 1
web server 1
配置3:标记web2主机为backup备用机器,只有当web1 down掉web2才起来工作
backend web
balance roundrobin
server web1 192.168.214.187:80 check rise 3
server web2 192.168.214.148:80 check rise 3 backup
测试效果:
[root@client ~]# for i in {1..5};do curl 192.168.214.134;done
web server 1
web server 1
web server 1
web server 1
web server 1
测试停掉web1主机,web2接替工作
[root@client ~]# for i in {1..5};do curl 192.168.214.134;done
web server 2
web server 2
web server 2
web server 2
web server 2
配置4:修改后端检测方式:基于url的检测
backend web
balance roundrobin
option httpchk GET /check.html #使用GET方法检测后端服务check。html页面
server web1 192.168.214.187:80
server web2 192.168.214.148:80
httpchk,smtpchk,mysql-check,等为应用层检测 7 层
定义基于http协议的7层健康状态检测机制:
使用option指令定义
option httpchk :不加参数首页检测
option httpchk < uri > :指定URL检测
option httpchk < method > :指定请求方法与路径检测
option httpchk < method > < uri > < version
:指定请求方法与路径检测和http协议版本
配置5 动静态分离:.jpg,.png,.gif等类型图片转发到后端web2主机上,将php内容转发至后端web1主机上
这里开始使用到了ACL匹配规则
前端定义
#定义前端静态页面匹配
frontend img *:80
acl url_imgs path_end -i .jpg .jpeg .png .gif
use_backend static if url_imgs
default_backend webserver
#定义前端动态页面匹配
frontend php :80
acl php_page path_end -i .php
use_backend phpserver if php_page
default_backend webserver
后端定义
backend static #静态后端
balance roundrobin
option httpchk GET /check.html
server imgserver1 192.168.214.148:80
backend phpserver #动态后端
server php1 192.168.214.187:80
backend webserver #默认转发
server web1 192.168.214.187:80
server web1 192.168.214.148:80
配置6,使用一致性哈希算法
backend webserver
balance source hash-type consistent #定义一致性哈希
server web1 192.168.214.187:80
server web1 192.168.214.148:80
配置7,启用haproxy状态页
listen status *:8025 #listen模式包含前端和后端,像是隧道模式
stats enable #启用状态页
stats uri /haproxy?yufu #定义状态页URL
stats realm "Haproxy status page"
stats auth admin:admin #设置状态页查看用户与密码
stats admin if TRUE #启用状态页中的管理接口
登录时要指定端口:http://192.168.214.134:8025/haproxy?yufu
完整的配置示列
动静分离配置
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
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 30000
listen stats
mode http
bind 0.0.0.0:1080
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
frontend http-in
bind *:80
mode http
log global
option httpclose
option logasap
option dontlognull
capture request header Host len 20
capture request header Referer len 60
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .jpeg .gif .png .css .js
use_backend static_servers if url_static
default_backend dynamic_servers
backend static_servers
balance roundrobin
server imgsrv1 172.16.200.7:80 check maxconn 6000
server imgsrv2 172.16.200.8:80 check maxconn 6000
backend dynamic_servers
cookie srv insert nocache
balance roundrobin
server websrv1 172.16.200.7:80 check maxconn 1000 cookie websrv1
server websrv2 172.16.200.8:80 check maxconn 1000 cookie websrv2
server websrv3 172.16.200.9:80 check maxconn 1000 cookie websrv3
mysql负载均衡配置
负载均衡MySQL服务的配置示例
#---------------------------------------------------------------------
# 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
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode tcp
log global
option httplog
option dontlognull
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 600
listen stats
mode http
bind 0.0.0.0:1080
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
frontend mysql
bind *:3306
mode tcp
log global
default_backend mysqlservers
backend mysqlservers
balance leastconn
server dbsrv1 192.168.10.11:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300
server dbsrv2 192.168.10.12:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300
以上是关于Haproxy相关配置的主要内容,如果未能解决你的问题,请参考以下文章