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.4.7. Configuring built-in monitoring with Prometheus
This guide describes the built-in monitoring support provided by the Operator SDK using the Prometheus Operator and details usage for Operator authors.
4.7.1. Prometheus Operator support 复制链接链接已复制到粘贴板!
Prometheus is an open-source systems monitoring and alerting toolkit. The Prometheus Operator creates, configures, and manages Prometheus clusters running on Kubernetes-based clusters, such as OpenShift Container Platform.
Helper functions exist in the Operator SDK by default to automatically set up metrics in any generated Go-based Operator for use on clusters where the Prometheus Operator is deployed.
4.7.2. Metrics helper 复制链接链接已复制到粘贴板!
In Go-based Operators generated using the Operator SDK, the following function exposes general metrics about the running program:
func ExposeMetricsPort(ctx context.Context, port int32) (*v1.Service, error)
func ExposeMetricsPort(ctx context.Context, port int32) (*v1.Service, error)
These metrics are inherited from the controller-runtime
library API. By default, the metrics are served on 0.0.0.0:8383/metrics
.
A Service
object is created with the metrics port exposed, which can be then accessed by Prometheus. The Service
object is garbage collected when the leader pod’s root
owner is deleted.
The following example is present in the cmd/manager/main.go
file in all Operators generated using the Operator SDK:
4.7.2.1. Modifying the metrics port 复制链接链接已复制到粘贴板!
Operator authors can modify the port that metrics are exposed on.
Prerequisites
- Go-based Operator generated using the Operator SDK
- Kubernetes-based cluster with the Prometheus Operator deployed
Procedure
In the
cmd/manager/main.go
file of the generated Operator, change the value ofmetricsPort
in the following line:var metricsPort int32 = 8383
var metricsPort int32 = 8383
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.7.3. Service monitors 复制链接链接已复制到粘贴板!
A ServiceMonitor
is a custom resource provided by the Prometheus Operator that discovers the Endpoints
in Service
objects and configures Prometheus to monitor those pods.
In Go-based Operators generated using the Operator SDK, the GenerateServiceMonitor()
helper function can take a Service
object and generate a ServiceMonitor
object based on it.
Additional resources
-
See the Prometheus Operator documentation for more information about the
ServiceMonitor
custom resource definition (CRD).
4.7.3.1. Creating service monitors 复制链接链接已复制到粘贴板!
Operator authors can add service target discovery of created monitoring services using the metrics.CreateServiceMonitor()
helper function, which accepts the newly created service.
Prerequisites
- Go-based Operator generated using the Operator SDK
- Kubernetes-based cluster with the Prometheus Operator deployed
Procedure
Add the
metrics.CreateServiceMonitor()
helper function to your Operator code:Copy to Clipboard Copied! Toggle word wrap Toggle overflow