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
...此处省略...
ADD php-fpm-exporter.linux.amd64 /php-fpm-exporter
RUN chmod 755 /php-fpm-exporter
ENTRYPOINT ["/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 截图
posted @ 2020-11-25 12:55  龍龍小宝  阅读(562)  评论(0编辑  收藏  举报