当前位置:网站首页>Prometheus Operator使用指南笔记
Prometheus Operator使用指南笔记
2022-07-23 05:45:00 【Sindweller5530】
本篇博文主要是翻译User Guide of Prometheus Operator并记录笔记
从 v0.39.0 开始,Prometheus Operator 需要使用 Kubernetes v1.16.x 及更高版本。
概念简介
- Operator 是一种可以操作其他软件的软件,就是将人所收集的操作经验转化为软件。
- Prometheus Operator旨在尽可能简单地在Kubernetes上运行Prometheus,同时保持K8s原生配置选项。
manifest举例
先决条件:一个可以访问的k8s集群
相关资源:引入额外的资源来声明Prometheus和Alertmanager集群的期望状态以及Prometheus配置
- Prometheus(声明性地描述了Prometheus部署的期望状态)
- Alertmanager
- ServiceMonitor(重点,描述了 Prometheus 监视的目标集合。)

Prometheus 资源包括一个名为serviceMonitorSelector的字段,它定义了要使用的 ServiceMonitor 。
关于不同namespace的问题,默认情况下,在 v0.19.0 版本之前,ServiceMonitors 必须安装在与 Prometheus 实例相同的命名空间中。 使用 Prometheus Operator v0.19.0 及更高版本,可以通过 Prometheus 资源的 serviceMonitorNamespaceSelector 字段在 Prometheus 自身的namespace之外选择 ServiceMonitors。
首先部署一个简单的实例应用,在8080端口上监听并暴露指标。
- Deployment 部署无状态应用,主要是创建、更新、回滚
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-app # app的名字
spec:
replicas: 3 # 复制三个
selector: # 标签选择器
matchLabels: # 选择器选择匹配的标签
app: example-app # 指明是哪个
###
template:
metadata:
labels:
app: example-app
###
spec:
containers:
- name: example-app
image: fabxc/instrumented_app # 该app的镜像
ports:
- name: web
containerPort: 8080 # 容器端口是8080
ServiceMonitor 有一个标签选择器(label selector 见第二个备注)来选择服务及其底层 Endpoint 对象。 示例应用程序的 Service 对象通过具有 example-app 值的 app 标签选择 Pod。 Service 对象还指定了公开指标的端口为8080(注意这次不是容器端口,上边的是容器端口)。
- service对象
kind: Service # 上边是deployment这里是service了
apiVersion: v1
metadata: # 跟上边的template下的一样
name: example-app
labels:
app: example-app
spec:
###
selector:
app: example-app # 通过app标签,如果其值为example-app则选择这个pod
###
ports: #这里开始与上面containers里的ports内容相同,不过端口是实际端口
- name: web
port: 8080
该 Service 对象由 ServiceMonitor 发现,它以相同的方式进行选择。 app 标签的值必须是 example-app。
- ServiceMonitor
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor # 现在这是一个ServiceMonitor
metadata:
name: example-app
labels:
team: frontend
spec:
selector: # 与deployment和service中的选择方式相同
matchLabels:
app: example-app
endpoints: # endpoint是k8s集群中的一个资源对象,存储在etcd中,用来记录一个service对应的所有pod的访问地址
- port: web
为 Prometheus pod 启用 RBAC 规则
如果激活了 RBAC 授权,您必须为 Prometheus 和 Prometheus Operator 创建 RBAC 规则。 在上面的示例 Prometheus Operator manifest中为 Prometheus Operator 创建了 ClusterRole 和 ClusterRoleBinding。 必须对 Prometheus Pod 执行相同的操作。如下:
- 为 Prometheus Pod 创建 ClusterRole 和 ClusterRoleBinding:
- 首先创建一个service账户,名为prometheus
apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus
- 然后创建clusterrole,集群角色
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: prometheus
rules:
- apiGroups: [""]
resources:
- nodes
- nodes/metrics
- services
- endpoints
- pods
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources:
- configmaps
verbs: ["get"]
- apiGroups:
- networking.k8s.io
resources:
- ingresses
verbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]
verbs: ["get"]
- 然后创建binding
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: prometheus
roleRef: # 引用的role,是上面的clusterrole-prometheus
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus
subjects: # 条目为服务账户
- kind: ServiceAccount
name: prometheus
namespace: default # 注意这里prometheus的namespace
包含ServiceMonitors
一个Prometheus 对象定义了 serviceMonitorSelector 以指定哪个 ServiceMonitor应被包含在内。 在上边service monitor的label指定了 team: frontend , Prometheus 对象会通过这个label来选中service monitor。
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: prometheus
spec:
serviceAccountName: prometheus # 上面的service account
serviceMonitorSelector: # 选择哪个service monitor
matchLabels:
team: frontend
resources:
requests:
memory: 400Mi
enableAdminAPI: false
如果您已激活 RBAC 授权,请改用支持 RBAC 的 Prometheus 清单。
这使frontend team能够创建新的 ServiceMonitors 和服务,从而允许动态重新配置 Prometheus。
包含PodMonitors
最后,Prometheus 对象定义了 podMonitorSelector 以指定应包含哪些 PodMonitor。 label指定了 team: frontend , Prometheus 对象会通过这个label来选中service monitor。
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: prometheus
spec:
serviceAccountName: prometheus
podMonitorSelector: # 现在换成了podMonitor的Selector
matchLabels:
team: frontend # 同样是frontend team
resources:
requests:
memory: 400Mi
enableAdminAPI: false
暴露Prometheus实例、
要访问 Prometheus 实例,它必须对外暴露。 下例使用 NodePort 类型的服务来暴露实例。
apiVersion: v1
kind: Service
metadata:
name: prometheus
spec:
type: NodePort # 使可以在集群外访问
ports:
- name: web
nodePort: 30900 # 这是节点内端口
port: 9090 # 默认端口
protocol: TCP
targetPort: web
selector:
prometheus: prometheus
创建此服务后,Prometheus Web UI 在端口 30900 上的节点 IP 地址下可用。 Web UI 中的目标页面现在显示已成功发现示例应用程序的实例。
暴露Prometheus Admin API
Prometheus Admin API 允许访问并删除特定时间范围内序列、清理tombstones、捕获快照等。默认情况下禁用此 API 访问,可以使用此boolean flag-enableAdminAPI切换。以下示例暴露了Admin API:
警告:启用管理 API 会启用变异端点、删除数据、关闭 Prometheus 等。 应谨慎启用此功能,建议用户通过代理添加额外的身份验证授权,以确保只有授权执行这些操作的客户端才能这样做。
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: prometheus
spec:
serviceAccountName: prometheus
serviceMonitorSelector: # 还是service monitor的选择器
matchLabels:
team: frontend
resources:
requests:
memory: 400Mi
enableAdminAPI: true #
边栏推荐
猜你喜欢

