Prometheus Installation and Scraping Configuration
Provides necessary installation and configuration steps to collect Apinizer Gateway metrics with Prometheus. You can integrate Gateway metrics with Prometheus using Constant and Dynamic scraping methods, configure automatic metric collection in Kubernetes environment, and perform Prometheus installation.
Prometheus is a monitoring system that pulls metrics from specific targets and stores them as time series data. Scraping is the process by which Prometheus periodically collects metrics from target services. Apinizer Gateway’s metrics are provided via port 9091, and Prometheus can pull these metrics in two different ways:
Constant Scraping
In the fixed scraping method, services to be monitored are defined under static_configs with predetermined fixed IP addresses or DNS names. Prometheus queries these services at regular intervals. This method is useful when service addresses do not change or can be defined manually.
Dynamic Scraping
In the dynamic scraping method, Prometheus automatically discovers services through a service discovery mechanism such as Kubernetes. kubernetes_sd_config or similar configurations enable Prometheus to automatically discover services without the need to update Prometheus configuration as services change. This method provides great advantages in microservice architectures and constantly changing infrastructures.
For Apinizer Gateway to publish metrics via port 9091, the METRICS_ENABLED=TRUE parameter must be defined as an environment variable to the relevant container.
In Constant Scraping configuration, the service name from which Prometheus will scrape metrics must be specified. Therefore, a service that will route to port 9091 of the Apinizer Worker component must be created.
For Apinizer Gateway to provide metrics to Prometheus from port 9091, Prometheus’s scraping configuration must be done via ConfigMap. Constant scraping configuration is performed by defining Prometheus’s worker-prometheus-service named service in static_configs at the specified address.
The dynamic scraping method enables Prometheus to automatically discover pods within Kubernetes. Thanks to this method, there is no need to make manual scraping configuration for each new pod. Prometheus automatically pulls metrics using specific annotations added to pods.
For Gateway metrics to be collected by Prometheus, the METRICS_ENABLED=TRUE variable must be added by selecting Worker edit deployment option in the Deployments & Services section on the Gateway Environments page.
If Kubernetes Management is Not Done with Apinizer
To enable Prometheus to collect metrics from Worker pods, relevant annotations must be added to the spec.template.metadata.annotations section of the relevant Deployment manifest. In addition, for Apinizer Gateway to publish metrics via port 9091, the METRICS_ENABLED=TRUE parameter must be defined as an environment variable to the relevant container.
This way, the metrics service will be exported via port 9091 and Prometheus will automatically discover and scrape metrics from the Apinizer Gateway pod running on port 9091.
Dynamic scraping must be enabled using kubernetes_sd_configs in Prometheus’s ConfigMap configuration to discover annotations in pods.
The ConfigMap example below enables Prometheus to dynamically discover Kubernetes pods. This configuration enables Prometheus to automatically discover Kubernetes pods and collect metrics only from pods with the prometheus.io/scrape: "true" annotation. Thus, metrics can be collected by Prometheus by applying dynamic scraping to Apinizer Gateway without the need for manual target definition.
Since Prometheus’s metrics will be stored on a node in the Kubernetes cluster, PersistentVolume (PV) and PersistentVolumeClaim (PVC) definitions must be made. This configuration ensures that Prometheus preserves its data in case of shutdown or restart.
The PersistentVolume (PV) and PersistentVolumeClaim (PVC) configuration above ensures that Prometheus stores its data on a specific node. However, the hostPath used here is dependent on the local file system of the specific node where Prometheus runs.Therefore:
If Prometheus pods are moved to a different node, they will lose their data unless the same hostPath directory exists on the new node.
To guarantee that pods always run on the same node, pods must be pinned to specific nodes using nodeAffinity or nodeSelector.
Alternatively, NFS, Ceph, Longhorn or a cloud-based storage solution can be used to store data in a node-independent manner.
Prometheus must have the necessary permissions to discover pods and collect their metrics. For this, the following ServiceAccount, ClusterRole and ClusterRoleBinding definitions must be made:
When Prometheus is deployed on Kubernetes, it creates a Kubernetes service named prometheus-service and of type NodePort. This service is necessary for accessing Prometheus from outside Kubernetes. However, you can adapt this service according to the structure you use for Ingress or connection method in your organization.