From 732f3030d0bbb1801c0e2b5ef1b056c47bc440c1 Mon Sep 17 00:00:00 2001 From: Umanga Chapagain Date: Mon, 17 Jun 2019 15:26:28 +0530 Subject: [PATCH] integrate prometheus based monitoring with rook-ceph > add new field to cluster spec > deploy servicemonitor > deploy prometheusRule Signed-off-by: Umanga Chapagain --- Documentation/ceph-monitoring.md | 13 ++- Gopkg.lock | 17 ++++ .../kubernetes/ceph/cluster-minimal.yaml | 3 + .../kubernetes/ceph/cluster-test.yaml | 3 + cluster/examples/kubernetes/ceph/cluster.yaml | 10 ++ ...es.yaml => prometheus-ceph-v14-rules.yaml} | 0 images/ceph/Dockerfile | 2 +- images/ceph/Makefile | 1 + pkg/apis/ceph.rook.io/v1/types.go | 14 +++ .../ceph.rook.io/v1/zz_generated.deepcopy.go | 17 ++++ pkg/operator/ceph/cluster/cluster.go | 2 +- pkg/operator/ceph/cluster/mgr/mgr.go | 73 ++++++++++++++ pkg/operator/ceph/cluster/mgr/mgr_test.go | 1 + pkg/operator/ceph/cluster/mgr/spec_test.go | 4 + pkg/operator/k8sutil/prometheus.go | 95 +++++++++++++++++++ pkg/operator/k8sutil/prometheus_test.go | 32 +++++++ 16 files changed, 283 insertions(+), 4 deletions(-) rename cluster/examples/kubernetes/ceph/monitoring/{prometheus-ceph-rules.yaml => prometheus-ceph-v14-rules.yaml} (100%) create mode 100644 pkg/operator/k8sutil/prometheus.go create mode 100644 pkg/operator/k8sutil/prometheus_test.go diff --git a/Documentation/ceph-monitoring.md b/Documentation/ceph-monitoring.md index f3f1e5fa7..022a6dac4 100644 --- a/Documentation/ceph-monitoring.md +++ b/Documentation/ceph-monitoring.md @@ -81,11 +81,20 @@ You can find Prometheus Consoles here: https://github.com/ceph/cephmetrics/tree/ A guide to how you can write your own Prometheus consoles can be found on the official Prometheus site here: https://prometheus.io/docs/visualization/consoles/. ## Prometheus Alerts -To enable Prometheus based alerting for Ceph, run the following commands: +To enable prometheus alerts during cluster deployment, make following changes to `cluster.yaml` +```YAML +monitoring: + enabled: true + rulesNamespace: "rook-ceph" ``` +_Note: This expects prometheus to be pre-installed by the admin._ + +To enable prometheus alerts after cluster is deployed, run the following commands: +```BASH cd cluster/examples/kubernetes/ceph/monitoring -kubectl create -f prometheus-ceph-rules.yaml +kubectl create -f prometheus-ceph-v-rules.yaml ``` +_where, VERSION is the version of ceph cluster to be monitored_ ## Grafana Dashboards The dashboards have been created by [@galexrt](https://github.com/galexrt). For feedback on the dashboards please reach out to him on the [Rook.io Slack](https://slack.rook.io). diff --git a/Gopkg.lock b/Gopkg.lock index edf9e110a..67125ec37 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -112,6 +112,20 @@ revision = "97fdf19511ea361ae1c100dd393cc47f8dcfa1e1" version = "v4" +[[projects]] + digest = "1:32945438a670edb831120e35007cbb4ba17218e0c5fd52566ba7c56043ae0d96" + name = "github.com/coreos/prometheus-operator" + packages = [ + "pkg/apis/monitoring", + "pkg/apis/monitoring/v1", + "pkg/client/versioned", + "pkg/client/versioned/scheme", + "pkg/client/versioned/typed/monitoring/v1", + ] + pruneopts = "UT" + revision = "18fbf558ab7f8809fd610a3dc50bf483508dc1bb" + version = "v0.30.1" + [[projects]] digest = "1:86909a5b1e4522ad763f86327cfa65e51d7eb838d4adb3d6f68069fede181122" name = "github.com/corpix/uarand" @@ -1367,6 +1381,8 @@ "github.com/aws/aws-sdk-go/aws/session", "github.com/aws/aws-sdk-go/service/s3", "github.com/coreos/pkg/capnslog", + "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1", + "github.com/coreos/prometheus-operator/pkg/client/versioned", "github.com/davecgh/go-spew/spew", "github.com/ghodss/yaml", "github.com/go-ini/ini", @@ -1408,6 +1424,7 @@ "k8s.io/apimachinery/pkg/util/validation", "k8s.io/apimachinery/pkg/util/version", "k8s.io/apimachinery/pkg/util/wait", + "k8s.io/apimachinery/pkg/util/yaml", "k8s.io/apimachinery/pkg/watch", "k8s.io/apiserver/pkg/server", "k8s.io/client-go/discovery", diff --git a/cluster/examples/kubernetes/ceph/cluster-minimal.yaml b/cluster/examples/kubernetes/ceph/cluster-minimal.yaml index bccef11b8..2426e503c 100644 --- a/cluster/examples/kubernetes/ceph/cluster-minimal.yaml +++ b/cluster/examples/kubernetes/ceph/cluster-minimal.yaml @@ -24,5 +24,8 @@ spec: allowMultiplePerNode: false dashboard: enabled: true + monitoring: + enabled: false # requires Prometheus to be pre-installed + rulesNamespace: rook-ceph network: hostNetwork: false diff --git a/cluster/examples/kubernetes/ceph/cluster-test.yaml b/cluster/examples/kubernetes/ceph/cluster-test.yaml index 6413a3454..b5d32f022 100644 --- a/cluster/examples/kubernetes/ceph/cluster-test.yaml +++ b/cluster/examples/kubernetes/ceph/cluster-test.yaml @@ -22,6 +22,9 @@ spec: allowMultiplePerNode: true dashboard: enabled: true + monitoring: + enabled: false # requires Prometheus to be pre-installed + rulesNamespace: rook-ceph network: hostNetwork: false rbdMirroring: diff --git a/cluster/examples/kubernetes/ceph/cluster.yaml b/cluster/examples/kubernetes/ceph/cluster.yaml index 233b2a7c1..f92da11fe 100644 --- a/cluster/examples/kubernetes/ceph/cluster.yaml +++ b/cluster/examples/kubernetes/ceph/cluster.yaml @@ -41,6 +41,16 @@ spec: # port: 8443 # serve the dashboard using SSL # ssl: true + # enable prometheus alerting for cluster + monitoring: + # requires Prometheus to be pre-installed + enabled: false + # namespace to deploy prometheusRule in. If empty, namespace of the cluster will be used. + # Recommended: + # If you have a single rook-ceph cluster, set the rulesNamespace to the same namespace as the cluster or keep it empty. + # If you have multiple rook-ceph clusters in the same k8s cluster, choose the same namespace (ideally, namespace with prometheus + # deployed) to set rulesNamespace for all the clusters. Otherwise, you will get duplicate alerts with multiple alert definitions. + rulesNamespace: rook-ceph network: # toggle to use hostNetwork hostNetwork: false diff --git a/cluster/examples/kubernetes/ceph/monitoring/prometheus-ceph-rules.yaml b/cluster/examples/kubernetes/ceph/monitoring/prometheus-ceph-v14-rules.yaml similarity index 100% rename from cluster/examples/kubernetes/ceph/monitoring/prometheus-ceph-rules.yaml rename to cluster/examples/kubernetes/ceph/monitoring/prometheus-ceph-v14-rules.yaml diff --git a/images/ceph/Dockerfile b/images/ceph/Dockerfile index 40fa723c1..bca2051b0 100644 --- a/images/ceph/Dockerfile +++ b/images/ceph/Dockerfile @@ -24,6 +24,6 @@ RUN curl --fail -sSL -o /tini https://github.com/krallin/tini/releases/download/ COPY rook rookflex toolbox.sh /usr/local/bin/ COPY ceph-csi /etc/ceph-csi - +COPY ceph-monitoring /etc/ceph-monitoring ENTRYPOINT ["/tini", "--", "/usr/local/bin/rook"] CMD [""] diff --git a/images/ceph/Makefile b/images/ceph/Makefile index 0be293f89..bf3661c80 100755 --- a/images/ceph/Makefile +++ b/images/ceph/Makefile @@ -39,6 +39,7 @@ do.build: @cp $(OUTPUT_DIR)/bin/linux_$(GOARCH)/rook $(TEMP) @cp $(OUTPUT_DIR)/bin/linux_$(GOARCH)/rookflex $(TEMP) @cp -r ../../cluster/examples/kubernetes/ceph/csi/template $(TEMP)/ceph-csi + @cp -r ../../cluster/examples/kubernetes/ceph/monitoring $(TEMP)/ceph-monitoring @cd $(TEMP) && $(SED_CMD) 's|BASEIMAGE|$(BASEIMAGE)|g' Dockerfile @$(DOCKERCMD) build $(BUILD_ARGS) \ --build-arg ARCH=$(GOARCH) \ diff --git a/pkg/apis/ceph.rook.io/v1/types.go b/pkg/apis/ceph.rook.io/v1/types.go index 8dd60941d..d670eca98 100755 --- a/pkg/apis/ceph.rook.io/v1/types.go +++ b/pkg/apis/ceph.rook.io/v1/types.go @@ -77,6 +77,9 @@ type ClusterSpec struct { // Dashboard settings Dashboard DashboardSpec `json:"dashboard,omitempty"` + + // Prometheus based Monitoring settings + Monitoring MonitoringSpec `json:"monitoring,omitempty"` } // VersionSpec represents the settings for the Ceph version that Rook is orchestrating. @@ -100,6 +103,17 @@ type DashboardSpec struct { SSL *bool `json:"ssl,omitempty"` } +// MonitoringSpec represents the settings for Prometheus based Ceph monitoring +type MonitoringSpec struct { + // Whether to create the prometheus rules for the ceph cluster. If true, the prometheus + // types must exist or the creation will fail. + Enabled bool `json:"enabled,omitempty"` + + // The namespace where the prometheus rules and alerts should be created. + // If empty, the same namespace as the cluster will be used. + RulesNamespace string `json:"rulesNamespace,omitempty"` +} + type ClusterStatus struct { State ClusterState `json:"state,omitempty"` Message string `json:"message,omitempty"` diff --git a/pkg/apis/ceph.rook.io/v1/zz_generated.deepcopy.go b/pkg/apis/ceph.rook.io/v1/zz_generated.deepcopy.go index 36dbc02bd..f0d23fc8a 100644 --- a/pkg/apis/ceph.rook.io/v1/zz_generated.deepcopy.go +++ b/pkg/apis/ceph.rook.io/v1/zz_generated.deepcopy.go @@ -481,6 +481,7 @@ func (in *ClusterSpec) DeepCopyInto(out *ClusterSpec) { out.Mon = in.Mon out.RBDMirroring = in.RBDMirroring in.Dashboard.DeepCopyInto(&out.Dashboard) + out.Monitoring = in.Monitoring return } @@ -682,6 +683,22 @@ func (in *MonSpec) DeepCopy() *MonSpec { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *MonitoringSpec) DeepCopyInto(out *MonitoringSpec) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new MonitoringSpec. +func (in *MonitoringSpec) DeepCopy() *MonitoringSpec { + if in == nil { + return nil + } + out := new(MonitoringSpec) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *NFSGaneshaSpec) DeepCopyInto(out *NFSGaneshaSpec) { *out = *in diff --git a/pkg/operator/ceph/cluster/cluster.go b/pkg/operator/ceph/cluster/cluster.go index c49ea5bc0..4ecf2f297 100755 --- a/pkg/operator/ceph/cluster/cluster.go +++ b/pkg/operator/ceph/cluster/cluster.go @@ -204,7 +204,7 @@ func (c *cluster) doOrchestration(rookImage string, cephVersion cephver.CephVers mgrs := mgr.New(c.Info, c.context, c.Namespace, rookImage, spec.CephVersion, cephv1.GetMgrPlacement(spec.Placement), cephv1.GetMgrAnnotations(c.Spec.Annotations), - spec.Network.HostNetwork, spec.Dashboard, cephv1.GetMgrResources(spec.Resources), c.ownerRef, c.Spec.DataDirHostPath) + spec.Network.HostNetwork, spec.Dashboard, spec.Monitoring, cephv1.GetMgrResources(spec.Resources), c.ownerRef, c.Spec.DataDirHostPath) err = mgrs.Start() if err != nil { return fmt.Errorf("failed to start the ceph mgr. %+v", err) diff --git a/pkg/operator/ceph/cluster/mgr/mgr.go b/pkg/operator/ceph/cluster/mgr/mgr.go index 720dafa59..e2653a471 100644 --- a/pkg/operator/ceph/cluster/mgr/mgr.go +++ b/pkg/operator/ceph/cluster/mgr/mgr.go @@ -19,6 +19,9 @@ package mgr import ( "fmt" + "path" + "strconv" + "strings" "github.com/coreos/pkg/capnslog" cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" @@ -36,11 +39,15 @@ import ( var logger = capnslog.NewPackageLogger("github.com/rook/rook", "op-mgr") +var prometheusRuleName = "prometheus-ceph-vVERSION-rules" + const ( appName = "rook-ceph-mgr" serviceAccountName = "rook-ceph-mgr" prometheusModuleName = "prometheus" metricsPort = 9283 + monitoringPath = "/etc/ceph-monitoring/" + serviceMonitorFile = "service-monitor.yaml" // minimum amount of memory in MB to run the pod cephMgrPodMinimumMemory uint64 = 512 ) @@ -58,6 +65,7 @@ type Cluster struct { resources v1.ResourceRequirements ownerRef metav1.OwnerReference dashboard cephv1.DashboardSpec + monitoringSpec cephv1.MonitoringSpec cephVersion cephv1.CephVersionSpec rookVersion string exitCode func(err error) (int, bool) @@ -74,6 +82,7 @@ func New( annotations rookalpha.Annotations, hostNetwork bool, dashboard cephv1.DashboardSpec, + monitoringSpec cephv1.MonitoringSpec, resources v1.ResourceRequirements, ownerRef metav1.OwnerReference, dataDirHostPath string, @@ -88,6 +97,7 @@ func New( Replicas: 1, dataDir: k8sutil.DataDir, dashboard: dashboard, + monitoringSpec: monitoringSpec, HostNetwork: hostNetwork, resources: resources, ownerRef: ownerRef, @@ -171,6 +181,32 @@ func (c *Cluster) Start() error { logger.Infof("mgr metrics service started") } + // enable monitoring if `monitoring: enabled: true` + if c.monitoringSpec.Enabled { + if c.clusterInfo.CephVersion.IsAtLeastNautilus() { + logger.Infof("starting monitoring deployment") + // servicemonitor takes some metadata from the service for easy mapping + if err := c.enableServiceMonitor(service); err != nil { + logger.Errorf("failed to enable service monitor. %+v", err) + } else { + logger.Infof("servicemonitor enabled") + } + // namespace in which the prometheusRule should be deployed + // if left empty, it will be deployed in current namespace + namespace := c.monitoringSpec.RulesNamespace + if namespace == "" { + namespace = c.Namespace + } + if err := c.deployPrometheusRule(prometheusRuleName, namespace); err != nil { + logger.Errorf("failed to deploy prometheusule. %+v", err) + } else { + logger.Infof("prometheusRule deployed") + } + logger.Debugf("ended monitoring deployment") + } else { + logger.Debugf("monitoring not supported for ceph versions