Prometheus官网地址:https://prometheus.io/
GitHub地址:https://github.com/prometheus/prometheus
一、prometheus的安装
Prometheus是最初由SoundCloud开发的监控和警报工具,于2016年加入了 Cloud Native Computing Foundation,这是继Kubernetes之后的第CNC的二个托管项目。
1、prometheus的特征
(1)具有由metric名称和键/值对标识的时间序列数据的多维数据模型
(2)有⼀个灵活的查询语⾔
(3)不依赖分布式存储,只和本地磁盘有关
(4)通过HTTP的服务拉取时间序列数据
(5)⽀持推送的⽅式来添加时间序列数据
(6)⽀持通过服务发现或静态配置发现⽬标
(7)多种图形和仪表板⽀持
2、prometheus的组件
Prometheus生态系统包含许多组件,部分组件是可选组件,主要的组件如下:
(1)Prometheus Server:⽤于抓取指标、存储时间序列数据
(2)exporter:暴露指标让任务来抓
(3)pushgateway:push的⽅式将指标数据推送到该⽹关
(4)alertmanager:处理报警的报警组件
(5)adhoc:⽤于数据查询的组件

3、prometheus的安装
Prometheus的二进制安装非常简单,只需要在prometheus的官方网站下载对应版本的二进制包,可通过prometheus的程序制定配置文件即可启动。此处我的prometheus运行在k8s集群中,所以我通过deployment控制器去运行prometheus运行。
(1)创建prometheus配置文件
默认的prometheus的配置为如下:
~]# cat prometheus-2.21.0.linux-386/prometheus.yml | grep -Ev "^#|^[[:space:]]{1,}#|^$"
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
alerting:
alertmanagers:
- static_configs:
- targets:
rule_files:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
Prometheus的主要几个配置参数说明如下:
1)scrape_interval:表示 prometheus 抓取指标数据的频率,默认是15s
2)evaluation_interval:⽤来控制评估规则的频率,prometheus使⽤规则产⽣新的时间序列数据或者产⽣警报
3)rule_files 模块制定了规则所在的位置,prometheus可以根据这个配置加载规则,⽤于⽣成新的时间 序列数据或者报警信息
4)scrape_configs:⽤于控制prometheus监控哪些资源。由于 prometheus通过HTTP的⽅式来暴露的它本身的监控数据,prometheus也能够监控本身的健康情况。在默认的配置⾥有⼀个单独的job叫 做prometheus,它采集prometheus 服务本身的时间序列数据。这个job包含了⼀个单独的、静态配 置的⽬标:监听localhost上的9090端⼝。prometheus 默认会通过⽬标的/metrics路径采集metrics。
为了方便在k8s中prometheus使用配置文件,我们需要将配置文件通过ConfigMap的形式进行管理
# 定义所有的资源之前先创建一个名称空间
~]# kubectl create namespace prometheus
# 定义prometheus的configmap资源
]# cat prometheus-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
namespace: prometheus
data:
prometheus.yml: |
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
# 创建资源
]# kubectl apply -f prometheus-cm.yaml
configmap/prometheus-config created
(2)为prometheus中的数据库创建存储资源
为了防止prometheus所在的pod故障导致数据丢失,我们需要为prometheus的TSDB创建一个pvc。这里使用之前创建的基于nfs的存储类实现动态的pv供给。
# 定义pvc资源
]# cat prometheus-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: prometheus
namespace: prometheus
annotations:
volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
# 创建资源
]# kubectl apply -f prometheus-pvc.yaml
persistentvolumeclaim/prometheus created
(3)通过rbac为prometheus授权
由于prometheus需要访问Kubernetes集群中的相关信息,所以我们需要为prometheusc创建一个ServiceAccount,并授予相关的权限。
# 定义sa资源
]# cat prometheus-sa.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus
namespace: prometheus
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: prometheus
namespace: prometheus
rules:
- apiGroups:
- ""
resources:
- nodes
- services
- endpoints
- pods
- nodes/proxy
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- configmaps
- nodes/metrics
verbs:
- get
- nonResourceURLs:
- /metrics
verbs:
- get
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: prometheus
namespace: prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus
subjects:
- kind: ServiceAccount
name: prometheus
namespace: Prometheus
# 创建sa资源
]# kubectl apply -f prometheus-sa.yaml
serviceaccount/prometheus created
clusterrole.rbac.authorization.k8s.io/prometheus created
clusterrolebinding.rbac.authorization.k8s.io/prometheus created
(4)为prometheus创建deployment控制器
相关的配置文件及依赖的存储资源定义好之后就可以为prometheus创建deployment控制器。
# 定义deployment控制器资源
]# cat prometheus-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus
namespace: prometheus
labels:
app: prometheus
spec:
selector:
matchLabels:
app: prometheus
template:
metadata:
labels:
app: prometheus
spec:
serviceAccountName: prometheus
containers:
- image: prom/prometheus:v2.21.0
name: prometheus
command:
- "/bin/prometheus"
args:
- "--config.file=/etc/prometheus/prometheus.yml"
- "--storage.tsdb.path=/prometheus"
- "--storage.tsdb.retention=24h"
#控制对adminHTTPAPI的访问,其中包括删除时间序列等功能
- "--web.enable-admin-api"
#⽀持热更新,直接执⾏localhost:9090/-/reload⽴即⽣效
- "--web.enable-lifecycle"
ports:
- containerPort: 9090
protocol: TCP
name: http
volumeMounts:
- mountPath: "/prometheus"
subPath: prometheus
name: data
- mountPath: "/etc/prometheus"
name: config-volume
resources:
requests:
cpu: 200m
memory: 512Mi
limits:
cpu: 200m
memory: 512Mi
# 由于prometheusdocker中是以nobody身份运行,所以要定义该项
securityContext:
runAsUser: 0
volumes:
- name: data
persistentVolumeClaim:
claimName: prometheus
- configMap:
name: prometheus-config
name: config-volume
# 创建并查看运行状态
]# kubectl apply -f prometheus-deployment.yaml
deployment.apps/prometheus created
]# kubectl get pods -n prometheus
NAME READY STATUS RESTARTS AGE
prometheus-97bf69799-64n7w 1/1 Running 0 9s
(5)定义service资源通过集群外部访问
Prometheus的pod控制器创建成功后,为了便于在k8s集群外部访问prometheus,我们需要为k8s集群创建nodeport类型的service资源。
# 定义service资源
]# cat prometheus-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: prometheus
namespace: prometheus
labels:
app: prometheus
spec:
selector:
app: prometheus
type: NodePort
ports:
- name: web
port: 9090
targetPort: 9090
# 创建并查看service资源
]# kubectl apply -f prometheus-svc.yaml
service/prometheus created
]# kubectl get svc -n prometheus
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
prometheus NodePort 10.111.61.27 <none> 9090:31736/TCP 16s
至此,prometheus监控已经在k8s集群中安装完毕,可以通过node节点ip加映射至node节点的端口号去访问设置prometheus监控。
二、prometheus的使用
Prometheus的监控数据指标是通过⼀个公开的(HTTP(S))数据接⼝获取到的,不需要单独安装监控的agent,只需要暴露⼀个metrics接⼝,Prometheus就会定期去拉取数据; 对于⼀些普通的HTTP服务,我们完全可以直接给这个服务添加⼀个/metrics接⼝暴露给 Prometheus;⽽且获取到的指标数据格式是⾮常易懂的,不需要太⾼的学习成本。现在很多服务从⼀开始就内置了⼀个/metrics 接⼝,⽐如Kubernetes的各个组件;有⼀些服务即使没有原⽣集成该接⼝,也完全可以使⽤⼀些exporter来获取到指标数据⽐如mysqld_exporter、node_exporter,类似于传统监控服务中的agent。
1、kubernetes集群节点的监控
(1)集群节点需要监控的内容
1)kubernetes集群node几点的监控,包括cpu,内存,负载、磁盘等
2)内部系统组件的状态:⽐如kube-scheduler、kube-controller-manager、kubedns/coredns等组件的详细运⾏状态
3)编排级的资源:⽐如Deployment的状态、资源请求、调度和API 延迟等数据指标
(2)kubernetes集群的监控方案
1)Heapster:Heapster是⼀个集群范围的监控和数据聚合⼯具,以 Pod 的形式运⾏在集群中,目前Heapster已经被metrics-server所代替。
2)cAdvisor:cAdvisor是 Google 开源的容器资源监控和性能分析⼯具,它是专⻔为容器⽽⽣,本身也⽀持 Docker 容器,在 Kubernetes 中,我们不需要单独去安装,cAdvisor 作为 kubelet 内置的⼀部分程序可以直接使⽤。
3)Kube-state-metrics:kube-state-metrics通过监听API Server⽣成有关资源对象的状态指标,⽐如Deployment、Node、Pod,需要注意的是kube-state-metrics只是简单提供⼀个metrics数据,并不会存储这些指标数据,所以我们可以使⽤Prometheus 来抓取这些数据然后存储。
4)metrics-server:metrics-server也是⼀个集群范围内的资源数据聚合⼯具,是Heapster的替代品,同样metrics-server也只是显示数据,并不提供数据存储服务。
在上述监控方案中,kube-state-metrics主要关注的是业务相关的⼀些元数据,⽐如Deployment、Pod、副本状态等,而metrics-server主要关注的是资源度量API的实现,⽐如CPU、⽂件描述符、内存、请求延时等指标
(3)使用Prometheus监控集群节点
使用Prometheus监控node节点,可以通过node_exporter服务来获取监控数据, node_exporter就是抓取⽤于采集服务器节点的各种运⾏指标,⽬前 node_exporter⽀持⼏乎所有常⻅的监控点。
node_exporter服务监控各节点可通过DaemonSet 控制器来部署,这样可以保证每个节点都可以运行一个该服务,即使节点扩容,也可保证在新节点上运行该服务。
# node_exporter服务的DaemonSet控制器资源清单
]# cat prome-node-exporter.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-exporter
namespace: prometheus
labels:
name: node-exporter
spec:
selector:
matchLabels:
name: node-exporter
template:
metadata:
labels:
name: node-exporter
spec:
# 使用宿主机pid、ipc、network命名空间
hostPID: true
hostIPC: true
hostNetwork: true
containers:
- name: node-exporter
image: prom/node-exporter:v1.0.1
ports:
- containerPort: 9100
resources:
requests:
cpu: 0.15
securityContext:
privileged: true
args:
- --path.procfs
- /host/proc
- --path.sysfs
- /host/sys
- --collector.filesystem.ignored-mount-points
- '"^/(sys|proc|dev|host|etc)($|/)"'
volumeMounts:
- name: dev
mountPath: /host/dev
- name: proc
mountPath: /host/proc
- name: sys
mountPath: /host/sys
- name: rootfs
mountPath: /rootfs
tolerations:
- key: "node-role.kubernetes.io/master"
operator: "Exists"
effect: "NoSchedule"
volumes:
- name: proc
hostPath:
path: /proc
- name: dev
hostPath:
path: /dev
- name: sys
hostPath:
path: /sys
- name: rootfs
hostPath:
path: /
# 创建并查看资源你运行情况
]# kubectl apply -f prome-node-exporter.yaml
]# kubectl get pods -n prometheus
NAME READY STATUS RESTARTS AGE
node-exporter-dxbfn 1/1 Running 0 4m54s
node-exporter-fncvm 1/1 Running 0 4m54s
node-exporter-s6jbf 1/1 Running 0 4m51s
通过DaemonSet控制器管理的node-exporter服务是共享了宿主机的网络名称空间,所以该pod运行起来后,各node节点会监听9100端口,所以我们可通过9100端口去获取监控数据。
# 查看各节点node-export监听端口
]# netstat -lntp | grep node_exporter
tcp6 0 0 :::9100 :::*

本文介绍了Prometheus在K8s集群中的安装、使用,包括其特征、组件及安装步骤。还阐述了对Kubernetes集群节点和常用资源对象的监控方法,以及Grafana的使用和告警配置。此外,详细说明了Prometheus Operator的安装、配置、自定义监控项和告警规则设置。

4682

被折叠的 条评论
为什么被折叠?



