我正在通过Helm图表将Prometheus-operator部署到我的集群中,但是我实现了一个自定义服务来监视我的应用程序,我需要将我的服务添加到Prometheus-操作符中,以查看我的度量数据。
我怎么能这么做?
发布于 2020-08-27 05:16:49
首先,您需要通过Helm或手动部署Prometheus操作符:
# By Helm:
$ helm install stable/prometheus-operator --generate-name
# By manual: for release `release-0.41`
kubectl apply -f https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/release-0.41/bundle.yaml如果您的集群启用了RBAC,那么您需要为Prometheus对象安装RBAC组件:
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
name: prometheus
rules:
- apiGroups: [""]
resources:
- nodes
- nodes/metrics
- services
- endpoints
- pods
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources:
- configmaps
verbs: ["get"]
- nonResourceURLs: ["/metrics"]
verbs: ["get"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus
subjects:
- kind: ServiceAccount
name: prometheus
namespace: default然后需要部署Promethues对象:
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: prometheus
labels:
prometheus: prometheus
spec:
replicas: 1
serviceAccountName: prometheus
serviceMonitorSelector:
matchLabels:
k8s-app: prometheus
serviceMonitorNamespaceSelector:
matchLabels:
prometheus: prometheus
resources:
requests:
memory: 400Mi在这里,Prometheus对象将选择满足以下条件的所有ServiceMonitor:
ServiceMonitor将在具有prometheus: prometheus标签的命名空间中创建k8s-app: prometheus label.ServiceMonitor。ServiceMonitor有一个标签选择器来选择服务及其底层端点对象。示例应用程序的服务对象通过具有app值的example-app标签选择Pods。Service对象还指定了公开度量的端口。
kind: Service
apiVersion: v1
metadata:
name: example-app
labels:
app: example-app
spec:
selector:
app: example-app
ports:
- name: web
port: 8080这个服务对象是由一个ServiceMonitor发现的,它以相同的方式进行选择。app标签必须具有example-app值。
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: example-app
labels:
k8s-app: prometheus
spec:
selector:
matchLabels:
app: example-app
namespaceSelector:
# matchNames:
# - demo
any: true
endpoints:
- port: web在这里,namespaceSelector用于选择创建服务的所有名称空间。可以使用matchNames指定特定的任何命名空间。
您还可以根据需要在任何名称空间中创建ServiceMonitor。但是您需要在Prometheus cr的spec中指定它,例如:
serviceMonitorNamespaceSelector:
matchLabels:
prometheus: prometheus上面的serviceMonitorNamespaceSelector在Prometheus运算符中用于选择带有标签prometheus: prometheus的命名空间。假设您有一个名称空间demo,并且在这个demo名称空间中创建了一个Prometheus,那么您需要使用修补程序在demo名称空间中添加标签prometheus: prometheus:
$ kubectl patch namespace demo -p '{"metadata":{"labels": {"prometheus":"prometheus"}}}'您可以在这里找到更多详细信息:
https://github.com/helm/charts/tree/master/stable/prometheus-operator
https://stackoverflow.com/questions/63606347
复制相似问题