【AUTOSAR COM 2.通信协议栈进阶介绍】

【Autosar CP通用 1.如何阅读Autosar官方文档】

5.4 installation and use of pyinstaller Library

NLP natural language processing - Introduction to machine learning and natural language processing (I)

Uni native plug-in development -- Youmeng one click login
![[AUTOSAR cantp 1. learn the network layer protocol of UDS diagnosis]](/img/dc/51a4f091c623dbaaa4c174ebdae92a.png)
[AUTOSAR cantp 1. learn the network layer protocol of UDS diagnosis]

二叉树基础oj练习-

堆的实现与堆排序实现

Questions and answers of basic principles of steel structure

5.4 Pyinstaller库安装与使用
随机推荐
博客搭建二:NexT主题相关设置beta
Jetson TX1安装 Pytorch
广播,组播,单播
Baidu Shen Shuo: focus on the scene, deeply cultivate the industry, and bring practical results to enterprise Digitalization
NLP natural language processing - Introduction to machine learning and natural language processing (I)
【AUTOSAR DCM 1.模块简介(DSL,DSD,DSP)】
高电压技术学习总结
冒泡排序,快速排序
3.2daydayup draw inferences from one instance: three days of fishing and two days of net learning
高分子合成工艺学复习考题
A comprehensive and detailed summary of the basic principles of steel structure
C语言:详细讲解基于tcp和udp的两种本地通信方式
编码器的一点理解
嵌入式从入门到精通(入土)——超详细知识点分享3
对字符串函数的使用和理解(2)
C语言:基于顺序表的学生管理系统,超级详细,全部都有注释,看完不懂来扇我。
Blog Building III: comment system selection
二叉树基础oj练习-
[AUTOSAR DEM iv.event memory]
VS属性配置相关知识