5.11. 使用 Prometheus 配置内置监控

本指南介绍了 Operator SDK 通过 Prometheus Operator 提供的内置监控支持,及其对 Operator 作者的详细用途。

5.11.1. Prometheus Operator 支持

Prometheus 是一个开源系统监视和警报工具包。Prometheus Operator 会创建、配置和管理在基于 Kubernetes 的集群(如 OpenShift Container Platform)中运行的 Prometheus 集群。

默认情况下,Operator SDK 中包括帮助函数,用于在任何生成的 Go-based Operator 中自动设置指标,以便在部署了 Prometheus Operator 的集群上使用。

5.11.2. 公开自定义指标

作为 Operator 作者,您可以使用 controller-runtime/pkg/metrics 库中的全局 Prometheus registry 发布自定义指标。

先决条件

  • 使用 Operator SDK 生成基于 Go 的 Operator
  • Prometheus Operator (默认情况下在 OpenShift Container Platform 集群中部署)

流程

  1. 在 Operator SDK 项目中,取消注释 config/default/kustomization.yaml 文件中的以下行:

    ../prometheus
  2. 创建自定义控制器类,以便从 Operator 发布其他指标。以下示例将 widgetswidgetFailures 收集器声明为全局变量,然后将它们注册到控制器的软件包中的 init() 函数:

    例 5.6. controllers/memcached_controller_test_metrics.go 文件

    package controllers
    
    import (
    	"github.com/prometheus/client_golang/prometheus"
    	"sigs.k8s.io/controller-runtime/pkg/metrics"
    )
    
    
    var (
        widgets = prometheus.NewCounter(
            prometheus.CounterOpts{
                Name: "widgets_total",
                Help: "Number of widgets processed",
            },
        )
        widgetFailures = prometheus.NewCounter(
            prometheus.CounterOpts{
                Name: "widget_failures_total",
                Help: "Number of failed widgets",
            },
        )
    )
    
    func init() {
        // Register custom metrics with the global prometheus registry
        metrics.Registry.MustRegister(widgets, widgetFailures)
    }
  3. 记录这些收集器来自 main 控制器类中协调循环的任何部分,这决定了指标的业务逻辑:

    例 5.7. controllers/memcached_controller.go 文件

    func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
    	...
    	...
    	// Add metrics
    	widgets.Inc()
    	widgetFailures.Inc()
    
    	return ctrl.Result{}, nil
    }
  4. 构建和推送 Operator:

    $ make docker-build docker-push IMG=<registry>/<user>/<image_name>:<tag>
  5. 部署 Operator:

    $ make deploy IMG=<registry>/<user>/<image_name>:<tag>
  6. 创建角色和角色绑定定义,以允许 OpenShift Container Platform 集群的 Prometheus 实例提取 Operator 的服务监控。

    必须分配角色,以便服务帐户具有提取命名空间指标的权限:

    例 5.8. config/prometheus/role.yaml 角色

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: prometheus-k8s-role
      namespace: <operator_namespace>
    rules:
      - apiGroups:
          - ""
        resources:
          - endpoints
          - pods
          - services
          - nodes
          - secrets
        verbs:
          - get
          - list
          - watch

    例 5.9. config/prometheus/rolebinding.yaml 角色绑定

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: prometheus-k8s-rolebinding
      namespace: memcached-operator-system
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: prometheus-k8s-role
    subjects:
      - kind: ServiceAccount
        name: prometheus-k8s
        namespace: openshift-monitoring
  7. 为部署的 Operator 应用角色和绑定:

    $ oc apply -f config/prometheus/role.yaml
    $ oc apply -f config/prometheus/rolebinding.yaml
  8. 为您要提取的命名空间设置标签,为该命名空间启用 OpenShift 集群监控:

    $ oc label namespace <operator_namespace> openshift.io/cluster-monitoring="true"

验证

  • 查询并查看 OpenShift Container Platform Web 控制台中的指标。您可以使用自定义控制器类中设置的名称,如 widgets_totalwidget_failures_total