日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HAProxy 日志管理

發布時間:2024/1/1 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HAProxy 日志管理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、HAProxy 配置

HAProxy 日志采用默認配置:

cat /etc/haproxy/haproxy.cfg

globallog /dev/log local0log /dev/log local1 notice

注意:

  • 如果后臺服務不是 http,而是 redis 等,則對應的 listen 需要覆蓋指定:option tcplog。
  • 日志的 level:local0~local7,16~23保留為本地使用,其中:
    • 0:emerg,系統不可用;
    • 1:alert,必須馬上采取行動的事件;
    • 2:crit,關鍵的事件;
    • 3:err,錯誤事件;
    • 4:warning,警告事件;
    • 5:notice,普通但重要的事件;
    • 6:info,有用的信息;
    • 7:debug,調試信息。

二、修改 /etc/rsyslog.d/49-haproxy.conf

原文件如下:

注釋紅線部分,并在其下面添加:

$ModLoad imudp $UDPServerRun 514$template haproxylog,"/root/haproxy/logs/haproxy-%$year%-%$month%-%$day%.log" #template(name="haproxylog" type="string" string="/root/haproxy/logs/haproxy-%$year%-%$month%-%$day%.log")$template logformat,"%TIMESTAMP:8:15%.%timestamp:::date-subseconds% %syslogtag% %msg%\n"if $programname startswith 'haproxy' then ?haproxylog; logformat&~

注意:

  • 文件名不一定是 49-haproxy.conf,不過都沒關系,只要文件后綴是“.conf”,并且文件位于目錄:/etc/rsyslog.d/ 就可以。這是因為服務 rsyslog 的配置文件:/etc/rsyslog.conf 中有一句話:

    $IncludeConfig /etc/rsyslog.d/*.conf

    所以,如果我們要自定義某個服務的日志,最好在 /etc/rsyslog.d/ 目錄下創建相應的文件,而不是直接修改 /etc/rsyslog.conf。

  • 由于 HAProxy 的日志是采用 UDP 傳輸,所以需啟用 rsyslog 的 udp監聽。

  • 兩種定義 template 路徑的語句都是正確的,但無論使用哪一種格式,在引用 template 路徑時,必須在 template 名稱前加一個 “?”。

  • 最后一行:&~,如果不加這條的配置,則除了將日志寫入自定義文件之外,同時會寫入 /var/log/messages。

重啟 rsyslog 與 HAProxy:

systemctl restart rsyslog
systemctl restart haproxy

查看日志文件:

三、設置日志內容格式

HAProxy 默認日志比較雜亂,字段含義也比較難懂。

可使用 log-format,對格式進行自定義:

listen proxy_twbind :16001option tcplogmode tcpbalance roundrobinserver tw_proxy_1 192.168.255.128:22121 check inter 10sserver tw_proxy_2 192.168.177.128:22121 check inter 10slog globaloption logasaplog-format client:%ci:%cp\ frontend:%fi:%fp(%f)\ backend:%bi:%bp(%b)\ real_server:%si:%sp(%s)\ status_code:%ST\ retries:%rc\ termination_state:%tslisten proxy_webbind :16002mode httpbalance roundrobin#option forwardfortimeout server 15stimeout connect 15s#option httpcloseoption redispatchlog globaloption logasaplog-format client:%ci:%cp\ URI:/%HU\ frontend:%fi:%fp(%f)\ backend:%bi:%bp(%b)\ real_server:%si:%sp(%s)\ status_code:%ST\ http_request:%r\ retries:%rc\ termination_state:%tsserver web_proxy_1 192.168.255.128:8571 check inter 10sserver web_proxy_2 192.168.255.128:8572 check inter 10sserver web_proxy_3 192.168.177.128:8571 check inter 10sserver web_proxy_4 192.168.177.128:8572 check inter 10s


注意:

  • tcplog 與 httplog 模式下,某些字段不通用,注意區分,否則 HAProxy 可能無法啟動;

  • 若某個后端不想寫日志,比如 HAProxy web 管理平臺,每次刷新會寫兩條記錄,可以將“log global”只放到需要寫日志的后端配置下;

  • 默認情況下,tcplog 與 httplog 模式只會在連接中斷后寫日志,以便能夠在日志中記錄時間和字節等信息。這樣可能導致,tcp 連接只要不關閉,永遠不會寫日志。解決方法:option logasap,表示只要服務發送了完整的 header,就馬上寫日志。缺點:

    • 日志中可能缺少某些數據;
    • 連接關閉時,不會寫日志;
  • 日志格式:
    HAProxy 配置手冊

    Rvarfield name (8.2.2 and 8.2.3 for description)type
    %ospecial variable, apply flags on all next var
    %Bbytes_read (from server to client)numeric
    H%CCcaptured_request_cookiestring
    H%CScaptured_response_cookiestring
    %Hhostnamestring
    H%HMHTTP method (ex: POST)string
    H%HPHTTP request URI without query string (path)string
    H%HQHTTP request URI query string (ex: ?bar=baz)string
    H%HUHTTP request URI (ex: /foo?bar=baz)string
    H%HVHTTP version (ex: HTTP/1.0)string
    %IDunique-idstring
    %STstatus_codenumeric
    %Tgmt_date_timedate
    %TcTcnumeric
    %TdTd = Tt - (Tq + Tw + Tc + Tr)numeric
    %Tllocal_date_timedate
    H%TqTqnumeric
    H%TrTrnumeric
    %Tstimestampnumeric
    %TtTtnumeric
    %TwTwnumeric
    %Ubytes_uploaded (from client to server)numeric
    %acactconnnumeric
    %bbackend_namestring
    %bcbeconn (backend concurrent connections)numeric
    %bibackend_source_ip (connecting address)IP
    %bpbackend_source_port (connecting address)numeric
    %bqbackend_queuenumeric
    %ciclient_ip (accepted address)IP
    %cpclient_port (accepted address)numeric
    %ffrontend_namestring
    %fcfeconn (frontend concurrent connections)numeric
    %fifrontend_ip (accepting address)IP
    %fpfrontend_port (accepting address)numeric
    %ftfrontend_name_transport (’~’ suffix for SSL)string
    %lcfrontend_log_counternumeric
    %hrcaptured_request_headers default stylestring
    %hrlcaptured_request_headers CLF stylestring list
    %hscaptured_response_headers default stylestring
    %hslcaptured_response_headers CLF stylestring list
    %msaccept date milliseconds (left-padded with 0)numeric
    %pidPIDnumeric
    H%rhttp_requeststring
    %rcretriesnumeric
    %rtrequest_counter (HTTP req or TCP session)numeric
    %sserver_namestring
    %scsrv_conn (server concurrent connections)numeric
    %siserver_IP (target address)IP
    %spserver_port (target address)numeric
    %sqsrv_queuenumeric
    S%sslcssl_ciphers (ex: AES-SHA)string
    S%sslvssl_version (ex: TLSv1)string
    %tdate_time (with millisecond resolution)date
    %tstermination_statestring
    H%tsctermination_state with cookie statusstring

總結

以上是生活随笔為你收集整理的HAProxy 日志管理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。