By default, if you are running OpenShift 4+, it comes with a nice Cluster Operator called “monitoring”. The Cluster Operator allows you to monitor the OpenShift nodes and Kubernetes API, it provides information regarding the cluster’s state, pods, and other cluster-wide diagnostics.
Cluster Operators are configured during the installation of the cluster and provide the core services for the OpenShift cluster.
An issue arose when developers and operators in different organizations realized that monitoring Kubernetes resources alone is not enough. Therefore, A new feature has been added to OpenShift’s monitoring stack — The ability for developers to monitor their own services and produce service-relevant metrics.
Demo — Monitoring a MariaDB database on OpenShift
In this demo I will go through the procedure of adding a service into the OpenShift Monitoring Stack, my service in this case will be MariaDB. The instructions in this demo might be specific for MariaDB, but they are relevant to any service which is able to expose metrics to Prometheus.
More information and examples can be found at the Official Documentation.
Prerequisites
- A running OpenShift 4.5 cluster.
- A running MariaDB server.
Configuration Walk-through
- Since monitoring your own services is not enabled in the monitoring stack by default, we will need to enable it by creating a new config map in the openshift-monitoring namespace.
- After creating the new file, apply the new config map to the cluster.
- Note that a new namespace has been created in the OpenShift cluster. The Prometheus server in this namespace will be in charge of the custom services that users in the cluster will monitor.
- Validate the your MariaDB database is up and running.
- Configure the MySQL exporter so that the Prometheus server will be able to scrape metrics and save them in it’s Time Series DataBase. The exporter will receive connection parameters to the DB as an argument, so it could access the data needed for metrics production.
Prometheus Exporters — Agents that expose the service’s metrics to a Prometheus server. The exporter accesses the service’s data, and exposes it in a unique format so Prometheus will be able to parse it and save it in it’s database.
Example:
- A pod will be created for the exporter. Make sure that it is up and running.
- (Optional) To check whether the exporter is producing metrics, you could expose the service as a route, and try to access it.
- (Optional) After the route has been created, try accessing the route and check out the metrics!
- Before continuing, validate the port name of the exporter’s service.
- After validating the exporter, create the ServiceMonitor CR to connect the newly created exporter to OpenShift’s monitoring stack.
- Create the ServiceMonitor CR.
- Connect to the Administration Portal in the OpenShift console. Navigate to the Monitoring →Metrics tab.
- You will be able to search MariaDB’s metrics in the ‘Metrics’ tab. Try looking for the ‘mysql_up’ metric. And press on ‘Run Queries’.
Configuration Alerts
Now that you are monitoring your custom services on top of OpenShift, you might need to get notifications if something is wrong with your services.
In this part, I will demonstrate the configuration of an alert that will trigger if the MariaDB service will go down for some reason.
- Create a file for a PrometheusRule CR. The PrometheusRule will contain a condition for which the alert will be triggered. In our case it is — `mysql_up{job=”mysqld-exporter”} != 1` Which will trigger the alert if the mysql_up metric will return values which are not 1 (1 represents that MariaDB is up).
- Create the PrometheusRule CR.
- To check the alert status, you will need to obtain the route of the Thanos Ruler (Thanos Ruler will manage and aggregate alerts created for custom services).
- Check out the alert using the web browser.
- Let’s trigger the alert by removing the MariaDB’s service, thereby, disconnecting the DB from the network (You could use a different method to trigger the alert).
- Note that the alert has been triggered.
Conclusion
Monitoring in OpenShift is awesome. You can monitor the infrastructure and the services in the same monitoring stack! It is simple, intuitive and there is no need for extra tools or contracts.
As I mentioned before, the best thing in Prometheus is the fact that every platform or tool can be integrated into it very easily. Make sure to find or create the correct exporter for your service and you are set! Make sure to check Prometheus’s Documentation to fully understand its functions and features.
There are many other plugins and extra configurations that can be integrated into OpenShift’s monitoring stack. Make sure to check the Official Documentation for further features and examples.