基本原理
php-fpm 接入容器后需要对fpm 的状态做监控, 这里选用这个插件php-fpm-exporter,基本原理就是开启php-fpm的状态页面,然后这个插件读取状态页的信息并装换为prometheus 格式的metrics接口供prometheus 采集
步骤
- php开启状态页和慢日志
ping.path = /ping
ping.response = pong
pm.status_path = /php-status
access.log = var/log/$pool.access.log
access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"
slowlog = /usr/local/php71/var/log/$pool.log.slow
request_slowlog_timeout = 3s
- 获取插件并配置
获取二进制,通过supervisor 或者sidecar 方式启动,具体参数可以参考GitHub,
php-fpm-exporter --addr 0.0.0.0:9999 --fastcgi tcp://127.0.0.1:9000/php-status
- prometheus配置自动发现
在对应的服务中,添加自动发现注解
# deployment中添加注解
spec:
replicas: 10
revisionHistoryLimit: 10
selector:
matchLabels:
app: fpm-monitoring-test
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
# 添加部分
annotations:
prometheus.io/port: "9999" # 对应启动命令中的--addr 参数
prometheus.io/scrape: php-fpm
- prometheus配置采集
因为使用的是prometheus-operator,无法直接操作prometheus的配置, 这里采用additionalScrapeConfigs的方式, 将配置保存为一个密文,并挂载到prometheus中
# cat prometheus-addition-config.yaml
- job_name: 'kubernetes-php-fpm'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: php-fpm
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
regex: ([^:]+)(?::\d+)?;(\d+)
replacement: $1:$2
target_label: __address__
- action: labelmap
regex: __meta_kubernetes_pod_label_(.+)
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: namespace
- source_labels: [__meta_kubernetes_pod_name]
action: replace
target_label: pod
# kubectl create secret generic -n monitoring prometheus-addition-config --from-file=prometheus-addition-config.yaml
# kubectl edit prometheus -n monitoring k8s
...部分省略
spec:
additionalScrapeConfigs: # 增加部分
key: prometheus-addition-config.yaml # 增加部分
name: prometheus-addition-config # 增加部分
alerting:
alertmanagers:
- name: alertmanager-main
namespace: monitoring
port: web
arbitraryFSAccessThroughSMs: {}
baseImage: registry.cn-shanghai.aliyuncs.com/leozhanggg/prometheus/prometheus
externalUrl: http://prometheus.piggy.xiaozhu.com
nodeSelector:
kubernetes.io/os: linux
podMonitorSelector: {}
...
-
查看prometheus是否抓取到数据
p.png
6.导入grafana 数据
dashboardID: 13281
网友评论