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 http://node107.yinzhengjie.org.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 http://node107.yinzhengjie.org.cn/monitor/index.html        #"node107.yinzhengjie.org.cn"节点可以访问到监控页面
Apache httpd is ok
[root@node102.yinzhengjie.org.cn ~]# [root@node102.yinzhengjie.org.cn ~]# curl -I http://node106.yinzhengjie.org.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 http://node108.yinzhengjie.org.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的日志,如下图所示。**

目录
相关文章
|
14天前
HAProxy的高级配置选项-配置haproxy支持https协议及服务器动态上下线
文章介绍了如何配置HAProxy以支持HTTPS协议和实现服务器的动态上下线。
46 8
HAProxy的高级配置选项-配置haproxy支持https协议及服务器动态上下线
|
14天前
|
网络协议
keepalived对后端服务器的监测方式实战案例
关于使用keepalived进行后端服务器TCP监测的实战案例,包括配置文件的编辑和keepalived服务的重启,以确保配置生效。
28 1
keepalived对后端服务器的监测方式实战案例
|
8天前
|
监控 应用服务中间件
Nagios 服务器 Nrpe 配置
Nagios服务器需安装NRPE并定义监控命令于`command.cfg`中。示例配置如下:`check_nrpe -H $HOSTADDRESS$ -c $ARG1$`。客户端配置文件如`192.168.149.128.cfg`可引用NRPE配置的命令,如`check_nrpe!check_load`以监控负载。监控HTTP关键词使用`check_http`命令加参数,如`-I`指定IP,`-u`指定URL,`-s`指定关键词,可在`command.cfg`中定义如`check_http_word`命令,并在主机配置文件中引用。
28 13
|
10天前
|
编解码 小程序
无影云电脑产品使用黑神话悟空之:游戏服务器更新/配置问题
该文档主要介绍了使用无影云电脑玩《黑神话:悟空》时可能遇到的问题及解决方法,包括游戏服务器更新、配置问题、画质建议及如何开启帧数显示等内容,并提供了详细的步骤指导与参考链接。
|
13天前
|
监控 安全 网络协议
快速配置Linux云服务器
快速配置Linux云服务器
|
1月前
|
机器学习/深度学习 编解码 人工智能
阿里云gpu云服务器租用价格:最新收费标准与活动价格及热门实例解析
随着人工智能、大数据和深度学习等领域的快速发展,GPU服务器的需求日益增长。阿里云的GPU服务器凭借强大的计算能力和灵活的资源配置,成为众多用户的首选。很多用户比较关心gpu云服务器的收费标准与活动价格情况,目前计算型gn6v实例云服务器一周价格为2138.27元/1周起,月付价格为3830.00元/1个月起;计算型gn7i实例云服务器一周价格为1793.30元/1周起,月付价格为3213.99元/1个月起;计算型 gn6i实例云服务器一周价格为942.11元/1周起,月付价格为1694.00元/1个月起。本文为大家整理汇总了gpu云服务器的最新收费标准与活动价格情况,以供参考。
阿里云gpu云服务器租用价格:最新收费标准与活动价格及热门实例解析
|
6天前
|
Cloud Native Java 编译器
将基于x86架构平台的应用迁移到阿里云倚天实例云服务器参考
随着云计算技术的不断发展,云服务商们不断推出高性能、高可用的云服务器实例,以满足企业日益增长的计算需求。阿里云推出的倚天实例,凭借其基于ARM架构的倚天710处理器,提供了卓越的计算能力和能效比,特别适用于云原生、高性能计算等场景。然而,有的用户需要将传统基于x86平台的应用迁移到倚天实例上,本文将介绍如何将基于x86架构平台的应用迁移到阿里云倚天实例的服务器上,帮助开发者和企业用户顺利完成迁移工作,享受更高效、更经济的云服务。
将基于x86架构平台的应用迁移到阿里云倚天实例云服务器参考
|
4天前
|
编解码 前端开发 安全
通过阿里云的活动购买云服务器时如何选择实例、带宽、云盘
在我们选购阿里云服务器的过程中,不管是新用户还是老用户通常都是通过阿里云的活动去买了,一是价格更加实惠,二是活动中的云服务器配置比较丰富,足可以满足大部分用户的需求,但是面对琳琅满目的云服务器实例、带宽和云盘选项,如何选择更适合自己,成为许多用户比较关注的问题。本文将介绍如何在阿里云的活动中选择合适的云服务器实例、带宽和云盘,以供参考和选择。
通过阿里云的活动购买云服务器时如何选择实例、带宽、云盘
|
2天前
|
弹性计算 运维 安全
阿里云轻量应用服务器和经济型e实例区别及选择参考
目前在阿里云的活动中,轻量应用服务器2核2G3M带宽价格为82元1年,2核2G3M带宽的经济型e实例云服务器价格99元1年,对于云服务器配置和性能要求不是很高的阿里云用户来说,这两款服务器配置和价格都差不多,阿里云轻量应用服务器和ECS云服务器让用户二选一,很多用户不清楚如何选择,本文来说说轻量应用服务器和经济型e实例的区别及选择参考。
阿里云轻量应用服务器和经济型e实例区别及选择参考
|
3天前
|
机器学习/深度学习 存储 人工智能
阿里云GPU云服务器实例规格gn6v、gn7i、gn6i实例性能及区别和选择参考
阿里云的GPU云服务器产品线在深度学习、科学计算、图形渲染等多个领域展现出强大的计算能力和广泛的应用价值。本文将详细介绍阿里云GPU云服务器中的gn6v、gn7i、gn6i三个实例规格族的性能特点、区别及选择参考,帮助用户根据自身需求选择合适的GPU云服务器实例。
阿里云GPU云服务器实例规格gn6v、gn7i、gn6i实例性能及区别和选择参考

热门文章

最新文章