haproxy简介
HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。
包括 GitHub、Bitbucket、Stack Overflow、Reddit、Tumblr、Twitter和 Tuenti在内的知名网站,及亚马逊网络服务系统都使用了HAProxy。
负载均衡
二层负载均衡(mac)
用于虚拟mac地址方式,外部对虚拟mac地址请求,负载均衡接收后分配给后端实际的mac地址响应。
三层负载均衡(ip)
一般用于虚拟ip地址的方式,外部对虚拟ip地址请求,负载均衡接收后分配给后端实际的ip地址响应。
四层负载均衡(tcp)
在三层负载均衡的基础上,用ip+port接收请求,在转发到对应的机器上。
产品大概有:F5,lvs,nginx,haproxy…
七层负载均衡(http)
根据虚拟的url或者ip,主机名接收请求,在转发到相应的处理服务器上。
产品大概有:haproxy,nginx,apache,mysql proxy…
haproxy安装
yum安装
yum -y install haproxy
源码安装
haproxy官方帮助文档:https://cbonte.github.io/haproxy-dconv/
下载安装包的地址
haproxy源码包下载网站地址:https://src.fedoraproject.org/repo/pkgs/haproxy/
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
uid=993(haproxy) gid=990(haproxy) groups=990(haproxy)
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost haproxy-2.1.3]
> TARGET=linux-glibc \
> USE_OPENSSL=1 \
> USE_ZLIB=1 \
> USE_PCRE=1 \
> USE_SYSTEMD=1
[root@localhost haproxy-2.1.3]
[root@localhost haproxy-2.1.3]
[root@localhost haproxy-2.1.3]
...
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
[root@localhost haproxy-2.1.3]
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
[root@localhost haproxy-2.1.3]
[root@localhost haproxy-2.1.3]
global
log 127.0.0.1 local0 info
maxconn 20480
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
listen admin_stats
bind 0.0.0.0:8189
stats enable
mode http
log global
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
stats refresh 30s
listen webcluster
bind 0.0.0.0:80
mode http
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
server web01 172.16.103.130:80 check inter 2000 fall 5
[root@localhost]
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
[root@localhost ~]
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]
*.info;mail.none;authpriv.none;cron.none /var/log/messages
local0.* /var/log/haproxy.log
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
配置文件解析
haproxy 的配置文件由两部分组成:
全局设定和对代理的设定,共分为五段:global,defaults,frontend,backend,listen
global: 全局配置,主要用于定义全局参数,属于进程级的配置,通常和操作系统配置有关。
default: 配置默认参数,这些参数可以被用到frontend,backend,Listen组件。
frontend:接收请求的前端虚拟节点,frontend可以指定具体使用后端的backend。
backend : 后端服务集群的配置,真实服务器,一个backend对应一个或者多个实体服务器。
listen: fronted和backend的组合体,比如haproxy实例状态监控部分配置。Haproxy1.3之前的唯一配置方式。
时间格式
一些包含了值的参数表示时间,如超时时长。这些值一般以毫秒为单位,但也可以使用其它的时间单位后缀
us: 微秒(microseconds),即1/1000000秒;
ms: 毫秒(milliseconds),即1/1000秒;
s: 秒(seconds);
m: 分钟(minutes);
h:小时(hours);
d: 天(days);
全局global配置
global
log 127.0.0.1 local0 info //定义haproxy日志输出设置
#log loghost local0 info //定义haproxy日志级别
maxconn 20480 //定义最大连接数
#chroot /usr/local/haproxy //chroot运行路径
pidfile /var/run/haproxy.pid //haproxy进程PID文件
#maxconn 4000
user haproxy //运行haproxy用户,可用uid代替
group haproxy //运行haproxy用户组,可用gid代替
daemon //以后台形式运行haproxy
Haproxy搭建http负载均衡
搭建环境
主机名称 | IP****地址 | 需要安装的应用 | 系统版本 |
---|
client | 192.168.141.141 | 无 | centos8.5 |
DR | 192.168.141.140 | haproxy | centos8.5 |
RS1 | 192.168.141.143 | httpd | centos8.5 |
RS2 | 192.168.141.144 | httpd | centos8.5 |
[root@localhost ~]
[root@localhost ~]
[root@DR ~]
[root@RS1 ~]
[root@RS1 ~]
[root@RS1 ~]
[root@RS1 ~]
[root@RS2 ~]
[root@RS2 ~]
[root@RS2 ~]
[root@RS2 ~]
[root@DR ~]
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
[root@DR ~]
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
[root@DR ~]
[root@DR ~]
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:80
default_backend servers
backend servers
server web01 192.168.141.143:80
server web02 192.168.141.144:80
如果没有使用systemd管理,则执行
[root@DR ~]
如果配置systemd管理haproxy服务
[root@DR ~]
[root@DR ~]
RS1
[root@DR ~]
RS2
[root@DR ~]
RS1
[root@DR ~]
RS2
[root@DR ~]
使用WEB网页访问测试
[root@DR ~]
[root@DR ~]
global
log 127.0.0.1 local0 info
maxconn 20480
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
listen admin_stats
bind 0.0.0.0:8189
stats enable
mode http
log global
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
stats refresh 30s
listen webcluster
bind 0.0.0.0:80
mode http
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
server web01 192.168.141.143:80 check inter 2000 fall 5
server web02 192.168.141.144:80 check inter 2000 fall 5
[root@DR ~]
[root@DR ~]
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@DR ~]
通过浏览器访问测试
网页访问成功,通过刷新可以访问RS1和RS2的测试网页
登录管理员网页
#用户名和密码admin:admin
就是在/etc/haproxy/haproxy.cfg中设置的可以更换
登录成功可以看见有两台虚拟机这里就是RS1和RS2
配置文件解析
可以浏览这个解决你的疑问
haproxy 的配置文件由两部分组成:全局设定和对代理的设定,共分为五段:global,defaults,frontend,backend,listen
global: 全局配置,主要用于定义全局参数,属于进程级的配置,通常和操作系统配置有关。
default: 配置默认参数,这些参数可以被用到frontend,backend,Listen组件。
frontend:接收请求的前端虚拟节点,frontend可以指定具体使用后端的backend。
backend : 后端服务集群的配置,真实服务器,一个backend对应一个或者多个实体服务器。
listen: fronted和backend的组合体,比如haproxy实例状态监控部分配置。Haproxy1.3之前的唯一配置方式。
一些包含了值的参数表示时间,如超时时长。这些值一般以毫秒为单位,但也可以使用其它的时间单位后缀
us: 微秒(microseconds),即1/1000000秒;
ms: 毫秒(milliseconds),即1/1000秒;
s: 秒(seconds);
m: 分钟(minutes);
h:小时(hours);
d: 天(days);
global
log 127.0.0.1 local0 info
maxconn 20480
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon
级别 代码 描述
energ 0 系统不可用
alert 1 必须马上采取行动的事件
crit 2 关键的事件
err 3 错误事件
warning 4 警告事件
notice 5 普通但重要的事件
info 6 有用的信息
debug 7 调试信息
用于设置配置默认参数,这些参数可以被用到frontend,backend,listen组件。
在此部分中设置的参数值,默认会自动引用到下面的frontend、backend、listen部分中。如果某些参数属于公用的配置,只需要在defaults部分添加一次即可。而如果frontend、backend、listen部分也配置了与defaults部分一样的参数,defaults部分参数对应的值自动被覆盖。
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
mode http:设置haproxy的运行模式,有三种{http|tcp|health}。注意:如果haproxy中还要使用4层的应用(mode tcp)的话,不建议在此定义haproxy的运行模式。
tcp模式:在此模式下,客户端和服务器端之前将建立一个全双工的连接,不会对七层报文做任何检查,默认为tcp模式,经常用于SSL、SSH、SMTP等应用。
http模式:在此模式下,客户端请求在转发至后端服务器之前将会被深度分板,所有不与RFC格式兼容的请求都会被拒绝。
health:已基本不用了。
frontend是在haproxy 1.3版本以后才引入的一个组件,同时引入的还有backend组件。通过引入这些组件,在很大程度上简化了haproxy配置文件的复杂性。forntend可以根据ACL规则直接指定要使用的后端backend。
如:
frontend http_80_in
bind 0.0.0.0:80
mode http
log global
option httpclose
option httplog
option forwardfor
default_backend wwwpool
用来定义后端服务集群的配置,真实服务器,一个Backend对应一个或者多个实体服务器
backend wwwpool
mode http
option redispath
option abortonclose
balancer source
cookie SERVERID
option httpdchk GET /test.html
server web1 10.1.1.2:80 cookie 2 weight 3 check inter 2000 rise 2 fall 3 maconn 8
常常用于状态页面监控,以及后端server检查,是Fronted和backend的组合体。
listen admin_stats
bind 0.0.0.0:8189
stats enable
mode http
log global
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
stats refresh 30s
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)