本文将指导您在kubernetes集群上设置Prometheus,以监视kubernetes集群。
本次将使用Prometheus服务发现配置自动收集节点、pod和服务指标。
一、Prometheus 介绍
Prometheus 是一个高可伸缩的开源监控框架。它为Kubernetes容器编排平台提供了开箱即用的监控功能。此外,在可观察性领域,因其能很好的实现指标收集和告警,也被越来越多的公司和企业使用。
详细介绍 Prometheus 超出了本文的范围。如果你想了解更多关于 Prometheus 的知识,你可以访问其官网(Prometheus - Monitoring system & time series database)。在此,我想列出几个要点供您参考。
- 指标收集: Prometheus 使用拉取模型通过 HTTP 协议检索度量。在 Prometheus 无法抓取指标的情况下,可以使用 Pushgateway 将指标推送给 Prometheus。下面是一个使用 Pushgateway 将指标推送给 Prometheus 的实例()。
- 指标端点: 使用 Prometheus 监视的系统应该在 /metrics 端点上暴露指标。Prometheus 使用这个端点定期抽取指标。
- PromQL: Prometheus 附带了 PromQL,这是一种非常灵活的查询语言,可用于查询Prometheus 仪表板中的指标。此外,Prometheus UI 和 Grafana 将使用 PromQL 查询来可视化指标。
- Prometheus exports: exports 是将第三方应用程序中的现有指标标准转换为 Prometheus 指标标准格式的库。有许多官方和社区的 Prometheus exports。Prometheus node exporter 就是一个例子。它以 Prometheus 格式公开了所有Linux系统级指标。
- 时序数据库(TSDB): Prometheus 使用 TSDB 有效地存储所有数据。默认情况下,所有数据都存储在本地。但是,为了避免单点故障,可以选择为 Prometheus TSDB 集成远端存储。
二、Prometheus 架构
如果您想在Linux虚拟机上安装 Prometheus,请参阅 Installation | Prometheus。
Kubernetes Prometheus 监控堆栈包含以下组件。
- Prometheus 服务器
- 告警管理
- Grafana
三、Prometheus监控Kubernetes集群
最新的 Prometheus 可以在其官方 docker hub 帐户中以 docker 镜像(prometheus/prometheus · Quay)的形式获得。我们将使用该图像进行安装。
3.1 连接到 Kubernetes 集群
连接到 Kubernetes 集群,并确保您拥有创建集群角色的管理员权限。
ACCOUNT=$(gcloud info --format='value(config.account)')
kubectl create clusterrolebinding owner-cluster-admin-binding \
--clusterrole cluster-admin \
--user $ACCOUNT
3.2 Prometheus Kubernetes Manifest 文件
本指南中提到的所有配置文件都托管在 Github 上。您可以使用以下命令clone repo。
git clone https://github.com/techiescamp/kubernetes-prometheus
您可以使用 GitHub repo 配置文件,也可以创建文件,以便更好地理解,如步骤中所述。
3.3 创建Namespace & ClusterRole
首先,我们将为所有监视组件创建一个 Kubernetes Namespace。如果不创建专用的 Namespace ,那么所有 Prometheus kubernetes 部署对象都将部署在默认 Namespace 上。
执行以下命令创建一个名为 monitoring 的新 Namespace 。
kubectl create namespace monitoring
Prometheus 使用 Kubernetes api 从 node、pod、deployment 等读取所有可用的指标。出于这个原因,我们需要创建一个具有对所需 API 组的读访问权限的 RBAC 策略,并将该策略绑定到 monitoring Namespace。
3.3.1 创建名为 clusterRole.yaml 的文件:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: prometheus
rules:
- apiGroups: [""]
resources:
- nodes
- nodes/proxy
- services
- endpoints
- pods
verbs: ["get", "list", "watch"]
- apiGroups:
- extensions
resources:
- ingresses
verbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus
subjects:
- kind: ServiceAccount
name: default
namespace: monitoring
3.3.2. 使用以下命令创建角色
kubectl create -f clusterRole.yaml
3.4 创建 Prometheus 配置文件
Prometheus 的所有配置在 prometheus.yaml 文件中。在 prometheus.rules 中配置了Alertmanager 的所有告警规则。
prometheus.yaml: ****这是Prometheus的主要配置文件,包含所有的数据源配置、服务发现细节、存储位置、数据保留配置等。
prometheus.rules:该文件包含所有 Prometheus 告警规则。
通过将 Prometheus 配置外部化到 Kubernetes 配置映射,你不必在需要添加或删除配置时构建Prometheus 映像。你需要更新配置映射并重新启动 Prometheus pod以 应用新的配置。
带有所有 Prometheus 刮削配置和警报规则的配置映射被挂载到 /etc/prometheus 位置中的Prometheus 容器中,作为 prometheus.yaml 和 prometheus.rules 文件。
1、创建一个名为config-map的文件。从这个链接- > Prometheus Config file复制文件内容。
2、执行以下命令在Kubernetes中创建配置映射。
kubectl create -f config-map.yaml
3.5 部署 Prometheus
1、创建一个名为 prometheus-deployment.yaml 的文件。并将以下内容复制到该文件中。在此配置中,我们将Prometheus配置映射作为文件挂载到 /etc/prometheus 中,如前一节所述。
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus-deployment
namespace: monitoring
labels:
app: prometheus-server
spec:
replicas: 1
selector:
matchLabels:
app: prometheus-server
template:
metadata:
labels:
app: prometheus-server
spec:
containers:
- name: prometheus
image: prom/prometheus
args:
- "--storage.tsdb.retention.time=12h"
- "--config.file=/etc/prometheus/prometheus.yml"
- "--storage.tsdb.path=/prometheus/"
ports:
- containerPort: 9090
resources:
requests:
cpu: 500m
memory: 500M
limits:
cpu: 1
memory: 1Gi
volumeMounts:
- name: prometheus-config-volume
mountPath: /etc/prometheus/
- name: prometheus-storage-volume
mountPath: /prometheus/
volumes:
- name: prometheus-config-volume
configMap:
defaultMode: 420
name: prometheus-server-conf
- name: prometheus-storage-volume
emptyDir: {}
2、使用上述文件创建监视名称空间的部署。
kubectl create -f prometheus-deployment.yaml
3、使用上述文件创建监视名称空间的部署。
kubectl get deployments --namespace=monitoring
四、连接 Prometheus 控制面板
使用 kubectl 端口转发,您可以使用本地主机上选定的端口从本地工作站访问 pod。此方法主要用于调试。
1、首先,获取 Prometheus Pod 的名称。
kubectl get pods --namespace=monitoring
The output will look like the following.
➜ kubectl get pods --namespace=monitoring
NAME READY STATUS RESTARTS AGE
prometheus-monitoring-3331088907-hm5n1 1/1 Running 0 5m
2、 使用 pod 名称执行以下命令,从本地主机端口 8080 访问 Prometheus。
注意: 将 prometheus-monitoring-3331088907-hm5n1 替换为你的 pod 名称。
kubectl port-forward prometheus-monitoring-3331088907-hm5n1 8080:9090 -n monitoring
3、现在如果访问 http://localhost:8080,
就可登陆到 Prometheus 的 Home 页.
写不动了,今天先介绍到这里,后续还会介绍其他方式。