K8S 上安装 Prometheus 并监控 K8S 集群
版本信息
- Centos 7
- Kubernetes 1.24
- Prometheus 2.44.0
- AlertManager 0.24.0
Prometheus 部署步骤
为 Prometheus 创建专用的 Namespace,此处创建 prometheus
kubectl create namespace prometheus |
创建集群角色
Prometheus 使用 Kubernetes API 从 Nodes、Pods、Deployments 等等中读取所有可用的指标。因此,我们需要创建一个包含读取所需 API 组的 RBAC 策略,并将该策略绑定到新建的 prometheus
命名空间。[1]
- 创建一个名为
prometheusClusterRole.yaml
的文件,并复制以下 RBAC 角色。在下面给出的角色中,可以看到,我们已经往
nodes
,services endpoints
,pods
和ingresses
中添加了get
,list
以及watch
权限。角色绑定被绑定到监控命名空间。如果有任何要从其他对象中检索指标的用例,则需要将其添加到此集群角色中。apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: prometheus
rules:
- apiGroups: [""]
resources:
- nodes
- nodes/proxy
- nodes/metrics
- 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: prometheus - 使用下面的命令创建角色
kubectl apply -f prometheusClusterRole.yaml