HAProxy的高級配置選項-Web服務器狀態監測
作者:尹正傑
版權聲明:原創作品,謝絕轉載!否則將追究法律責任。
一.三種狀態監測方式概述
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的日志,如下圖所示。