Prometheus监控k8s(7)-prometheus使用exporter监控应用-redis
prometheus使用exporter监控应用-redis
参考https://www.qikqiak.com/k8s-book/docs/53.监控Kubernetes集群应用.html
应用可能没有自带/metrics
接口供 Prometheus 使用,在这种情况下,我们就需要利用 exporter 服务来为 Prometheus 提供指标数据了。Prometheus 官方为许多应用就提供了对应的 exporter 应用,也有许多第三方的实现,我们可以前往官方网站进行查看:exporters
通过一个redis-exporter的服务来监控 redis 服务,对于这类应用,我们一般会以 sidecar 的形式和主应用部署在同一个 Pod 中,比如我们这里来部署一个 redis 应用,并用 redis-exporter 的方式来采集监控数据供 Prometheus 使用,如下资源清单文件:(prome-redis.yaml)
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: redis namespace: kube-system spec: template: metadata: # annotations: # prometheus.io/scrape: "true" # prometheus.io/port: "9121" labels: app: redis spec: containers: - name: redis image: redis:4 resources: requests: cpu: 100m memory: 100Mi ports: - containerPort: 6379 - name: redis-exporter image: oliver006/redis_exporter:latest resources: requests: cpu: 100m memory: 100Mi ports: - containerPort: 9121 --- kind: Service apiVersion: v1 metadata: name: redis namespace: kube-system spec: selector: app: redis ports: - name: redis port: 6379 targetPort: 6379 - name: prom port: 9121 targetPort: 9121
redis 这个 Pod 中包含了两个容器,一个就是 redis 本身的主应用,另外一个容器就是 redis_exporter
[root@k8s-master prometheus]# kubectl apply -f prome-redis.yaml deployment.extensions/redis created service/redis created [root@k8s-master prometheus]# [root@k8s-master prometheus]# kubectl get pods -n kube-system | grep redis redis-69987696df-rrm6n 2/2 Running 0 61s [root@k8s-master prometheus]# kubectl get svc -n kube-system | grep redis redis ClusterIP 10.96.3.57 <none> 6379/TCP,9121/TCP 70s [root@k8s-master prometheus]#
我们可以通过 9121 端口来校验是否能够采集到数据:
[root@k8s-master prometheus]# curl 10.96.3.57:9121/metrics | grep redis_up % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 16548 0 16548 0 0 358k 0 --:--:-- --:--:-- --:--:-- 367k # HELP redis_up Information about the Redis instance # TYPE redis_up gauge redis_up 1 # HELP redis_uptime_in_seconds uptime_in_seconds metric # TYPE redis_uptime_in_seconds gauge redis_uptime_in_seconds 110 [root@k8s-master prometheus]#
同样的,现在我们只需要更新 Prometheus 的配置文件:
- job_name: 'redis' static_configs: - targets: ['redis:9121'] #同一个namespace
[root@k8s-master prometheus]# vim prometheus/prometheus-configmap.yaml [root@k8s-master prometheus]# [root@k8s-master prometheus]# kubectl apply -f prometheus/prometheus-configmap.yaml configmap/prometheus-config configured [root@k8s-master prometheus]# #隔一会儿执行reload操作 curl -X POST "http://10.109.8.226:9090/-/reload"
按我的prometheus安装方式,官方也默认配置pod自动发现,具体参考下一篇