This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.5.10. 使用 Prometheus 配置内置监控
本指南介绍了 Operator SDK 通过 Prometheus Operator 提供的内置监控支持,及其对 Operator 作者的详细用途。
5.10.1. Prometheus Operator 支持
Prometheus 是一个开源系统监视和警报工具包。Prometheus Operator 会创建、配置和管理在基于 Kubernetes 的集群(如 OpenShift Container Platform)中运行的 Prometheus 集群。
默认情况下,Operator SDK 中包括帮助函数,用于在任何生成的 Go-based Operator 中自动设置指标,以便在部署了 Prometheus Operator 的集群上使用。
5.10.2. 指标帮助函数
在使用 Operator SDK 生成的基于 Go 的 Operator 中,以下函数会公开有关运行中程序的一般指标:
func ExposeMetricsPort(ctx context.Context, port int32) (*v1.Service, error)
func ExposeMetricsPort(ctx context.Context, port int32) (*v1.Service, error)
这些指标从 controller-runtime
库 API 继承而来。默认在 0.0.0.0:8383/metrics
上提供指标。
创建一个 Service
对象并公开指标端口,之后可通过 Prometheus 访问该端口。删除领导 Pod 的 root
所有者时,Service
对象便会被垃圾回收。
以下示例出现在使用 Operator SDK 生成的所有 Operator 的 cmd/manager/main.go
文件中:
import( "github.com/operator-framework/operator-sdk/pkg/metrics" "machine.openshift.io/controller-runtime/pkg/manager" ) var ( // Change the below variables to serve metrics on a different host or port. metricsHost = "0.0.0.0" metricsPort int32 = 8383 ) ... func main() { ... // Pass metrics address to controller-runtime manager mgr, err := manager.New(cfg, manager.Options{ Namespace: namespace, MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort), }) ... // Create Service object to expose the metrics port. _, err = metrics.ExposeMetricsPort(ctx, metricsPort) if err != nil { // handle error log.Info(err.Error()) } ... }
import(
"github.com/operator-framework/operator-sdk/pkg/metrics"
"machine.openshift.io/controller-runtime/pkg/manager"
)
var (
// Change the below variables to serve metrics on a different host or port.
metricsHost = "0.0.0.0"
metricsPort int32 = 8383
)
...
func main() {
...
// Pass metrics address to controller-runtime manager
mgr, err := manager.New(cfg, manager.Options{
Namespace: namespace,
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
})
...
// Create Service object to expose the metrics port.
_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
if err != nil {
// handle error
log.Info(err.Error())
}
...
}
5.10.2.1. 修改指标端口
Operator 作者可修改在其上公开指标的端口。
先决条件
- 使用 Operator SDK 生成基于 Go 的 Operator
- 基于 Kubernetes 的集群已部署 Prometheus Operator
流程
在生成的 Operator 的
cmd/manager/main.go
文件中,在以下行中更改metricsPort
的值:var metricsPort int32 = 8383
var metricsPort int32 = 8383
Copy to Clipboard Copied!
5.10.3. 服务监控器
ServiceMonitor
是 Prometheus Operator 提供的自定义资源,用于发现 Service
对象中的 Endpoints
,配置 Prometheus 以监控这些 pod。
在使用 Operator SDK 生成的基于 Go 的 Operator 中,GenerateServiceMonitor()
帮助函数可以获取 Service
对象并基于该对象生成 ServiceMonitor
对象。
5.10.3.1. 创建服务监控器
Operator 作者可使用 metrics.CreateServiceMonitor()
帮助函数来添加已创建监控服务的服务目标发现,该函数接受新创建的 Service。
先决条件
- 使用 Operator SDK 生成基于 Go 的 Operator
- 基于 Kubernetes 的集群已部署 Prometheus Operator
流程
将
metrics.CreateServiceMonitor()
帮助函数添加至您的 Operator 代码中:import( "k8s.io/api/core/v1" "github.com/operator-framework/operator-sdk/pkg/metrics" "machine.openshift.io/controller-runtime/pkg/client/config" ) func main() { ... // Populate below with the Service(s) for which you want to create ServiceMonitors. services := []*v1.Service{} // Create one ServiceMonitor per application per namespace. // Change the below value to name of the Namespace you want the ServiceMonitor to be created in. ns := "default" // restConfig is used for talking to the Kubernetes apiserver restConfig := config.GetConfig() // Pass the Service(s) to the helper function, which in turn returns the array of ServiceMonitor objects. serviceMonitors, err := metrics.CreateServiceMonitors(restConfig, ns, services) if err != nil { // Handle errors here. } ... }
import( "k8s.io/api/core/v1" "github.com/operator-framework/operator-sdk/pkg/metrics" "machine.openshift.io/controller-runtime/pkg/client/config" ) func main() { ... // Populate below with the Service(s) for which you want to create ServiceMonitors. services := []*v1.Service{} // Create one ServiceMonitor per application per namespace. // Change the below value to name of the Namespace you want the ServiceMonitor to be created in. ns := "default" // restConfig is used for talking to the Kubernetes apiserver restConfig := config.GetConfig() // Pass the Service(s) to the helper function, which in turn returns the array of ServiceMonitor objects. serviceMonitors, err := metrics.CreateServiceMonitors(restConfig, ns, services) if err != nil { // Handle errors here. } ... }
Copy to Clipboard Copied!