1. 程式人生 > 實用技巧 >Day6-01 如何監控K8S容器中的PHP程序

Day6-01 如何監控K8S容器中的PHP程序

一、概述

在生產環境中我們希望能夠通過prometheus監控容器中php程序的狀態,剛好看到一個比較好的解決辦法,利用php-fpm-exporter對php-fpm進行監控,但想實現該需求需要具備以下條件:

  • php-fpm開啟status介面
  • nginx代理php-fpm介面
  • 使用php-fpm-exporter暴露指標給prometheus

官方GitHub:
https://github.com/bakins/php-fpm-exporter
https://rtcamp.com/tutorials/php/fpm-status-page/

二、php-fpm開啟status介面

  • 編輯 php-fpm.conf 配置檔案,在其中增加以下兩行配置項
...
pm.status_path = /php_status
ping.path = /ping
...
  • 配置 nginx
  server {
      server_name 127.0.0.1;
      location ~ ^/(php_status|ping)$ {
          include fastcgi_params;
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }
  }
  • 檢查status是否可以訪問
shell> curl http://127.0.0.1/php_status

三、使用php-fpm-exporter暴露指標

  • 下載二進位制檔案
shell> wget https://github.com/bakins/php-fpm-exporter/releases/download/v0.6.1/php-fpm-exporter.linux.amd64
shell> chmod +x php-fpm-exporter.linux.amd64
  • 封裝到底包映象中
shell> cat Dockerfile
...此處省略...
RUN mkdir /opt/monitor/
ADD php-fpm-exporter.linux.amd64 /opt/monitor/php-fpm-exporter
ENTRYPOINT ["/opt/monitor/php-fpm-exporter","--addr","0.0.0.0:9190","--endpoint","http://127.0.0.1/php_status"]
  • 檢查metrics是否可以訪問
shell> curl http://127.0.0.1:9190/metrics

  • 瀏覽器訪問

四、配置prometheus,採集php-fpm資料

動態發現和靜態配置規則根據喜好二選一即可

  • 編輯prometheus.yml,增加php-fpm任務(動態發現)
  - job_name: 'php-fpm'
    kubernetes_sd_configs:
      - role: pod
    relabel_configs:
      - action: labelmap
        regex: __meta_kubernetes_pod_label_(.+)
      - source_labels: [__meta_kubernetes_namespace]
        action: replace
        target_label: kubernetes_namespace
      - source_labels: [__meta_kubernetes_pod_name]
        action: keep
        regex: .*php.*
      - source_labels: [__meta_kubernetes_pod_ip]
        action: replace
        regex: (.+)
        target_label: __address__
        replacement: ${1}:9190
  • 編輯prometheus.yml,增加php-fpm任務(靜態配置)
  - job_name: 'php-fpm'
    static_configs:
      - targets:
        - 172.24.101.6:9190
  • 附配置生效後的 prometheus 截圖