基本原理
php-fpm 接入容器后需要對(duì)fpm 的狀態(tài)做監(jiān)控宗挥, 這里選用這個(gè)插件php-fpm-exporter跷睦,基本原理就是開啟php-fpm的狀態(tài)頁(yè)面囱皿,然后這個(gè)插件讀取狀態(tài)頁(yè)的信息并裝換為prometheus 格式的metrics接口供prometheus 采集
步驟
- php開啟狀態(tài)頁(yè)和慢日志
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
- 獲取插件并配置
獲取二進(jìn)制氏堤,通過supervisor 或者sidecar 方式啟動(dòng),具體參數(shù)可以參考GitHub,
php-fpm-exporter --addr 0.0.0.0:9999 --fastcgi tcp://127.0.0.1:9000/php-status
- prometheus配置自動(dòng)發(fā)現(xiàn)
在對(duì)應(yīng)的服務(wù)中响谓,添加自動(dòng)發(fā)現(xiàn)注解
# 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" # 對(duì)應(yīng)啟動(dòng)命令中的--addr 參數(shù)
prometheus.io/scrape: php-fpm
- prometheus配置采集
因?yàn)槭褂玫氖莗rometheus-operator债查,無(wú)法直接操作prometheus的配置陕赃, 這里采用additionalScrapeConfigs的方式卵蛉, 將配置保存為一個(gè)密文,并掛載到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是否抓取到數(shù)據(jù)
6.導(dǎo)入grafana 數(shù)據(jù)
dashboardID: 13281