Skip to content

Commit 3a83a0c

Browse files
authored
doc/user: Add metrics and service-monitor docs (#719)
* doc/user/metrics: Add metrics pkg docs Add documentation for metrics and serviceMonitor.
1 parent d1b3911 commit 3a83a0c

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

doc/user-guide.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ To implement complex deletion logic, you can add a finalizer to your Custom Reso
407407
deleted until you remove the finalizer (ie, after your cleanup logic has successfully run). For more information, see the
408408
[official Kubernetes documentation on finalizers](https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/#finalizers).
409409

410+
### Metrics
411+
412+
To learn about how metrics work in the Operator SDK read the [metrics section][metrics_doc] of the user documentation.
413+
410414
## Leader election
411415

412416
During the lifecycle of an operator it's possible that there may be more than 1 instance running at any given time e.g when rolling out an upgrade for the operator.
@@ -468,6 +472,7 @@ func main() {
468472
When the operator is not running in a cluster, the Manager will return an error on starting since it can't detect the operator's namespace in order to create the configmap for leader election. You can override this namespace by setting the Manager's `LeaderElectionNamespace` option.
469473

470474

475+
471476
[pod_eviction_timeout]: https://kubernetes.io/docs/reference/command-line-tools-reference/kube-controller-manager/#options
472477
[manager_options]: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/manager#Options
473478
[lease_split_brain]: https://github.com/kubernetes/client-go/blob/30b06a83d67458700a5378239df6b96948cb9160/tools/leaderelection/leaderelection.go#L21-L24
@@ -492,3 +497,4 @@ When the operator is not running in a cluster, the Manager will return an error
492497
[controller-go-doc]: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg#hdr-Controller
493498
[request-go-doc]: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/reconcile#Request
494499
[result_go_doc]: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/reconcile#Result
500+
[metrics_doc]: ./user/metrics/README.md

doc/user/metrics/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Monitoring with Prometheus
2+
3+
[Prometheus][prometheus] is an open-source systems monitoring and alerting toolkit. Below is the overview of the different helpers that exist in operator-sdk to help setup metrics in the generated operator.
4+
5+
## Metrics in operator-sdk
6+
7+
The `func ExposeMetricsPort(ctx context.Context, port int32) (*v1.Service, error)` function exposes general metrics about the running program. These metrics are inherited from controller-runtime. This helper function creates a [Service][service] object with the metrics port exposed, which can then be accessed by Prometheus. The Service object is [garbage collected][gc] when the leader pod's root owner is deleted.
8+
9+
By default, the metrics are served on `0.0.0.0:8383/metrics`. To modify the port the metrics are exposed on, change the `var metricsPort int32 = 8383` variable in the `cmd/manager/main.go` file of the generated operator.
10+
11+
### Usage:
12+
13+
```go
14+
import(
15+
"github.com/operator-framework/operator-sdk/pkg/metrics"
16+
"sigs.k8s.io/controller-runtime/pkg/manager"
17+
)
18+
19+
func main() {
20+
21+
...
22+
23+
// Change the below variables to serve metrics on different host or port.
24+
var metricsHost = "0.0.0.0"
25+
var metricsPort int32 = 8383
26+
27+
// Pass metrics address to controller-runtime manager
28+
mgr, err := manager.New(cfg, manager.Options{
29+
Namespace: namespace,
30+
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
31+
})
32+
33+
...
34+
35+
// Create Service object to expose the metrics port.
36+
_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
37+
if err != nil {
38+
// handle error
39+
log.Info(err.Error())
40+
}
41+
42+
...
43+
}
44+
```
45+
46+
*Note:* The above example is already present in `cmd/manager/main.go` in all the operators generated with operator-sdk.
47+
48+
[prometheus]: https://prometheus.io/
49+
[service]: https://kubernetes.io/docs/concepts/services-networking/service/
50+
[gc]: https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/#owners-and-dependents
51+

doc/user/metrics/service-monitor.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## Using the ServiceMonitor prometheus-operator CRD
2+
3+
[prometheus-operator][prom-operator] is an operator that creates, configures, and manages Prometheus clusters atop Kubernetes.
4+
5+
`ServiceMonitor` is a CustomResource of the prometheus-operator, which discovers the `Endpoints` in `Service` objects and configures Prometheus to monitor those pods. See the prometheus-operator [documentation][service-monitor] to learn more about `ServiceMonitor`.
6+
7+
The `GenerateServiceMonitor` function takes a `Service` object and generates a `ServiceMonitor` resource based on it. To add `Service` target discovery of your created monitoring `Service` you can use the `metrics.CreateServiceMonitor()` helper function, which accepts the newly created `Service`.
8+
9+
### Prerequisites:
10+
11+
- [prometheus-operator][prom-quickstart] needs to be deployed in the cluster.
12+
13+
### Usage example:
14+
15+
```go
16+
import(
17+
"k8s.io/api/core/v1"
18+
"github.com/operator-framework/operator-sdk/pkg/metrics"
19+
)
20+
21+
func main() {
22+
23+
...
24+
25+
// Populate below with the Service(s) for which you want to create ServiceMonitors.
26+
services := []*v1.Service{}
27+
28+
// Create one `ServiceMonitor` per application per namespace.
29+
// Change below value to name of the Namespace you want the `ServiceMonitor` to be created in.
30+
ns := "default"
31+
32+
// Pass the Service(s) to the helper function, which in turn returns the array of `ServiceMonitor` objects.
33+
serviceMonitors, err := metrics.CreateServiceMonitors(restConfig, ns, services)
34+
if err != nil {
35+
// handle error here
36+
}
37+
38+
...
39+
}
40+
```
41+
42+
[prom-operator]: https://github.com/coreos/prometheus-operator
43+
[service-monitor]: https://github.com/coreos/prometheus-operator/blob/7a25bf6b6bb2347dacb235659b73bc210117acc7/Documentation/design.md#servicemonitor
44+
[prom-quickstart]: https://github.com/coreos/prometheus-operator/tree/master/contrib/kube-prometheus#quickstart

0 commit comments

Comments
 (0)